Files
go-worm/lib/crashlog/crashlog.go
T
kato 36cb847f9e feat(agent): add crash logs and watchdog startup
- Recover panics to disk for diagnostics
- Add 5-minute watchdog task with startup
- Add -ensure flag to start detached agent
- Add script to pull remote folder via API
2026-09-01 20:04:20 +03:00

59 lines
1.2 KiB
Go

package crashlog
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"sync"
"time"
"tea.chunkbyte.com/kato/go-worm/lib/config"
)
var mu sync.Mutex
// Recover logs a panic and re-raises after writing the crash file.
func Recover() {
if r := recover(); r != nil {
_ = write("panic", fmt.Sprint(r), debug.Stack())
panic(r)
}
}
// RecordPanic writes a recovered panic without exiting (e.g. HTTP handlers).
func RecordPanic(recovered any) {
_ = write("panic", fmt.Sprint(recovered), debug.Stack())
}
// RecordError writes a fatal error before exit.
func RecordError(err error) {
if err == nil {
return
}
_ = write("fatal", err.Error(), debug.Stack())
}
func write(kind, detail string, stack []byte) error {
dir, err := config.LogsDir()
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
name := fmt.Sprintf("crash-%s.log", time.Now().Format("2006-01-02-150405"))
path := filepath.Join(dir, name)
body := fmt.Sprintf(
"time: %s\nversion: %s\nkind: %s\ndetail: %s\n\nstack:\n%s",
time.Now().Format(time.RFC3339Nano),
config.Version,
kind,
detail,
stack,
)
return os.WriteFile(path, []byte(body), 0o600)
}