diff options
author | Koni Marti <koni.marti@gmail.com> | 2024-08-07 20:52:31 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-08-20 12:26:47 +0200 |
commit | 9d4a61503c5eddad814cd3cd825a586d612f15e6 (patch) | |
tree | 330e275bf289f442bb1f2707dd96db3208dfe999 /lib/log/logger.go | |
parent | 2a83d3e9c872f38b8dd7b7a214310d87adefb25e (diff) | |
download | aerc-9d4a61503c5eddad814cd3cd825a586d612f15e6.tar.gz |
log: handle config reload
Prepare the logging system for a config file reload. Make sure that we
never close os.Stdout but only log file descriptors. Note that if you
started aerc by redirecting its output into a specific file, this cannot
be changed by a config reload.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Tested-by: Inwit <inwit@sindominio.net>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/log/logger.go')
-rw-r--r-- | lib/log/logger.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/lib/log/logger.go b/lib/log/logger.go index 2ece5f32..80c78d18 100644 --- a/lib/log/logger.go +++ b/lib/log/logger.go @@ -18,6 +18,22 @@ const ( ERROR LogLevel = 40 ) +type logfilePtr struct { + f *os.File + useStdout bool +} + +func newLogfilePtr(f *os.File, isStdout bool) *logfilePtr { + return &logfilePtr{f: f, useStdout: isStdout} +} + +func (l *logfilePtr) Close() error { + if l.useStdout || l.f == nil { + return nil + } + return l.f.Close() +} + var ( trace *log.Logger dbg *log.Logger @@ -25,18 +41,38 @@ var ( warn *log.Logger err *log.Logger minLevel LogLevel = TRACE + + // logfile stores a pointer to the log file descriptor + logfile *logfilePtr ) -func Init(file *os.File, level LogLevel) { +func Init(file *os.File, useStdout bool, level LogLevel) error { + trace = nil + dbg = nil + info = nil + warn = nil + err = nil + + if logfile != nil { + e := logfile.Close() + if e != nil { + return e + } + logfile = nil + } + minLevel = level flags := log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile if file != nil { + logfile = newLogfilePtr(file, useStdout) 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) } + + return nil } func ParseLevel(value string) (LogLevel, error) { |