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

View file

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

View file

@ -4,8 +4,9 @@ package log
import (
"fmt"
"github.com/Safing/portbase/taskmanager"
"time"
"github.com/Safing/portbase/taskmanager"
)
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() {
var line *logLine
startedTask := false
shutdownWaitGroup.Add(1)
defer shutdownWaitGroup.Done()
for {