From 8edf7b0e4d386e5b4b4f052c88a781e40b1f1d30 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 13 Feb 2024 22:44:25 +0100 Subject: log: move package to lib This has nothing to do at the root of the source tree. Signed-off-by: Robin Jarry Acked-by: Bence Ferdinandy --- lib/log/logger.go | 152 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/log/panic-logger.go | 60 +++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 lib/log/logger.go create mode 100644 lib/log/panic-logger.go (limited to 'lib/log') diff --git a/lib/log/logger.go b/lib/log/logger.go new file mode 100644 index 00000000..2ece5f32 --- /dev/null +++ b/lib/log/logger.go @@ -0,0 +1,152 @@ +package log + +import ( + "fmt" + "io" + "log" + "os" + "strings" +) + +type LogLevel int + +const ( + TRACE LogLevel = 5 + DEBUG LogLevel = 10 + INFO LogLevel = 20 + WARN LogLevel = 30 + ERROR LogLevel = 40 +) + +var ( + trace *log.Logger + dbg *log.Logger + info *log.Logger + warn *log.Logger + err *log.Logger + minLevel LogLevel = TRACE +) + +func Init(file *os.File, level LogLevel) { + minLevel = level + flags := log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile + if file != nil { + trace = log.New(file, "TRACE ", flags) + dbg = log.New(file, "DEBUG ", flags) + info = log.New(file, "INFO ", flags) + warn = log.New(file, "WARN ", flags) + err = log.New(file, "ERROR ", flags) + } +} + +func ParseLevel(value string) (LogLevel, error) { + switch strings.ToLower(value) { + case "trace": + return TRACE, nil + case "debug": + return DEBUG, nil + case "info": + return INFO, nil + case "warn", "warning": + return WARN, nil + case "err", "error": + return ERROR, nil + } + return 0, fmt.Errorf("%s: invalid log level", value) +} + +func ErrorLogger() *log.Logger { + if err == nil { + return log.New(io.Discard, "", log.LstdFlags) + } + return err +} + +type Logger interface { + Tracef(string, ...interface{}) + Debugf(string, ...interface{}) + Infof(string, ...interface{}) + Warnf(string, ...interface{}) + Errorf(string, ...interface{}) +} + +type logger struct { + name string + calldepth int +} + +func NewLogger(name string, calldepth int) Logger { + return &logger{name: name, calldepth: calldepth} +} + +func (l *logger) format(message string, args ...interface{}) string { + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + if l.name != "" { + message = fmt.Sprintf("[%s] %s", l.name, message) + } + return message +} + +func (l *logger) Tracef(message string, args ...interface{}) { + if trace == nil || minLevel > TRACE { + return + } + message = l.format(message, args...) + trace.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +func (l *logger) Debugf(message string, args ...interface{}) { + if dbg == nil || minLevel > DEBUG { + return + } + message = l.format(message, args...) + dbg.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +func (l *logger) Infof(message string, args ...interface{}) { + if info == nil || minLevel > INFO { + return + } + message = l.format(message, args...) + info.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +func (l *logger) Warnf(message string, args ...interface{}) { + if warn == nil || minLevel > WARN { + return + } + message = l.format(message, args...) + warn.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +func (l *logger) Errorf(message string, args ...interface{}) { + if err == nil || minLevel > ERROR { + return + } + message = l.format(message, args...) + err.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +var root = logger{calldepth: 3} + +func Tracef(message string, args ...interface{}) { + root.Tracef(message, args...) +} + +func Debugf(message string, args ...interface{}) { + root.Debugf(message, args...) +} + +func Infof(message string, args ...interface{}) { + root.Infof(message, args...) +} + +func Warnf(message string, args ...interface{}) { + root.Warnf(message, args...) +} + +func Errorf(message string, args ...interface{}) { + root.Errorf(message, args...) +} diff --git a/lib/log/panic-logger.go b/lib/log/panic-logger.go new file mode 100644 index 00000000..a1904415 --- /dev/null +++ b/lib/log/panic-logger.go @@ -0,0 +1,60 @@ +package log + +import ( + "fmt" + "io" + "os" + "runtime/debug" + "strings" + "time" +) + +var ( + UICleanup = func() {} + BuildInfo string +) + +// PanicHandler tries to restore the terminal. A stack trace is written to +// aerc-crash.log and then passed on if a panic occurs. +func PanicHandler() { + r := recover() + + if r == nil { + return + } + + UICleanup() + + filename := time.Now().Format("/tmp/aerc-crash-20060102-150405.log") + + panicLog, err := os.OpenFile(filename, os.O_SYNC|os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0o600) + if err != nil { + // we tried, not possible. bye + panic(r) + } + defer panicLog.Close() + + outputs := io.MultiWriter(panicLog, os.Stderr) + + // if any error happens here, we do not care. + fmt.Fprintln(panicLog, strings.Repeat("#", 80)) + fmt.Fprint(panicLog, strings.Repeat(" ", 34)) + fmt.Fprintln(panicLog, "PANIC CAUGHT!") + fmt.Fprint(panicLog, strings.Repeat(" ", 24)) + fmt.Fprintln(panicLog, time.Now().Format("2006-01-02T15:04:05.000000-0700")) + fmt.Fprintln(panicLog, strings.Repeat("#", 80)) + fmt.Fprintf(outputs, "%s\n", panicMessage) + fmt.Fprintf(outputs, "Version: %s\n", BuildInfo) + fmt.Fprintf(panicLog, "Error: %v\n\n", r) + panicLog.Write(debug.Stack()) //nolint:errcheck // we are already in a panic, so not much we can do here + fmt.Fprintf(os.Stderr, "\nThis error was also written to: %s\n", filename) + panic(r) +} + +const panicMessage = ` +aerc has encountered a critical error and has terminated. Please help us fix +this by sending this log and the steps to reproduce the crash to: +~rjarry/aerc-devel@lists.sr.ht + +Thank you +` -- cgit