aboutsummaryrefslogtreecommitdiffstats
path: root/ethlog
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2014-07-15 01:37:01 +0800
committerzelig <viktor.tron@gmail.com>2014-07-15 01:37:01 +0800
commit5c03adbdededd31cb73f64ced01e33154347e193 (patch)
tree72b43f8f97596e7e871c0b865d446d0d90c5483a /ethlog
parent4d77b7facecfea7069af15f19429585687c47fbb (diff)
downloadgo-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar.gz
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar.bz2
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar.lz
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar.xz
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.tar.zst
go-tangerine-5c03adbdededd31cb73f64ced01e33154347e193.zip
fix logger channel blocking
Diffstat (limited to 'ethlog')
-rw-r--r--ethlog/loggers.go55
-rw-r--r--ethlog/loggers_test.go15
2 files changed, 43 insertions, 27 deletions
diff --git a/ethlog/loggers.go b/ethlog/loggers.go
index d7707cf9e..4e950cadd 100644
--- a/ethlog/loggers.go
+++ b/ethlog/loggers.go
@@ -39,9 +39,8 @@ func (msg *logMessage) send(logger LogSystem) {
var logMessages chan (*logMessage)
var logSystems []LogSystem
-var quit chan bool
+var quit chan chan error
var drained chan bool
-var shutdown chan bool
var mutex = sync.Mutex{}
type LogLevel uint8
@@ -55,44 +54,54 @@ const (
DebugDetailLevel
)
+func dispatch(msg *logMessage) {
+ for _, logSystem := range logSystems {
+ if logSystem.GetLogLevel() >= msg.LogLevel {
+ msg.send(logSystem)
+ }
+ }
+}
+
// log messages are dispatched to log writers
func start() {
-out:
for {
select {
- case <-quit:
- break out
+ case status := <-quit:
+ status <- nil
+ return
case msg := <-logMessages:
- for _, logSystem := range logSystems {
- if logSystem.GetLogLevel() >= msg.LogLevel {
- msg.send(logSystem)
- }
- }
- case drained <- true:
+ dispatch(msg)
default:
- drained <- true // this blocks until a message is sent to the queu
+ drained <- true // this blocks until a message is sent to the queue
}
}
- close(shutdown)
+}
+
+func send(msg *logMessage) {
+ logMessages <- msg
+ select {
+ case <-drained:
+ default:
+ }
}
func Reset() {
mutex.Lock()
defer mutex.Unlock()
if logSystems != nil {
- quit <- true
+ status := make(chan error)
+ quit <- status
select {
case <-drained:
+ default:
}
- <-shutdown
+ <-status
}
logSystems = nil
}
// waits until log messages are drained (dispatched to log writers)
func Flush() {
- mutex.Lock()
- defer mutex.Unlock()
if logSystems != nil {
<-drained
}
@@ -110,22 +119,14 @@ func AddLogSystem(logSystem LogSystem) {
mutex.Lock()
defer mutex.Unlock()
if logSystems == nil {
- logMessages = make(chan *logMessage)
- quit = make(chan bool)
+ logMessages = make(chan *logMessage, 5)
+ quit = make(chan chan error, 1)
drained = make(chan bool, 1)
- shutdown = make(chan bool, 1)
go start()
}
logSystems = append(logSystems, logSystem)
}
-func send(msg *logMessage) {
- select {
- case <-drained:
- }
- logMessages <- msg
-}
-
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
if logSystems != nil {
send(newPrintlnLogMessage(level, logger.tag, v...))
diff --git a/ethlog/loggers_test.go b/ethlog/loggers_test.go
index 9fff471c1..0e1c12e55 100644
--- a/ethlog/loggers_test.go
+++ b/ethlog/loggers_test.go
@@ -28,6 +28,21 @@ func (t *TestLogSystem) GetLogLevel() LogLevel {
return t.level
}
+func TestLoggerFlush(t *testing.T) {
+ logger := NewLogger("TEST")
+ testLogSystem := &TestLogSystem{level: WarnLevel}
+ AddLogSystem(testLogSystem)
+ for i := 0; i < 5; i++ {
+ logger.Errorf(".")
+ }
+ Flush()
+ Reset()
+ output := testLogSystem.Output
+ if output != "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] ." {
+ t.Error("Expected complete logger output '[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .', got ", output)
+ }
+}
+
func TestLoggerPrintln(t *testing.T) {
logger := NewLogger("TEST")
testLogSystem := &TestLogSystem{level: WarnLevel}