From 7cd1b9f3569a4c5337d81aa086bb71a7b86b496f Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 31 May 2021 21:02:46 +0200 Subject: [PATCH] console: logging to a log file is now optional; add -log flag to specify log output --- hsapp/app.go | 23 ++++++++++++------- hsapp/flags.go | 3 +++ hsconfig/config.go | 2 ++ .../hspreferencesdialog/preferencesdialog.go | 22 ++++++++++++++++++ hswindow/hstoolwindow/hsconsole/console.go | 12 ++++++---- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/hsapp/app.go b/hsapp/app.go index 42b74770..b7b4bc59 100644 --- a/hsapp/app.go +++ b/hsapp/app.go @@ -157,17 +157,24 @@ func (a *App) Run() { defer a.Quit() // force-close and save everything (in case of crash) - a.logFile, err = os.OpenFile(a.config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, logFilePerms) - if err != nil { - log.Printf("Error opening log file at %s: %v", a.config.LogFilePath, err) - } + if a.config.LoggingToFile || *a.Flags.logFile != "" { + var path string = a.config.LogFilePath + if *a.Flags.logFile != "" { + path = *a.Flags.logFile + } - defer func() { - err := a.logFile.Close() + a.logFile, err = os.OpenFile(filepath.Clean(path), os.O_CREATE|os.O_APPEND|os.O_WRONLY, logFilePerms) if err != nil { - log.Fatal(err) + log.Printf("Error opening log file at %s: %v", a.config.LogFilePath, err) } - }() + + defer func() { + err := a.logFile.Close() + if err != nil { + log.Fatal(err) + } + }() + } if err := a.setup(); err != nil { log.Panic(err) diff --git a/hsapp/flags.go b/hsapp/flags.go index 05b0ca36..a8679453 100644 --- a/hsapp/flags.go +++ b/hsapp/flags.go @@ -12,18 +12,21 @@ import ( type Flags struct { optionalConfigPath *string bgColor *uint + logFile *string } func (a *App) parseArgs() { configDescr := fmt.Sprintf("specify a custom config path. Default is: %s", hsconfig.GetConfigPath()) a.Flags.optionalConfigPath = flag.String("config", "", configDescr) a.Flags.bgColor = flag.Uint("bgColor", hsconfig.DefaultBGColor, "custom background color") + a.Flags.logFile = flag.String("log", "", "path to the output log file") showHelp := flag.Bool("h", false, "Show help") flag.Usage = func() { fmt.Printf("usage: %s []\n\nFlags:\n", os.Args[0]) flag.PrintDefaults() } + flag.Parse() if *showHelp { diff --git a/hsconfig/config.go b/hsconfig/config.go index 45a6ae92..cb101963 100644 --- a/hsconfig/config.go +++ b/hsconfig/config.go @@ -39,6 +39,7 @@ type Config struct { ExternalListFile string `json:"externalListFile"` OpenMostRecentOnStartup bool `json:"openMostRecentOnStartup"` ProjectStates map[string]hsstate.AppState `json:"projectStates"` + LoggingToFile bool `json:"loggingToFile"` LogFilePath string `json:"logFile"` Locale hsenum.Locale `json:"locale"` BGColor color.RGBA `json:"bgColor"` @@ -60,6 +61,7 @@ func generateDefaultConfig(path string) *Config { RecentProjects: []string{}, OpenMostRecentOnStartup: true, ProjectStates: make(map[string]hsstate.AppState), + LoggingToFile: false, LogFilePath: filepath.Join(filepath.Dir(path), "output.txt"), Locale: hsenum.LocaleEnglish, BGColor: hsutil.Color(DefaultBGColor), diff --git a/hswindow/hsdialog/hspreferencesdialog/preferencesdialog.go b/hswindow/hsdialog/hspreferencesdialog/preferencesdialog.go index a41a62fc..e917a66a 100644 --- a/hswindow/hsdialog/hspreferencesdialog/preferencesdialog.go +++ b/hswindow/hsdialog/hspreferencesdialog/preferencesdialog.go @@ -73,6 +73,19 @@ func (p *PreferencesDialog) Build() { g.Separator(), g.Checkbox("Open most recent project on start-up", &p.config.OpenMostRecentOnStartup), g.Separator(), + g.Checkbox("Save log output in a log file", &p.config.LoggingToFile), + g.Custom(func() { + if p.config.LoggingToFile { + g.Layout{ + g.Label("Log file path"), + g.Line( + g.InputText("##AppPreferencesLogFilePath", &p.config.LogFilePath).Size(textboxSize).Flags(g.InputTextFlags_ReadOnly), + g.Button("...##AppPreferencesLogFilePathBrowse").Size(btnW, btnH).OnClick(p.onBrowseLogFilePathClicked), + ), + }.Build() + } + }), + g.Separator(), g.Custom(func() { if !p.restartPrompt { return @@ -135,6 +148,15 @@ func (p *PreferencesDialog) onBrowseExternalListfileClicked() { p.config.ExternalListFile = filePath } +func (p *PreferencesDialog) onBrowseLogFilePathClicked() { + path, err := dialog.Directory().Browse() + if err != nil || path == "" { + return + } + + p.config.LogFilePath = path +} + func (p *PreferencesDialog) onSaveClicked() { p.onConfigChanged(p.config) p.Visible = false diff --git a/hswindow/hstoolwindow/hsconsole/console.go b/hswindow/hstoolwindow/hsconsole/console.go index 514f62f3..044a1a0b 100644 --- a/hswindow/hstoolwindow/hsconsole/console.go +++ b/hswindow/hstoolwindow/hsconsole/console.go @@ -64,11 +64,13 @@ func (c *Console) Write(p []byte) (n int, err error) { fmt.Print(msg) // print to terminal - n, err = c.logFile.Write(p) // print to file - if err != nil { - return n, fmt.Errorf("error writing to log file: %w", err) - } else if n != len(p) { - return n, fmt.Errorf("invalid data written to log file") + if c.logFile != nil { + n, err = c.logFile.Write(p) // print to file + if err != nil { + return n, fmt.Errorf("error writing to log file: %w", err) + } else if n != len(p) { + return n, fmt.Errorf("invalid data written to log file") + } } return len(p), nil