diff --git a/README.md b/README.md index f54f6ec..329fe46 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,12 @@ Minimal supported version of kanata is `v1.6.0`. More specifically, builds after commit [010338b](https://github.com/jtroo/kanata/commit/010338b14d0020098b9263a615ef2152c249d666) (because it fixed an issue with TCP server) +## Troubleshooting + +Log file - By default kanata-tray will try to write a log file named `kanata_tray_lastrun.log` in the same directory as itself. If it causes problems e.g. because of the location is read-only, the log directory can be changed by setting new path in `KANATA_TRAY_LOG_DIR` environment variable. + +Debug logs - more verbose kanata-tray output, debug logging can be enabled with `--log-level=1` flag. You can use it to see loaded config struct or raw tcp messages from kanata. + ## Linux Dependencies For Linux, make sure to install required packages first: diff --git a/main.go b/main.go index 093d4e9..150d705 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,11 @@ var ( version = flag.Bool("version", false, "Print the version and exit") ) +const ( + configFileName = "kanata-tray.toml" + logFilename = "kanata_tray_lastrun.log" +) + func main() { flag.Parse() if *version { @@ -46,13 +51,11 @@ func main() { } } -const configFileName = "kanata-tray.toml" - func figureOutConfigDir() (configFolder string) { if v := os.Getenv("KANATA_TRAY_CONFIG_DIR"); v != "" { return v } - exePath, err := os.Executable() + exePath, err := exePath() if err != nil { log.Errorf("Failed to get kanata-tray executable path, can't check if kanata-tray.toml is there. Error: %v", err) } else { @@ -64,20 +67,44 @@ func figureOutConfigDir() (configFolder string) { return configdir.LocalConfig("kanata-tray") } -func mainImpl() error { - log.SetLevel(log.Lvl(*logLevel)) - +func exePath() (string, error) { exePath, err := os.Executable() if err != nil { - return fmt.Errorf("failed to determine kanata-tray path: %v", err) + return "", fmt.Errorf("os.Executable: %v", err) } exePath, err = filepath.EvalSymlinks(exePath) if err != nil { - return fmt.Errorf("failed to eval symlinks: %v", err) + return "", fmt.Errorf("filepath.EvalSymlinks: %v", err) } - exeDir := filepath.Dir(exePath) - const logFilename string = "kanata_tray_lastrun.log" - logFilepath := filepath.Join(exeDir, logFilename) + return exePath, nil +} + +func mainImpl() error { + log.SetLevel(log.Lvl(*logLevel)) + + if int(*logLevel) <= int(log.DEBUG) { + log.SetHeader(`${time_rfc3339_nano} ${level} ${short_file}:${line}`) + } else { + log.SetHeader(`${time_rfc3339_nano} ${level}`) + } + + var logDir string + if v := os.Getenv("KANATA_TRAY_LOG_DIR"); v != "" { + var err error + logDir, err = filepath.EvalSymlinks(v) + if err != nil { + return fmt.Errorf("filepath.EvalSymlinks on KANATA_TRAY_LOG_DIR failed: %v", err) + } + } else { + exePath, err := exePath() + if err != nil { + return fmt.Errorf("failed to determine kanata-tray executable path: %v", err) + } + exeDirPath := filepath.Dir(exePath) + logDir = exeDirPath + } + + logFilepath := filepath.Join(logDir, logFilename) logFile, err := os.Create(logFilepath) if err != nil { return fmt.Errorf("failed to create %s file: %v", logFilename, err) @@ -91,12 +118,6 @@ func mainImpl() error { 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)