Fix race condition in log

This commit is contained in:
Daniel 2018-10-23 11:57:36 +02:00
parent 3d2d636516
commit c399d7c748
3 changed files with 32 additions and 21 deletions

View file

@ -10,7 +10,7 @@ import (
"time" "time"
) )
func log_fastcheck(level severity) bool { func fastcheck(level severity) bool {
if fileLevelsActive.IsSet() { if fileLevelsActive.IsSet() {
return true return true
} }
@ -23,9 +23,9 @@ func log_fastcheck(level severity) bool {
func log(level severity, msg string) { func log(level severity, msg string) {
if !started.IsSet() { if !started.IsSet() {
// resouce intense, but keeps logs before logging started. // a bit resouce intense, but keeps logs before logging started.
go func(){ go func(){
time.Sleep(1 * time.Second) <-startedSignal
log(level, msg) log(level, msg)
}() }()
return return
@ -92,73 +92,73 @@ func log(level severity, msg string) {
} }
func Tracef(things ...interface{}) { func Tracef(things ...interface{}) {
if log_fastcheck(TraceLevel) { if fastcheck(TraceLevel) {
log(TraceLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(TraceLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Trace(msg string) { func Trace(msg string) {
if log_fastcheck(TraceLevel) { if fastcheck(TraceLevel) {
log(TraceLevel, msg) log(TraceLevel, msg)
} }
} }
func Debugf(things ...interface{}) { func Debugf(things ...interface{}) {
if log_fastcheck(DebugLevel) { if fastcheck(DebugLevel) {
log(DebugLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(DebugLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Debug(msg string) { func Debug(msg string) {
if log_fastcheck(DebugLevel) { if fastcheck(DebugLevel) {
log(DebugLevel, msg) log(DebugLevel, msg)
} }
} }
func Infof(things ...interface{}) { func Infof(things ...interface{}) {
if log_fastcheck(InfoLevel) { if fastcheck(InfoLevel) {
log(InfoLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(InfoLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Info(msg string) { func Info(msg string) {
if log_fastcheck(InfoLevel) { if fastcheck(InfoLevel) {
log(InfoLevel, msg) log(InfoLevel, msg)
} }
} }
func Warningf(things ...interface{}) { func Warningf(things ...interface{}) {
if log_fastcheck(WarningLevel) { if fastcheck(WarningLevel) {
log(WarningLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(WarningLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Warning(msg string) { func Warning(msg string) {
if log_fastcheck(WarningLevel) { if fastcheck(WarningLevel) {
log(WarningLevel, msg) log(WarningLevel, msg)
} }
} }
func Errorf(things ...interface{}) { func Errorf(things ...interface{}) {
if log_fastcheck(ErrorLevel) { if fastcheck(ErrorLevel) {
log(ErrorLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(ErrorLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Error(msg string) { func Error(msg string) {
if log_fastcheck(ErrorLevel) { if fastcheck(ErrorLevel) {
log(ErrorLevel, msg) log(ErrorLevel, msg)
} }
} }
func Criticalf(things ...interface{}) { func Criticalf(things ...interface{}) {
if log_fastcheck(CriticalLevel) { if fastcheck(CriticalLevel) {
log(CriticalLevel, fmt.Sprintf(things[0].(string), things[1:]...)) log(CriticalLevel, fmt.Sprintf(things[0].(string), things[1:]...))
} }
} }
func Critical(msg string) { func Critical(msg string) {
if log_fastcheck(CriticalLevel) { if fastcheck(CriticalLevel) {
log(CriticalLevel, msg) log(CriticalLevel, msg)
} }
} }

View file

@ -67,7 +67,10 @@ var (
shutdownSignal = make(chan struct{}, 0) shutdownSignal = make(chan struct{}, 0)
shutdownWaitGroup sync.WaitGroup shutdownWaitGroup sync.WaitGroup
started = abool.NewBool(false) initializing = abool.NewBool(false)
started = abool.NewBool(false)
startedSignal = make(chan struct{}, 0)
testErrors = abool.NewBool(false) testErrors = abool.NewBool(false)
) )
@ -106,7 +109,7 @@ func ParseLevel(level string) severity {
func Start() error { func Start() error {
if !started.SetToIf(false, true) { if !initializing.SetToIf(false, true) {
return nil return nil
} }
@ -138,8 +141,10 @@ func Start() error {
SetFileLevels(newFileLevels) SetFileLevels(newFileLevels)
} }
fmt.Println(fmt.Sprintf("%s%s ▶ BOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), endColor())) startWriter()
go writer()
started.Set()
close(startedSignal)
return nil return nil
} }

View file

@ -4,8 +4,9 @@ package log
import ( import (
"fmt" "fmt"
"github.com/Safing/portbase/taskmanager"
"time" "time"
"github.com/Safing/portbase/taskmanager"
) )
func writeLine(line *logLine) { func writeLine(line *logLine) {
@ -17,10 +18,15 @@ func writeLine(line *logLine) {
} }
func startWriter() {
shutdownWaitGroup.Add(1)
fmt.Println(fmt.Sprintf("%s%s ▶ BOF%s", InfoLevel.color(), time.Now().Format("060102 15:04:05.000"), endColor()))
go writer()
}
func writer() { func writer() {
var line *logLine var line *logLine
startedTask := false startedTask := false
shutdownWaitGroup.Add(1)
defer shutdownWaitGroup.Done() defer shutdownWaitGroup.Done()
for { for {