Improved logging++

This commit is contained in:
Lea Anthony 2023-08-13 16:52:39 +10:00
commit 6cbead5bfe
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
7 changed files with 100 additions and 11 deletions

View file

@ -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()

View file

@ -268,3 +268,5 @@ func setIcon(icon []byte) {
}
C.setApplicationIcon(unsafe.Pointer(&icon[0]), C.int(len(icon)))
}
func (a *App) logPlatformInfo() {}

View file

@ -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...)
}

View file

@ -170,3 +170,5 @@ func setIcon(icon []byte) {
//C.setApplicationIcon(unsafe.Pointer(&icon[0]), C.int(len(icon)))
}
*/
func (a *App) logPlatformInfo() {}

View file

@ -10,3 +10,5 @@ func newApplication(options *Options) *App {
result.init()
return result
}
func (a *App) logStartup() {}

View file

@ -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...)
}

View file

@ -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