|
|
@ -39,9 +39,8 @@ func (msg *logMessage) send(logger LogSystem) { |
|
|
|
|
|
|
|
|
|
|
|
var logMessages chan (*logMessage) |
|
|
|
var logMessages chan (*logMessage) |
|
|
|
var logSystems []LogSystem |
|
|
|
var logSystems []LogSystem |
|
|
|
var quit chan bool |
|
|
|
var quit chan chan error |
|
|
|
var drained chan bool |
|
|
|
var drained chan bool |
|
|
|
var shutdown chan bool |
|
|
|
|
|
|
|
var mutex = sync.Mutex{} |
|
|
|
var mutex = sync.Mutex{} |
|
|
|
|
|
|
|
|
|
|
|
type LogLevel uint8 |
|
|
|
type LogLevel uint8 |
|
|
@ -55,44 +54,54 @@ const ( |
|
|
|
DebugDetailLevel |
|
|
|
DebugDetailLevel |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func dispatch(msg *logMessage) { |
|
|
|
|
|
|
|
for _, logSystem := range logSystems { |
|
|
|
|
|
|
|
if logSystem.GetLogLevel() >= msg.LogLevel { |
|
|
|
|
|
|
|
msg.send(logSystem) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// log messages are dispatched to log writers
|
|
|
|
// log messages are dispatched to log writers
|
|
|
|
func start() { |
|
|
|
func start() { |
|
|
|
out: |
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-quit: |
|
|
|
case status := <-quit: |
|
|
|
break out |
|
|
|
status <- nil |
|
|
|
|
|
|
|
return |
|
|
|
case msg := <-logMessages: |
|
|
|
case msg := <-logMessages: |
|
|
|
for _, logSystem := range logSystems { |
|
|
|
dispatch(msg) |
|
|
|
if logSystem.GetLogLevel() >= msg.LogLevel { |
|
|
|
default: |
|
|
|
msg.send(logSystem) |
|
|
|
drained <- true // this blocks until a message is sent to the queue
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
case drained <- true: |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func send(msg *logMessage) { |
|
|
|
|
|
|
|
logMessages <- msg |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-drained: |
|
|
|
default: |
|
|
|
default: |
|
|
|
drained <- true // this blocks until a message is sent to the queu
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
close(shutdown) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Reset() { |
|
|
|
func Reset() { |
|
|
|
mutex.Lock() |
|
|
|
mutex.Lock() |
|
|
|
defer mutex.Unlock() |
|
|
|
defer mutex.Unlock() |
|
|
|
if logSystems != nil { |
|
|
|
if logSystems != nil { |
|
|
|
quit <- true |
|
|
|
status := make(chan error) |
|
|
|
|
|
|
|
quit <- status |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-drained: |
|
|
|
case <-drained: |
|
|
|
|
|
|
|
default: |
|
|
|
} |
|
|
|
} |
|
|
|
<-shutdown |
|
|
|
<-status |
|
|
|
} |
|
|
|
} |
|
|
|
logSystems = nil |
|
|
|
logSystems = nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// waits until log messages are drained (dispatched to log writers)
|
|
|
|
// waits until log messages are drained (dispatched to log writers)
|
|
|
|
func Flush() { |
|
|
|
func Flush() { |
|
|
|
mutex.Lock() |
|
|
|
|
|
|
|
defer mutex.Unlock() |
|
|
|
|
|
|
|
if logSystems != nil { |
|
|
|
if logSystems != nil { |
|
|
|
<-drained |
|
|
|
<-drained |
|
|
|
} |
|
|
|
} |
|
|
@ -110,22 +119,14 @@ func AddLogSystem(logSystem LogSystem) { |
|
|
|
mutex.Lock() |
|
|
|
mutex.Lock() |
|
|
|
defer mutex.Unlock() |
|
|
|
defer mutex.Unlock() |
|
|
|
if logSystems == nil { |
|
|
|
if logSystems == nil { |
|
|
|
logMessages = make(chan *logMessage) |
|
|
|
logMessages = make(chan *logMessage, 5) |
|
|
|
quit = make(chan bool) |
|
|
|
quit = make(chan chan error, 1) |
|
|
|
drained = make(chan bool, 1) |
|
|
|
drained = make(chan bool, 1) |
|
|
|
shutdown = make(chan bool, 1) |
|
|
|
|
|
|
|
go start() |
|
|
|
go start() |
|
|
|
} |
|
|
|
} |
|
|
|
logSystems = append(logSystems, logSystem) |
|
|
|
logSystems = append(logSystems, logSystem) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func send(msg *logMessage) { |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-drained: |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
logMessages <- msg |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (logger *Logger) sendln(level LogLevel, v ...interface{}) { |
|
|
|
func (logger *Logger) sendln(level LogLevel, v ...interface{}) { |
|
|
|
if logSystems != nil { |
|
|
|
if logSystems != nil { |
|
|
|
send(newPrintlnLogMessage(level, logger.tag, v...)) |
|
|
|
send(newPrintlnLogMessage(level, logger.tag, v...)) |
|
|
|