diff options
Diffstat (limited to 'log')
-rw-r--r-- | log/logger.go | 113 | ||||
-rw-r--r-- | log/panic-logger.go | 60 |
2 files changed, 173 insertions, 0 deletions
diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 00000000..eb52d7b7 --- /dev/null +++ b/log/logger.go @@ -0,0 +1,113 @@ +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 +} + +func Tracef(message string, args ...interface{}) { + if trace == nil || minLevel > TRACE { + return + } + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + trace.Output(2, message) //nolint:errcheck // we can't do anything with what we log +} + +func Debugf(message string, args ...interface{}) { + if dbg == nil || minLevel > DEBUG { + return + } + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + dbg.Output(2, message) //nolint:errcheck // we can't do anything with what we log +} + +func Infof(message string, args ...interface{}) { + if info == nil || minLevel > INFO { + return + } + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + info.Output(2, message) //nolint:errcheck // we can't do anything with what we log +} + +func Warnf(message string, args ...interface{}) { + if warn == nil || minLevel > WARN { + return + } + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + warn.Output(2, message) //nolint:errcheck // we can't do anything with what we log +} + +func Errorf(message string, args ...interface{}) { + if err == nil || minLevel > ERROR { + return + } + if len(args) > 0 { + message = fmt.Sprintf(message, args...) + } + err.Output(2, message) //nolint:errcheck // we can't do anything with what we log +} diff --git a/log/panic-logger.go b/log/panic-logger.go new file mode 100644 index 00000000..a1904415 --- /dev/null +++ b/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 +` |