Skip to content

Commit

Permalink
logger: per-log load
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 25, 2023
1 parent 92153e2 commit d629517
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions internal/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d629517

Please sign in to comment.