Skip to content

Commit

Permalink
Debug Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne authored and oz committed Sep 3, 2024
1 parent 645336b commit 88dec44
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func LoadConfig(tzConfigs []string) (Config, error) {
mergedConfig.Zones = fileConfig.Zones
}

logger.Printf("File config: %s", fileConfig.Zones)
logger.Printf("Env config: %s", envConfig.Zones)
logger.Printf("Merged config: %s", mergedConfig.Zones)

// Merge Keymaps
if len(fileConfig.Keymaps.PrevHour) > 0 {
mergedConfig.Keymaps.PrevHour = fileConfig.Keymaps.PrevHour
Expand Down
3 changes: 3 additions & 0 deletions config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -72,13 +73,15 @@ func LoadConfigFile() (*Config, error) {
configFile, err := os.ReadFile(configFilePath)
if err != nil {
// Silently return
logger.Println("Config file '~/.config/tz/conf.toml' not found. Skipping...")
return &conf, nil
}

// Unmarshal the TOML data into the Config struct
var config ConfigFile
err = toml.Unmarshal(configFile, &config)
if err != nil {
log.Panicf("Error parsing config file %s \n %v", configFilePath, err)
panic(err)
}

Expand Down
29 changes: 29 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"log"
"os"

tea "github.com/charmbracelet/bubbletea"
)

var logger *log.Logger

type NoOpWriter struct{}

func (w *NoOpWriter) Write(p []byte) (int, error) {
return len(p), nil
}

func SetupLogger() {

if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
log.Fatalf("failed to open log file: %v", err)
}
logger = log.New(f, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
} else {
logger = log.New(&NoOpWriter{}, "", 0)
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func main() {
SetupLogger()
logger.Println("Startup")

exitQuick := flag.Bool("q", false, "exit immediately")
showVersion := flag.Bool("v", false, "show version")
when := flag.Int64("when", 0, "time in seconds since unix epoch")
Expand Down

0 comments on commit 88dec44

Please sign in to comment.