From d6295176d9bd126f07b054d539f39c422779c70c Mon Sep 17 00:00:00 2001 From: Jacob McSwain Date: Fri, 24 Feb 2023 14:17:25 -0600 Subject: [PATCH] logger: per-log load --- internal/logging/log.go | 53 +++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/internal/logging/log.go b/internal/logging/log.go index 85be8737a..2ff1e88f9 100644 --- a/internal/logging/log.go +++ b/internal/logging/log.go @@ -21,46 +21,43 @@ const ( ) var ( - accessLog *Logger //nolint:golint,gochecknoglobals - errorLog *Logger //nolint:golint,gochecknoglobals - isInit atomic.Bool - loaded atomic.Bool + accessLog *Logger //nolint:golint,gochecknoglobals + errorLog *Logger //nolint:golint,gochecknoglobals + isAccessInit atomic.Bool //nolint:golint,gochecknoglobals + accessLoaded atomic.Bool //nolint:golint,gochecknoglobals + isErrorInit atomic.Bool //nolint:golint,gochecknoglobals + errorLoaded atomic.Bool //nolint:golint,gochecknoglobals ) -func getLogger(logType LogType) *Logger { +func GetLogger(logType LogType) *Logger { + const loadDelay = 100 * time.Nanosecond + switch logType { case Access: - if accessLog != nil { - return accessLog + lastInit := isAccessInit.Swap(true) + if !lastInit { + accessLog = createLogger(logType) + accessLoaded.Store(true) + } + for !accessLoaded.Load() { + time.Sleep(loadDelay) } - // Create access logger - return createLogger(logType) + return accessLog case Error: - if errorLog != nil { - return errorLog + lastInit := isErrorInit.Swap(true) + if !lastInit { + errorLog = createLogger(logType) + errorLoaded.Store(true) } - // Create error logger - return createLogger(logType) + for !errorLoaded.Load() { + time.Sleep(loadDelay) + } + return errorLog default: panic("Logging failed") } } -func GetLogger(logType LogType) *Logger { - lastInit := isInit.Swap(true) - if !lastInit { - logger := getLogger(logType) - loaded.Store(true) - return logger - } - for !loaded.Load() { - const loadDelay = 100 * time.Nanosecond - time.Sleep(loadDelay) - } - - return getLogger(logType) -} - func createLogger(logType LogType) *Logger { var logFile *os.File switch runtime.GOOS {