forked from cerc-io/plugeth
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.
This commit is contained in:
parent
50f5ba5b0c
commit
c090a77f1c
@ -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) {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ethlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
@ -16,15 +15,9 @@ type TestLogSystem struct {
|
||||
level LogLevel
|
||||
}
|
||||
|
||||
func (ls *TestLogSystem) Println(v ...interface{}) {
|
||||
func (ls *TestLogSystem) LogPrint(level LogLevel, msg string) {
|
||||
ls.mutex.Lock()
|
||||
ls.output += fmt.Sprintln(v...)
|
||||
ls.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (ls *TestLogSystem) Printf(format string, v ...interface{}) {
|
||||
ls.mutex.Lock()
|
||||
ls.output += fmt.Sprintf(format, v...)
|
||||
ls.output += msg
|
||||
ls.mutex.Unlock()
|
||||
}
|
||||
|
||||
@ -54,14 +47,9 @@ type blockedLogSystem struct {
|
||||
unblock chan struct{}
|
||||
}
|
||||
|
||||
func (ls blockedLogSystem) Println(v ...interface{}) {
|
||||
func (ls blockedLogSystem) LogPrint(level LogLevel, msg string) {
|
||||
<-ls.unblock
|
||||
ls.LogSystem.Println(v...)
|
||||
}
|
||||
|
||||
func (ls blockedLogSystem) Printf(fmt string, v ...interface{}) {
|
||||
<-ls.unblock
|
||||
ls.LogSystem.Printf(fmt, v...)
|
||||
ls.LogSystem.LogPrint(level, msg)
|
||||
}
|
||||
|
||||
func TestLoggerFlush(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user