40 lines
956 B
Go
40 lines
956 B
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...)
|
|
}
|
|
}
|