Former/debug_on.go

50 lines
1.2 KiB
Go

//go:build debug
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
)
var debugLogger *log.Logger
func init() {
home, _ := os.UserHomeDir()
logDir := filepath.Join(home, "former")
os.MkdirAll(logDir, 0755)
logPath := filepath.Join(logDir, "debug.log")
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
// Fall back to stderr
debugLogger = log.New(os.Stderr, "[DEBUG] ", log.Ltime|log.Lshortfile)
debugLogger.Printf("Could not open %s: %v — logging to stderr", logPath, err)
return
}
debugLogger = log.New(f, "[DEBUG] ", log.Ltime|log.Lshortfile)
debugLogger.Printf("=== Former debug session started %s ===", time.Now().Format(time.RFC3339))
debugLogger.Printf("Log file: %s", logPath)
fmt.Fprintf(os.Stderr, "[Former] Debug logging to %s\n", logPath)
}
func debugLog(format string, args ...interface{}) {
if debugLogger != nil {
debugLogger.Printf(format, args...)
}
}
// JSDebugLog is called from the frontend to pipe browser console messages into the debug log.
func (a *App) JSDebugLog(msg string) {
debugLog("[JS] %s", msg)
}
// IsDebugMode returns true when the app was built with the debug tag.
func (a *App) IsDebugMode() bool {
return true
}