Add startup logging and Discord ready timeout

Adds log lines at each startup step so the log file is never empty on
failure. Adds a 30s timeout on the Discord ready event so a bad token or
blocked network produces a clear log message instead of a silent hang.
Falls back to %TEMP% for the log file if the exe directory isn't writable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 19:31:21 +03:00
parent 75c113909d
commit 6c56192141
+28 -7
View File
@@ -8,6 +8,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"fyne.io/systray" "fyne.io/systray"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@@ -120,19 +121,34 @@ func toggleMute(dg *discordgo.Session, cfg *Config, muted bool) {
} }
} }
func run() { func setupLogging() {
// Log to file next to the executable so errors are visible even without a console. // Try exe directory first, fall back to temp dir (e.g. if installed in Program Files).
var dirs []string
if exe, err := os.Executable(); err == nil { if exe, err := os.Executable(); err == nil {
logPath := filepath.Join(filepath.Dir(exe), "discord-lobby-mute.log") dirs = append(dirs, filepath.Dir(exe))
if f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err == nil { }
dirs = append(dirs, os.TempDir())
for _, dir := range dirs {
logPath := filepath.Join(dir, "discord-lobby-mute.log")
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
continue
}
log.SetOutput(io.MultiWriter(os.Stderr, f)) log.SetOutput(io.MultiWriter(os.Stderr, f))
return
} }
} }
func run() {
setupLogging()
log.Println("starting")
cfg, err := loadConfig() cfg, err := loadConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println("config loaded")
dg, err := discordgo.New("Bot " + cfg.Token) dg, err := discordgo.New("Bot " + cfg.Token)
if err != nil { if err != nil {
@@ -145,13 +161,18 @@ func run() {
close(ready) close(ready)
}) })
log.Println("connecting to Discord...")
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
log.Fatalf("connect to Discord: %v", err) log.Fatalf("connect to Discord: %v", err)
} }
defer dg.Close() defer dg.Close()
<-ready select {
log.Println("Connected to Discord.") case <-ready:
case <-time.After(30 * time.Second):
log.Fatal("timed out waiting for Discord ready — check token and network connection")
}
log.Println("connected to Discord")
keydownCh, err := startHotkey(cfg.Keybind) keydownCh, err := startHotkey(cfg.Keybind)
if err != nil { if err != nil {