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 {
|
type LogSystem interface {
|
||||||
GetLogLevel() LogLevel
|
GetLogLevel() LogLevel
|
||||||
SetLogLevel(i LogLevel)
|
SetLogLevel(i LogLevel)
|
||||||
Println(v ...interface{})
|
LogPrint(LogLevel, string)
|
||||||
Printf(format string, v ...interface{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type logMessage struct {
|
type message struct {
|
||||||
LogLevel LogLevel
|
level LogLevel
|
||||||
format bool
|
msg string
|
||||||
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 LogLevel uint8
|
type LogLevel uint8
|
||||||
@ -60,7 +50,7 @@ var (
|
|||||||
mutex sync.RWMutex // protects logSystems
|
mutex sync.RWMutex // protects logSystems
|
||||||
logSystems []LogSystem
|
logSystems []LogSystem
|
||||||
|
|
||||||
logMessages = make(chan *logMessage)
|
logMessages = make(chan message)
|
||||||
drainWaitReq = make(chan chan struct{})
|
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()
|
mutex.RLock()
|
||||||
for _, sys := range logSystems {
|
for _, sys := range logSystems {
|
||||||
if sys.GetLogLevel() >= msg.LogLevel {
|
if sys.GetLogLevel() >= msg.level {
|
||||||
if msg.format {
|
sys.LogPrint(msg.level, msg.msg)
|
||||||
sys.Printf(msg.msg)
|
|
||||||
} else {
|
|
||||||
sys.Println(msg.msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mutex.RUnlock()
|
mutex.RUnlock()
|
||||||
done <- struct{}{}
|
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.
|
// Reset removes all active log systems.
|
||||||
func Reset() {
|
func Reset() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
@ -147,21 +126,15 @@ type Logger struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger(tag string) *Logger {
|
func NewLogger(tag string) *Logger {
|
||||||
return &Logger{tag}
|
return &Logger{"[" + tag + "] "}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
|
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
|
||||||
if logMessages != nil {
|
logMessages <- message{level, logger.tag + fmt.Sprintln(v...)}
|
||||||
msg := newPrintlnLogMessage(level, logger.tag, v...)
|
|
||||||
send(msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
|
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
|
||||||
if logMessages != nil {
|
logMessages <- message{level, logger.tag + fmt.Sprintf(format, v...)}
|
||||||
msg := newPrintfLogMessage(level, logger.tag, format, v...)
|
|
||||||
send(msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorln writes a message with ErrorLevel.
|
// Errorln writes a message with ErrorLevel.
|
||||||
@ -240,12 +213,8 @@ type stdLogSystem struct {
|
|||||||
level uint32
|
level uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *stdLogSystem) Println(v ...interface{}) {
|
func (t *stdLogSystem) LogPrint(level LogLevel, msg string) {
|
||||||
t.logger.Println(v...)
|
t.logger.Print(msg)
|
||||||
}
|
|
||||||
|
|
||||||
func (t *stdLogSystem) Printf(format string, v ...interface{}) {
|
|
||||||
t.logger.Printf(format, v...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *stdLogSystem) SetLogLevel(i LogLevel) {
|
func (t *stdLogSystem) SetLogLevel(i LogLevel) {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ethlog
|
package ethlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
@ -16,15 +15,9 @@ type TestLogSystem struct {
|
|||||||
level LogLevel
|
level LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ls *TestLogSystem) Println(v ...interface{}) {
|
func (ls *TestLogSystem) LogPrint(level LogLevel, msg string) {
|
||||||
ls.mutex.Lock()
|
ls.mutex.Lock()
|
||||||
ls.output += fmt.Sprintln(v...)
|
ls.output += msg
|
||||||
ls.mutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ls *TestLogSystem) Printf(format string, v ...interface{}) {
|
|
||||||
ls.mutex.Lock()
|
|
||||||
ls.output += fmt.Sprintf(format, v...)
|
|
||||||
ls.mutex.Unlock()
|
ls.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,14 +47,9 @@ type blockedLogSystem struct {
|
|||||||
unblock chan struct{}
|
unblock chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ls blockedLogSystem) Println(v ...interface{}) {
|
func (ls blockedLogSystem) LogPrint(level LogLevel, msg string) {
|
||||||
<-ls.unblock
|
<-ls.unblock
|
||||||
ls.LogSystem.Println(v...)
|
ls.LogSystem.LogPrint(level, msg)
|
||||||
}
|
|
||||||
|
|
||||||
func (ls blockedLogSystem) Printf(fmt string, v ...interface{}) {
|
|
||||||
<-ls.unblock
|
|
||||||
ls.LogSystem.Printf(fmt, v...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoggerFlush(t *testing.T) {
|
func TestLoggerFlush(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user