Fixed crazy looping

This commit is contained in:
obscuren 2014-07-05 13:24:26 +02:00
parent 90eb4f1939
commit 329887df99

@ -39,7 +39,7 @@ func (msg *logMessage) send(logger LogSystem) {
var logMessages chan (*logMessage) var logMessages chan (*logMessage)
var logSystems []LogSystem var logSystems []LogSystem
var drained = true var quit chan bool
type LogLevel uint8 type LogLevel uint8
@ -54,6 +54,7 @@ const (
// log messages are dispatched to log writers // log messages are dispatched to log writers
func start() { func start() {
out:
for { for {
select { select {
case msg := <-logMessages: case msg := <-logMessages:
@ -62,15 +63,23 @@ func start() {
msg.send(logSystem) msg.send(logSystem)
} }
} }
default: case <-quit:
drained = true break out
} }
} }
} }
// waits until log messages are drained (dispatched to log writers) // waits until log messages are drained (dispatched to log writers)
func Flush() { func Flush() {
for !drained { quit <- true
done:
for {
select {
case <-logMessages:
default:
break done
}
} }
} }
@ -88,6 +97,7 @@ func AddLogSystem(logSystem LogSystem) {
defer mutex.Unlock() defer mutex.Unlock()
if logSystems == nil { if logSystems == nil {
logMessages = make(chan *logMessage) logMessages = make(chan *logMessage)
quit = make(chan bool)
go start() go start()
} }
logSystems = append(logSystems, logSystem) logSystems = append(logSystems, logSystem)
@ -96,7 +106,6 @@ func AddLogSystem(logSystem LogSystem) {
func (logger *Logger) sendln(level LogLevel, v ...interface{}) { func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
if logMessages != nil { if logMessages != nil {
msg := newPrintlnLogMessage(level, logger.tag, v...) msg := newPrintlnLogMessage(level, logger.tag, v...)
drained = false
logMessages <- msg logMessages <- msg
} }
} }
@ -104,7 +113,6 @@ func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) { func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
if logMessages != nil { if logMessages != nil {
msg := newPrintfLogMessage(level, logger.tag, format, v...) msg := newPrintfLogMessage(level, logger.tag, format, v...)
drained = false
logMessages <- msg logMessages <- msg
} }
} }