Added the ability to extend the logger with more sub systems

This commit is contained in:
obscuren 2014-02-22 01:53:09 +01:00
parent cca8585554
commit 4bfd717ba2

View File

@ -1,6 +1,7 @@
package ethutil package ethutil
import ( import (
"fmt"
"log" "log"
"os" "os"
"os/user" "os/user"
@ -18,7 +19,7 @@ const (
type config struct { type config struct {
Db Database Db Database
Log Logger Log *Logger
ExecPath string ExecPath string
Debug bool Debug bool
Ver string Ver string
@ -45,7 +46,7 @@ func ReadConfig(base string) *config {
} }
} }
Config = &config{ExecPath: path, Debug: true, Ver: "0.2.3"} Config = &config{ExecPath: path, Debug: true, Ver: "0.3.0"}
Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug) Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug)
} }
@ -59,13 +60,18 @@ const (
LogStd = 0x2 LogStd = 0x2
) )
type LogSystem interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
type Logger struct { type Logger struct {
logSys []*log.Logger logSys []LogSystem
logLevel int logLevel int
} }
func NewLogger(flag LoggerType, level int) Logger { func NewLogger(flag LoggerType, level int) *Logger {
var loggers []*log.Logger var loggers []LogSystem
flags := log.LstdFlags flags := log.LstdFlags
@ -84,7 +90,11 @@ func NewLogger(flag LoggerType, level int) Logger {
loggers = append(loggers, log) loggers = append(loggers, log)
} }
return Logger{logSys: loggers, logLevel: level} return &Logger{logSys: loggers, logLevel: level}
}
func (log *Logger) AddLogSystem(logger LogSystem) {
log.logSys = append(log.logSys, logger)
} }
const ( const (
@ -92,7 +102,7 @@ const (
LogLevelInfo LogLevelInfo
) )
func (log Logger) Debugln(v ...interface{}) { func (log *Logger) Debugln(v ...interface{}) {
if log.logLevel != LogLevelDebug { if log.logLevel != LogLevelDebug {
return return
} }
@ -102,7 +112,7 @@ func (log Logger) Debugln(v ...interface{}) {
} }
} }
func (log Logger) Debugf(format string, v ...interface{}) { func (log *Logger) Debugf(format string, v ...interface{}) {
if log.logLevel != LogLevelDebug { if log.logLevel != LogLevelDebug {
return return
} }
@ -112,17 +122,18 @@ func (log Logger) Debugf(format string, v ...interface{}) {
} }
} }
func (log Logger) Infoln(v ...interface{}) { func (log *Logger) Infoln(v ...interface{}) {
if log.logLevel > LogLevelInfo { if log.logLevel > LogLevelInfo {
return return
} }
fmt.Println(len(log.logSys))
for _, logger := range log.logSys { for _, logger := range log.logSys {
logger.Println(v...) logger.Println(v...)
} }
} }
func (log Logger) Infof(format string, v ...interface{}) { func (log *Logger) Infof(format string, v ...interface{}) {
if log.logLevel > LogLevelInfo { if log.logLevel > LogLevelInfo {
return return
} }