-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
65 lines (60 loc) · 1.67 KB
/
logging.go
File metadata and controls
65 lines (60 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime/debug"
)
// logPath returns the path to atlas.llm.log inside the data dir.
func logPath() (string, error) {
dir, err := atlasDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "atlas.llm.log"), nil
}
// setupLogging opens the TUI log file for append, routes the stdlib logger
// to it, and returns a close function plus the resolved path. Safe to call
// even if the file cannot be opened — logging is silently disabled in that
// case and the returned closer is a no-op.
func setupLogging() (string, func(), error) {
p, err := logPath()
if err != nil {
return "", func() {}, err
}
f, err := os.OpenFile(p, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return p, func() {}, err
}
log.SetOutput(f)
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Printf("=== atlas.llm session start ===")
return p, func() {
log.Printf("=== atlas.llm session end ===")
_ = f.Close()
}, nil
}
// logPanicln dumps panic info + stack trace to the log file. Intended for
// use inside a deferred recover() so the TUI alt-screen teardown doesn't
// eat the traceback.
func logPanicln(v any) {
log.Printf("PANIC: %v\n%s", v, debug.Stack())
fmt.Fprintf(os.Stderr, "atlas.llm crashed: %v\n", v)
if p, err := logPath(); err == nil {
fmt.Fprintf(os.Stderr, "full stack trace written to %s\n", p)
}
}
// clearLogs removes the persistent log file. Safe to call when the file
// doesn't exist yet.
func clearLogs() error {
p, err := logPath()
if err != nil {
return err
}
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return err
}
fmt.Printf("Removed %s\n", p)
return nil
}