Skip to content

Commit

Permalink
console: logging to a log file is now optional; add -log flag to spec…
Browse files Browse the repository at this point in the history
…ify log output
  • Loading branch information
gucio321 committed May 31, 2021
1 parent 7a19a44 commit 7cd1b9f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
23 changes: 15 additions & 8 deletions hsapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions hsapp/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<flags>]\n\nFlags:\n", os.Args[0])
flag.PrintDefaults()
}

flag.Parse()

if *showHelp {
Expand Down
2 changes: 2 additions & 0 deletions hsconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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),
Expand Down
22 changes: 22 additions & 0 deletions hswindow/hsdialog/hspreferencesdialog/preferencesdialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions hswindow/hstoolwindow/hsconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7cd1b9f

Please sign in to comment.