From 6cbead5bfe1357502653ab3e36537b673fa40243 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 13 Aug 2023 16:52:39 +1000 Subject: [PATCH] Improved logging++ --- v3/pkg/application/application.go | 23 +++++++--- v3/pkg/application/application_darwin.go | 2 + v3/pkg/application/application_debug.go | 45 ++++++++++++++++++++ v3/pkg/application/application_linux.go | 2 + v3/pkg/application/application_production.go | 2 + v3/pkg/application/application_windows.go | 14 ++++-- v3/pkg/w32/consts.go | 23 +++++++++- 7 files changed, 100 insertions(+), 11 deletions(-) diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 6f5578425..00e3e3f27 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -47,7 +47,7 @@ func New(appOptions Options) *App { result := newApplication(&appOptions) globalApplication = result - if result.Logger == nil { + if result.isDebugMode && result.Logger == nil { result.Logger = DefaultLogger() } @@ -328,21 +328,31 @@ func (a *App) GetPID() int { } func (a *App) info(message string, args ...any) { - a.Logger.Info(message, args...) + if a.Logger != nil { + a.Logger.Info(message, args...) + } } func (a *App) debug(message string, args ...any) { - a.Logger.Debug(message, args...) + if a.Logger != nil { + a.Logger.Debug(message, args...) + } } func (a *App) fatal(message string, args ...any) { msg := "A FATAL ERROR HAS OCCURRED: " + message - a.Logger.Error(msg, args...) + if a.Logger != nil { + a.Logger.Error(msg, args...) + } else { + println(msg) + } os.Exit(1) } func (a *App) error(message string, args ...any) { - a.Logger.Error(message, args...) + if a.Logger != nil { + a.Logger.Error(message, args...) + } } func (a *App) NewWebviewWindowWithOptions(windowOptions WebviewWindowOptions) *WebviewWindow { @@ -377,7 +387,8 @@ func (a *App) NewSystemTray() *SystemTray { } func (a *App) Run() error { - a.info("Starting application") + a.logStartup() + a.logPlatformInfo() // Setup panic handler defer processPanicHandlerRecover() diff --git a/v3/pkg/application/application_darwin.go b/v3/pkg/application/application_darwin.go index 1842f3c0a..b87d64a71 100644 --- a/v3/pkg/application/application_darwin.go +++ b/v3/pkg/application/application_darwin.go @@ -268,3 +268,5 @@ func setIcon(icon []byte) { } C.setApplicationIcon(unsafe.Pointer(&icon[0]), C.int(len(icon))) } + +func (a *App) logPlatformInfo() {} diff --git a/v3/pkg/application/application_debug.go b/v3/pkg/application/application_debug.go index 78f978c83..7edcbd080 100644 --- a/v3/pkg/application/application_debug.go +++ b/v3/pkg/application/application_debug.go @@ -2,6 +2,30 @@ package application +import ( + "github.com/samber/lo" + "github.com/wailsapp/wails/v3/internal/commands" + "path/filepath" + "runtime/debug" +) + +// BuildSettings contains the build settings for the application +var BuildSettings map[string]string + +// BuildInfo contains the build info for the application +var BuildInfo *debug.BuildInfo + +func init() { + var ok bool + BuildInfo, ok = debug.ReadBuildInfo() + if !ok { + return + } + BuildSettings = lo.Associate(BuildInfo.Settings, func(setting debug.BuildSetting) (string, string) { + return setting.Key, setting.Value + }) +} + // We use this to patch the application to production mode. func newApplication(options *Options) *App { result := &App{ @@ -11,3 +35,24 @@ func newApplication(options *Options) *App { result.init() return result } + +func (a *App) logStartup() { + var args []any + + wailsPackage, _ := lo.Find(BuildInfo.Deps, func(dep *debug.Module) bool { + return dep.Path == "github.com/wailsapp/wails/v3" + }) + + wailsVersion := commands.VersionString + if wailsPackage != nil && wailsPackage.Replace != nil { + wailsVersion = "(local) => " + filepath.ToSlash(wailsPackage.Replace.Path) + } + args = append(args, "Wails", wailsVersion) + args = append(args, "Compiler", BuildInfo.GoVersion) + + for key, value := range BuildSettings { + args = append(args, key, value) + } + + a.info("Build Info:", args...) +} diff --git a/v3/pkg/application/application_linux.go b/v3/pkg/application/application_linux.go index d53e02c94..cfb2505da 100644 --- a/v3/pkg/application/application_linux.go +++ b/v3/pkg/application/application_linux.go @@ -170,3 +170,5 @@ func setIcon(icon []byte) { //C.setApplicationIcon(unsafe.Pointer(&icon[0]), C.int(len(icon))) } */ + +func (a *App) logPlatformInfo() {} diff --git a/v3/pkg/application/application_production.go b/v3/pkg/application/application_production.go index 74b45156a..4ebf39688 100644 --- a/v3/pkg/application/application_production.go +++ b/v3/pkg/application/application_production.go @@ -10,3 +10,5 @@ func newApplication(options *Options) *App { result.init() return result } + +func (a *App) logStartup() {} diff --git a/v3/pkg/application/application_windows.go b/v3/pkg/application/application_windows.go index 92891785f..f98d61e30 100644 --- a/v3/pkg/application/application_windows.go +++ b/v3/pkg/application/application_windows.go @@ -173,9 +173,6 @@ func (m *windowsApp) setApplicationMenu(menu *Menu) { } func (m *windowsApp) run() error { - if webviewloader.UsingGoWebview2Loader { - globalApplication.info("Using Go Webview2Loader") - } for eventID := range m.parent.applicationEventListeners { m.on(eventID) } @@ -319,3 +316,14 @@ func newPlatformApp(app *App) *windowsApp { return result } + +func (a *App) logPlatformInfo() { + var args []any + args = append(args, "Go-WebView2Loader", webviewloader.UsingGoWebview2Loader) + windowsVersion, err := w32.GetWindowsVersionInfo() + if err == nil { + args = append(args, "Version", windowsVersion) + } + args = append(args, "Branding", w32.GetBranding()) + a.info("Windows Info:", args...) +} diff --git a/v3/pkg/w32/consts.go b/v3/pkg/w32/consts.go index b9afe2794..b8cdf0358 100644 --- a/v3/pkg/w32/consts.go +++ b/v3/pkg/w32/consts.go @@ -3,9 +3,12 @@ package w32 import ( + "fmt" + "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" "strconv" "syscall" + "unsafe" ) var ( @@ -20,8 +23,12 @@ var ( kernelGlobalUnlock = kernel32.NewProc("GlobalUnlock") kernelLstrcpy = kernel32.NewProc("lstrcpyW") ) +var ( + modBranding = syscall.NewLazyDLL("winbrand.dll") + brandingFormatString = modBranding.NewProc("BrandingFormatString") +) -var windowsVersion, _ = getWindowsVersionInfo() +var windowsVersion, _ = GetWindowsVersionInfo() func IsWindowsVersionAtLeast(major, minor, buildNumber int) bool { return windowsVersion.Major >= major && @@ -36,11 +43,23 @@ type WindowsVersionInfo struct { DisplayVersion string } +func (w *WindowsVersionInfo) String() string { + return fmt.Sprintf("%d.%d.%d (%s)", w.Major, w.Minor, w.Build, w.DisplayVersion) +} + func (w *WindowsVersionInfo) IsWindowsVersionAtLeast(major, minor, buildNumber int) bool { return w.Major >= major && w.Minor >= minor && w.Build >= buildNumber } -func getWindowsVersionInfo() (*WindowsVersionInfo, error) { +func GetBranding() string { + windowsLong := MustStringToUTF16Ptr("%WINDOWS_LONG%\x00") + ret, _, _ := brandingFormatString.Call( + uintptr(unsafe.Pointer(windowsLong)), + ) + return windows.UTF16PtrToString((*uint16)(unsafe.Pointer(ret))) +} + +func GetWindowsVersionInfo() (*WindowsVersionInfo, error) { key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) if err != nil { return nil, err