aboutsummaryrefslogtreecommitdiffstats
path: root/ethlog/loggers.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-10-16 16:50:51 +0800
committerFelix Lange <fjl@twurst.com>2014-10-17 23:23:29 +0800
commitc090a77f1c9bc890e67a00fb47a1c23c8769799d (patch)
treea01be1b9baf2b6cb4a0f133b40ab7219f395cdcd /ethlog/loggers.go
parent50f5ba5b0cfdf02cd87a9070676bd3c21c217296 (diff)
downloadgo-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar.gz
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar.bz2
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar.lz
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar.xz
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.tar.zst
go-tangerine-c090a77f1c9bc890e67a00fb47a1c23c8769799d.zip
ethlog: simplify LogSystem interface
Messages are formatted by generic part, so the log system doesn't need to provide formatting. This fixes the test from the previous commit. As a small bonus, log systems now have access to the level of the message. This could be used to provide colored logging in the future.
Diffstat (limited to 'ethlog/loggers.go')
-rw-r--r--ethlog/loggers.go57
1 files changed, 13 insertions, 44 deletions
diff --git a/ethlog/loggers.go b/ethlog/loggers.go
index b8a7b0455..f5ec4d402 100644
--- a/ethlog/loggers.go
+++ b/ethlog/loggers.go
@@ -26,22 +26,12 @@ import (
type LogSystem interface {
GetLogLevel() LogLevel
SetLogLevel(i LogLevel)
- Println(v ...interface{})
- Printf(format string, v ...interface{})
+ LogPrint(LogLevel, string)
}
-type logMessage struct {
- LogLevel LogLevel
- format bool
- msg string
-}
-
-func newPrintlnLogMessage(level LogLevel, tag string, v ...interface{}) *logMessage {
- return &logMessage{level, false, fmt.Sprintf("[%s] %s", tag, fmt.Sprint(v...))}
-}
-
-func newPrintfLogMessage(level LogLevel, tag string, format string, v ...interface{}) *logMessage {
- return &logMessage{level, true, fmt.Sprintf("[%s] %s", tag, fmt.Sprintf(format, v...))}
+type message struct {
+ level LogLevel
+ msg string
}
type LogLevel uint8
@@ -60,7 +50,7 @@ var (
mutex sync.RWMutex // protects logSystems
logSystems []LogSystem
- logMessages = make(chan *logMessage)
+ logMessages = make(chan message)
drainWaitReq = make(chan chan struct{})
)
@@ -95,28 +85,17 @@ func dispatchLoop() {
}
}
-func dispatch(msg *logMessage, done chan<- struct{}) {
+func dispatch(msg message, done chan<- struct{}) {
mutex.RLock()
for _, sys := range logSystems {
- if sys.GetLogLevel() >= msg.LogLevel {
- if msg.format {
- sys.Printf(msg.msg)
- } else {
- sys.Println(msg.msg)
- }
+ if sys.GetLogLevel() >= msg.level {
+ sys.LogPrint(msg.level, msg.msg)
}
}
mutex.RUnlock()
done <- struct{}{}
}
-// send delivers a message to all installed log
-// systems. it doesn't wait for the message to be
-// written.
-func send(msg *logMessage) {
- logMessages <- msg
-}
-
// Reset removes all active log systems.
func Reset() {
mutex.Lock()
@@ -147,21 +126,15 @@ type Logger struct {
}
func NewLogger(tag string) *Logger {
- return &Logger{tag}
+ return &Logger{"[" + tag + "] "}
}
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
- if logMessages != nil {
- msg := newPrintlnLogMessage(level, logger.tag, v...)
- send(msg)
- }
+ logMessages <- message{level, logger.tag + fmt.Sprintln(v...)}
}
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
- if logMessages != nil {
- msg := newPrintfLogMessage(level, logger.tag, format, v...)
- send(msg)
- }
+ logMessages <- message{level, logger.tag + fmt.Sprintf(format, v...)}
}
// Errorln writes a message with ErrorLevel.
@@ -240,12 +213,8 @@ type stdLogSystem struct {
level uint32
}
-func (t *stdLogSystem) Println(v ...interface{}) {
- t.logger.Println(v...)
-}
-
-func (t *stdLogSystem) Printf(format string, v ...interface{}) {
- t.logger.Printf(format, v...)
+func (t *stdLogSystem) LogPrint(level LogLevel, msg string) {
+ t.logger.Print(msg)
}
func (t *stdLogSystem) SetLogLevel(i LogLevel) {