aboutsummaryrefslogtreecommitdiffstats
path: root/ethlog/loggers.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <obscuren@users.noreply.github.com>2014-07-07 16:52:02 +0800
committerJeffrey Wilcke <obscuren@users.noreply.github.com>2014-07-07 16:52:02 +0800
commitb958179263aa1fa5060dd5d5848b8bb092a4165c (patch)
tree9ea81946396fc584c8c8a0b64d9e9f9fff43b17a /ethlog/loggers.go
parentb232acd04ef957fb65e1c49b330165535da7e871 (diff)
parent5a2afc575485e2d651b9840f5d1ea080cdc72fa7 (diff)
downloadgo-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar.gz
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar.bz2
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar.lz
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar.xz
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.tar.zst
go-tangerine-b958179263aa1fa5060dd5d5848b8bb092a4165c.zip
Merge pull request #33 from ethersphere/feature/ethutil-refactor
ethreact - Feature/ethutil refactor
Diffstat (limited to 'ethlog/loggers.go')
-rw-r--r--ethlog/loggers.go57
1 files changed, 39 insertions, 18 deletions
diff --git a/ethlog/loggers.go b/ethlog/loggers.go
index 219c78240..d7707cf9e 100644
--- a/ethlog/loggers.go
+++ b/ethlog/loggers.go
@@ -40,6 +40,9 @@ 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
@@ -57,29 +60,41 @@ 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 <-quit:
- break out
+ case drained <- true:
+ default:
+ drained <- true // this blocks until a message is sent to the queu
}
}
+ close(shutdown)
}
-// waits until log messages are drained (dispatched to log writers)
-func Flush() {
- quit <- true
-
-done:
- for {
+func Reset() {
+ mutex.Lock()
+ defer mutex.Unlock()
+ if logSystems != nil {
+ quit <- true
select {
- case <-logMessages:
- default:
- break done
+ case <-drained:
}
+ <-shutdown
+ }
+ logSystems = nil
+}
+
+// waits until log messages are drained (dispatched to log writers)
+func Flush() {
+ mutex.Lock()
+ defer mutex.Unlock()
+ if logSystems != nil {
+ <-drained
}
}
@@ -92,28 +107,34 @@ 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 logMessages != nil {
- msg := newPrintlnLogMessage(level, logger.tag, v...)
- logMessages <- msg
+ if logSystems != nil {
+ send(newPrintlnLogMessage(level, logger.tag, v...))
}
}
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
- if logMessages != nil {
- msg := newPrintfLogMessage(level, logger.tag, format, v...)
- logMessages <- msg
+ if logSystems != nil {
+ send(newPrintfLogMessage(level, logger.tag, format, v...))
}
}