Skip to content

Commit

Permalink
feat: add "Show logs" button
Browse files Browse the repository at this point in the history
Previously, with Windows binaries it wasn't possible to see kanata-tray
logs, unless recompiled without -H=windowsgui ldflag. Now, we also
output logs to a file, so it's not a problem anymore.

closes #20
  • Loading branch information
rszyma committed Nov 6, 2024
1 parent 4d74aa6 commit a5448e6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ icons
.direnv
kanata-tray
.envrc
kanata_tray_lastrun.log
16 changes: 11 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

type SystrayApp struct {
logFilepath string

concurrentPresets bool

// Used when `concurrentPresets` is disabled.
Expand All @@ -37,16 +39,18 @@ type SystrayApp struct {
mPresetLogs []*systray.MenuItem
mPresetStatuses []*systray.MenuItem

mOptions *systray.MenuItem
mQuit *systray.MenuItem
mOptions *systray.MenuItem
mShowLogs *systray.MenuItem
mQuit *systray.MenuItem
}

func NewSystrayApp(menuTemplate []PresetMenuEntry, layerIcons LayerIcons, allowConcurrentPresets bool) *SystrayApp {
func NewSystrayApp(menuTemplate []PresetMenuEntry, layerIcons LayerIcons, allowConcurrentPresets bool, logFilepath string) *SystrayApp {

systray.SetIcon(icons.Default)
systray.SetTooltip("kanata-tray")

t := &SystrayApp{
logFilepath: logFilepath,
presets: menuTemplate,
scheduledPresetIndex: -1,
layerIcons: layerIcons,
Expand All @@ -66,14 +70,14 @@ func NewSystrayApp(menuTemplate []PresetMenuEntry, layerIcons LayerIcons, allowC

t.presetCancelFuncs = append(t.presetCancelFuncs, nil)

openLogsItem := menuItem.AddSubMenuItem("Open Logs", "Open Logs")
// openLogsItem.Disable()
openLogsItem := menuItem.AddSubMenuItem("Open kanata logs", "Open kanata log file")
t.mPresetLogs = append(t.mPresetLogs, openLogsItem)
}

systray.AddSeparator()

t.mOptions = systray.AddMenuItem("Configure", "Reveals kanata-tray config file")
t.mShowLogs = systray.AddMenuItem("Open logs", "Reveals kanata-tray log file")
t.mQuit = systray.AddMenuItem("Exit tray", "Closes kanata (if running) and exits the tray")

t.statusClickedCh = multipleMenuItemsClickListener(t.mPresetStatuses)
Expand Down Expand Up @@ -220,6 +224,8 @@ func (app *SystrayApp) StartProcessingLoop(runner *runner_pkg.Runner, configFold
}
case <-app.mOptions.ClickedCh:
open.Start(configFolder)
case <-app.mShowLogs.ClickedCh:
open.Start(app.logFilepath)
case <-app.mQuit.ClickedCh:
log.Print("Exiting...")
for _, cancel := range app.presetCancelFuncs {
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func ReadConfigOrCreateIfNotExist(configFilePath string) (*Config, error) {
cfg2.Presets.Set(layerName, exported)
}

pretty.Println("loaded config:", cfg2)
log.Debugf("loaded config: %s", pretty.Sprint(cfg2))
return cfg2, nil
}

Expand Down
45 changes: 35 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -38,14 +39,6 @@ func main() {
os.Exit(1)
}

log.SetLevel(log.Lvl(*logLevel))
log.SetOutput(os.Stderr)
if int(*logLevel) <= int(log.DEBUG) {
log.SetHeader(`${time_rfc3339_nano} ${level} ${short_file}:${line}`)
} else {
log.SetHeader(`${time_rfc3339_nano} ${level}`)
}

err := mainImpl()
if err != nil {
log.Errorf("kanata-tray exited with an error: %v", err)
Expand All @@ -72,11 +65,43 @@ func figureOutConfigDir() (configFolder string) {
}

func mainImpl() error {
log.SetLevel(log.Lvl(*logLevel))

exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to determine kanata-tray path: %v", err)
}
exePath, err = filepath.EvalSymlinks(exePath)
if err != nil {
return fmt.Errorf("failed to eval symlinks: %v", err)
}
exeDir := filepath.Dir(exePath)
const logFilename string = "kanata_tray_lastrun.log"
logFilepath := filepath.Join(exeDir, logFilename)
logFile, err := os.Create(logFilepath)
if err != nil {
return fmt.Errorf("failed to create %s file: %v", logFilename, err)
}
// Check if stderr is available. Specifically it won't be, when running
// Windows binary compiled with -H=windowsgui ldflag.
_, err = os.Stderr.Stat()
if err != nil {
log.SetOutput(logFile)
} else {
log.SetOutput(io.MultiWriter(logFile, os.Stderr))
}

if int(*logLevel) <= int(log.DEBUG) {
log.SetHeader(`${time_rfc3339_nano} ${level} ${short_file}:${line}`)
} else {
log.SetHeader(`${time_rfc3339_nano} ${level}`)
}

configFolder := figureOutConfigDir()

log.Infof("kanata-tray config folder: %s", configFolder)

err := os.MkdirAll(filepath.Join(configFolder, "icons"), os.ModePerm)
err = os.MkdirAll(filepath.Join(configFolder, "icons"), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create folder: %v", err)
}
Expand All @@ -94,7 +119,7 @@ func mainImpl() error {
runner := runner.NewRunner()

onReady := func() {
app := app.NewSystrayApp(menuTemplate, layerIcons, cfg.General.AllowConcurrentPresets)
app := app.NewSystrayApp(menuTemplate, layerIcons, cfg.General.AllowConcurrentPresets, logFilepath)
go app.StartProcessingLoop(runner, configFolder)
}

Expand Down

0 comments on commit a5448e6

Please sign in to comment.