diff options
author | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-07-07 16:59:16 +0800 |
---|---|---|
committer | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-07-07 16:59:16 +0800 |
commit | 6fe9b4ab5e839be96eb1c4a619bc14fab622d8d1 (patch) | |
tree | b8b1218e456d3daaad4a36e6ba022323c3762038 /ethlog | |
parent | 9dab7dcc3c99d387ad2c0a1623b574d3023bdfae (diff) | |
download | dexon-6fe9b4ab5e839be96eb1c4a619bc14fab622d8d1.tar.gz dexon-6fe9b4ab5e839be96eb1c4a619bc14fab622d8d1.tar.zst dexon-6fe9b4ab5e839be96eb1c4a619bc14fab622d8d1.zip |
Revert "ethreact - Feature/ethutil refactor"
Diffstat (limited to 'ethlog')
-rw-r--r-- | ethlog/loggers.go | 57 | ||||
-rw-r--r-- | ethlog/loggers_test.go | 23 |
2 files changed, 30 insertions, 50 deletions
diff --git a/ethlog/loggers.go b/ethlog/loggers.go index d7707cf9e..219c78240 100644 --- a/ethlog/loggers.go +++ b/ethlog/loggers.go @@ -40,9 +40,6 @@ func (msg *logMessage) send(logger LogSystem) { var logMessages chan (*logMessage) var logSystems []LogSystem var quit chan bool -var drained chan bool -var shutdown chan bool -var mutex = sync.Mutex{} type LogLevel uint8 @@ -60,41 +57,29 @@ func start() { out: for { select { - case <-quit: - break out case msg := <-logMessages: for _, logSystem := range logSystems { if logSystem.GetLogLevel() >= msg.LogLevel { msg.send(logSystem) } } - case drained <- true: - default: - drained <- true // this blocks until a message is sent to the queu - } - } - close(shutdown) -} - -func Reset() { - mutex.Lock() - defer mutex.Unlock() - if logSystems != nil { - quit <- true - select { - case <-drained: + case <-quit: + break out } - <-shutdown } - logSystems = nil } // waits until log messages are drained (dispatched to log writers) func Flush() { - mutex.Lock() - defer mutex.Unlock() - if logSystems != nil { - <-drained + quit <- true + +done: + for { + select { + case <-logMessages: + default: + break done + } } } @@ -107,34 +92,28 @@ func NewLogger(tag string) *Logger { } func AddLogSystem(logSystem LogSystem) { + var mutex = &sync.Mutex{} mutex.Lock() defer mutex.Unlock() if logSystems == nil { logMessages = make(chan *logMessage) quit = make(chan bool) - 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...)) + if logMessages != nil { + msg := newPrintlnLogMessage(level, logger.tag, v...) + logMessages <- msg } } func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) { - if logSystems != nil { - send(newPrintfLogMessage(level, logger.tag, format, v...)) + if logMessages != nil { + msg := newPrintfLogMessage(level, logger.tag, format, v...) + logMessages <- msg } } diff --git a/ethlog/loggers_test.go b/ethlog/loggers_test.go index 9fff471c1..89f416681 100644 --- a/ethlog/loggers_test.go +++ b/ethlog/loggers_test.go @@ -28,6 +28,10 @@ func (t *TestLogSystem) GetLogLevel() LogLevel { return t.level } +func quote(s string) string { + return fmt.Sprintf("'%s'", s) +} + func TestLoggerPrintln(t *testing.T) { logger := NewLogger("TEST") testLogSystem := &TestLogSystem{level: WarnLevel} @@ -37,10 +41,10 @@ func TestLoggerPrintln(t *testing.T) { logger.Infoln("info") logger.Debugln("debug") Flush() - Reset() output := testLogSystem.Output + fmt.Println(quote(output)) if output != "[TEST] error\n[TEST] warn\n" { - t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem.Output) + t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem.Output)) } } @@ -53,10 +57,10 @@ func TestLoggerPrintf(t *testing.T) { logger.Infof("info") logger.Debugf("debug") Flush() - Reset() output := testLogSystem.Output + fmt.Println(quote(output)) if output != "[TEST] error to { 2}\n[TEST] warn" { - t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", testLogSystem.Output) + t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", quote(testLogSystem.Output)) } } @@ -69,14 +73,13 @@ func TestMultipleLogSystems(t *testing.T) { logger.Errorln("error") logger.Warnln("warn") Flush() - Reset() output0 := testLogSystem0.Output output1 := testLogSystem1.Output if output0 != "[TEST] error\n" { - t.Error("Expected logger 0 output '[TEST] error\\n', got ", testLogSystem0.Output) + t.Error("Expected logger 0 output '[TEST] error\\n', got ", quote(testLogSystem0.Output)) } if output1 != "[TEST] error\n[TEST] warn\n" { - t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem1.Output) + t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem1.Output)) } } @@ -89,11 +92,11 @@ func TestFileLogSystem(t *testing.T) { logger.Errorf("error to %s\n", filename) logger.Warnln("warn") Flush() - Reset() contents, _ := ioutil.ReadFile(filename) output := string(contents) + fmt.Println(quote(output)) if output != "[TEST] error to test.log\n[TEST] warn\n" { - t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output) + t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", quote(output)) } else { os.Remove(filename) } @@ -102,7 +105,5 @@ func TestFileLogSystem(t *testing.T) { func TestNoLogSystem(t *testing.T) { logger := NewLogger("TEST") logger.Warnln("warn") - fmt.Println("1") Flush() - Reset() } |