Skip to content

Commit

Permalink
feat: add KANATA_TRAY_CONFIG_DIR env var (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
rszyma authored Dec 6, 2024
1 parent 4358803 commit 5513c63
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
55 changes: 38 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5513c63

Please sign in to comment.