mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
Refactor application creation. Remove internal logger package.
This commit is contained in:
parent
c03c41cb21
commit
429bb2bf17
10 changed files with 38 additions and 109 deletions
|
|
@ -25,9 +25,6 @@ import (
|
|||
|
||||
var globalApplication *App
|
||||
|
||||
// isDebugMode is true if the application is running in debug mode
|
||||
var isDebugMode func() bool
|
||||
|
||||
func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
|
@ -45,24 +42,9 @@ func New(appOptions Options) *App {
|
|||
return globalApplication
|
||||
}
|
||||
|
||||
// Patch isDebug if we aren't in prod mode
|
||||
if isDebugMode == nil {
|
||||
isDebugMode = func() bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
mergeApplicationDefaults(&appOptions)
|
||||
|
||||
result := &App{
|
||||
options: appOptions.getOptions(isDebugMode()),
|
||||
applicationEventListeners: make(map[uint][]*EventListener),
|
||||
windows: make(map[uint]*WebviewWindow),
|
||||
systemTrays: make(map[uint]*SystemTray),
|
||||
contextMenus: make(map[string]*Menu),
|
||||
Logger: appOptions.Logger,
|
||||
pid: os.Getpid(),
|
||||
}
|
||||
result := newApplication(&appOptions)
|
||||
globalApplication = result
|
||||
|
||||
if result.Logger == nil {
|
||||
|
|
@ -77,7 +59,7 @@ func New(appOptions Options) *App {
|
|||
Middleware: assetserver.Middleware(appOptions.Assets.Middleware),
|
||||
}
|
||||
|
||||
srv, err := assetserver.NewAssetServer(opts, false, nil, wailsruntime.RuntimeAssetsBundle, isDebugMode())
|
||||
srv, err := assetserver.NewAssetServer(opts, false, result.Logger, wailsruntime.RuntimeAssetsBundle, result.isDebugMode)
|
||||
if err != nil {
|
||||
result.fatal(err.Error())
|
||||
}
|
||||
|
|
@ -263,6 +245,16 @@ type App struct {
|
|||
|
||||
// Capabilities
|
||||
capabilities capabilities.Capabilities
|
||||
isDebugMode bool
|
||||
}
|
||||
|
||||
func (a *App) init() {
|
||||
a.applicationEventListeners = make(map[uint][]*EventListener)
|
||||
a.windows = make(map[uint]*WebviewWindow)
|
||||
a.systemTrays = make(map[uint]*SystemTray)
|
||||
a.contextMenus = make(map[string]*Menu)
|
||||
a.Logger = a.options.Logger
|
||||
a.pid = os.Getpid()
|
||||
}
|
||||
|
||||
func (a *App) getSystemTrayID() uint {
|
||||
|
|
|
|||
13
v3/pkg/application/application_debug.go
Normal file
13
v3/pkg/application/application_debug.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
//go:build !production
|
||||
|
||||
package application
|
||||
|
||||
// We use this to patch the application to production mode.
|
||||
func newApplication(options *Options) *App {
|
||||
result := &App{
|
||||
isDebugMode: true,
|
||||
options: options.getOptions(true),
|
||||
}
|
||||
result.init()
|
||||
return result
|
||||
}
|
||||
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
package application
|
||||
|
||||
// We use this to patch the application to production mode.
|
||||
func init() {
|
||||
isDebugMode = func() bool {
|
||||
return false
|
||||
func newApplication(options *Options) *App {
|
||||
result := &App{
|
||||
isDebugMode: false,
|
||||
options: options.getOptions(false),
|
||||
}
|
||||
result.init()
|
||||
return result
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1352,7 +1352,7 @@ func (w *windowsWebviewWindow) processRequest(req *edge.ICoreWebView2WebResource
|
|||
|
||||
func (w *windowsWebviewWindow) setupChromium() {
|
||||
chromium := w.chromium
|
||||
debugMode := isDebugMode()
|
||||
debugMode := globalApplication.isDebugMode
|
||||
|
||||
opts := w.parent.options.Windows
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package assetserver
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -35,7 +36,7 @@ type AssetServer struct {
|
|||
debug bool
|
||||
ipcJS func(*http.Request) []byte
|
||||
|
||||
logger Logger
|
||||
logger *slog.Logger
|
||||
runtime RuntimeAssets
|
||||
|
||||
servingFromDisk bool
|
||||
|
|
@ -55,7 +56,7 @@ type AssetServer struct {
|
|||
assetServerWebView
|
||||
}
|
||||
|
||||
func NewAssetServer(options *Options, servingFromDisk bool, logger Logger, runtime RuntimeAssets, debug bool) (*AssetServer, error) {
|
||||
func NewAssetServer(options *Options, servingFromDisk bool, logger *slog.Logger, runtime RuntimeAssets, debug bool) (*AssetServer, error) {
|
||||
handler, err := NewAssetHandler(options, logger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -64,7 +65,7 @@ func NewAssetServer(options *Options, servingFromDisk bool, logger Logger, runti
|
|||
return NewAssetServerWithHandler(handler, servingFromDisk, logger, runtime, debug)
|
||||
}
|
||||
|
||||
func NewAssetServerWithHandler(handler http.Handler, servingFromDisk bool, logger Logger, runtime RuntimeAssets, debug bool) (*AssetServer, error) {
|
||||
func NewAssetServerWithHandler(handler http.Handler, servingFromDisk bool, logger *slog.Logger, runtime RuntimeAssets, debug bool) (*AssetServer, error) {
|
||||
var buffer bytes.Buffer
|
||||
buffer.Write(runtime.RuntimeDesktopJS())
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package assetserver
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -12,7 +13,7 @@ The assetserver for the dev mode.
|
|||
Depending on the UserAgent it injects a websocket based IPC script into `index.html` or the default desktop IPC. The
|
||||
default desktop IPC is injected when the webview accesses the devserver.
|
||||
*/
|
||||
func NewDevAssetServer(handler http.Handler, servingFromDisk bool, logger Logger, runtime RuntimeAssets) (*AssetServer, error) {
|
||||
func NewDevAssetServer(handler http.Handler, servingFromDisk bool, logger *slog.Logger, runtime RuntimeAssets) (*AssetServer, error) {
|
||||
result, err := NewAssetServerWithHandler(handler, servingFromDisk, logger, runtime, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
output []Output
|
||||
}
|
||||
|
||||
func New(outputs ...Output) *Logger {
|
||||
result := &Logger{}
|
||||
if outputs != nil {
|
||||
result.output = outputs
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (l *Logger) AddOutput(output Output) {
|
||||
l.output = append(l.output, output)
|
||||
}
|
||||
|
||||
func (l *Logger) Log(message *Message) {
|
||||
for _, o := range l.output {
|
||||
go o.Log(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Flush() {
|
||||
for _, o := range l.output {
|
||||
if err := o.Flush(); err != nil {
|
||||
fmt.Printf("Error flushing '%s' Logger: %s\n", o.Name(), err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
package logger
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Console struct{}
|
||||
|
||||
func (l *Console) Name() string {
|
||||
return "Console"
|
||||
}
|
||||
|
||||
func (l *Console) Log(message *Message) {
|
||||
msg := fmt.Sprintf(message.Message+"\n", message.Data...)
|
||||
level := ""
|
||||
if message.Level != "" {
|
||||
level = fmt.Sprintf("[%s] ", message.Level)
|
||||
}
|
||||
sender := ""
|
||||
if message.Sender != "" {
|
||||
sender = fmt.Sprintf("%s: ", message.Sender)
|
||||
}
|
||||
|
||||
fmt.Printf("%s%s%s", level, sender, msg)
|
||||
}
|
||||
|
||||
func (l *Console) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package logger
|
||||
|
||||
import "time"
|
||||
|
||||
type Message struct {
|
||||
Level string `json:"log"`
|
||||
Message string `json:"message"`
|
||||
Data []any `json:"data,omitempty"`
|
||||
Sender string `json:"-"`
|
||||
Time time.Time `json:"-"`
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package logger
|
||||
|
||||
type Output interface {
|
||||
Name() string
|
||||
Log(message *Message)
|
||||
Flush() error
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue