From 74f3ce990f246e82d5cc80974cc8f8003212046a Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 21 Feb 2021 19:26:20 +1100 Subject: [PATCH] Support loglevel flag in dev mode --- v2/cmd/wails/internal/commands/dev/dev.go | 11 +++++++---- v2/internal/app/debug.go | 9 +++++---- v2/internal/app/dev.go | 21 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/v2/cmd/wails/internal/commands/dev/dev.go b/v2/cmd/wails/internal/commands/dev/dev.go index fdcd505f..7b7cd97d 100644 --- a/v2/cmd/wails/internal/commands/dev/dev.go +++ b/v2/cmd/wails/internal/commands/dev/dev.go @@ -57,6 +57,9 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { showWarnings := false command.BoolFlag("w", "Show warnings", &showWarnings) + loglevel := "" + command.StringFlag("loglevel", "Loglevel to use - Trace, Debug, Info, Warning, Error", &loglevel) + command.Action(func() error { // Create logger @@ -82,7 +85,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { // Do initial build logger.Println("Building application for development...") - newProcess, err := restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess) + newProcess, err := restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess, loglevel) if newProcess != nil { debugBinaryProcess = newProcess } @@ -132,7 +135,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { // Do a rebuild // Try and build the app - newBinaryProcess, err := restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess) + newBinaryProcess, err := restartApp(logger, "dev", ldflags, compilerCommand, debugBinaryProcess, loglevel) if err != nil { fmt.Printf("Error during build: %s", err.Error()) return @@ -221,7 +224,7 @@ exit: } } -func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, debugBinaryProcess *process.Process) (*process.Process, error) { +func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, debugBinaryProcess *process.Process, loglevel string) (*process.Process, error) { appBinary, err := buildApp(logger, outputType, ldflags, compilerCommand) println() @@ -245,7 +248,7 @@ func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, // TODO: Generate `backend.js` // Start up new binary - newProcess := process.NewProcess(logger, appBinary) + newProcess := process.NewProcess(logger, appBinary, "-loglevel", loglevel) err = newProcess.Start() if err != nil { // Remove binary diff --git a/v2/internal/app/debug.go b/v2/internal/app/debug.go index 22bc7cdc..42c4758d 100644 --- a/v2/internal/app/debug.go +++ b/v2/internal/app/debug.go @@ -4,8 +4,9 @@ package app import ( "flag" - "github.com/wailsapp/wails/v2/pkg/logger" "strings" + + "github.com/wailsapp/wails/v2/pkg/logger" ) // Init initialises the application for a debug environment @@ -19,10 +20,10 @@ func (a *App) Init() error { } // Set log levels - greeting := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error") + loglevel := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error") flag.Parse() - if len(*greeting) > 0 { - switch strings.ToLower(*greeting) { + if len(*loglevel) > 0 { + switch strings.ToLower(*loglevel) { case "trace": a.logger.SetLogLevel(logger.TRACE) case "info": diff --git a/v2/internal/app/dev.go b/v2/internal/app/dev.go index 82f4ee97..508a9425 100644 --- a/v2/internal/app/dev.go +++ b/v2/internal/app/dev.go @@ -4,11 +4,14 @@ package app import ( "context" + "flag" + "strings" "sync" "github.com/wailsapp/wails/v2/internal/bridge" "github.com/wailsapp/wails/v2/internal/menumanager" + clilogger "github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/internal/binding" @@ -66,7 +69,23 @@ func CreateApp(appoptions *options.App) (*App, error) { // Set up logger myLogger := logger.New(appoptions.Logger) - myLogger.SetLogLevel(appoptions.LogLevel) + + loglevel := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error") + flag.Parse() + if len(*loglevel) > 0 { + switch strings.ToLower(*loglevel) { + case "trace": + myLogger.SetLogLevel(clilogger.TRACE) + case "info": + myLogger.SetLogLevel(clilogger.INFO) + case "warning": + myLogger.SetLogLevel(clilogger.WARNING) + case "error": + myLogger.SetLogLevel(clilogger.ERROR) + default: + myLogger.SetLogLevel(appoptions.LogLevel) + } + } // Create the menu manager menuManager := menumanager.NewManager()