aboutsummaryrefslogtreecommitdiffstats
path: root/ethlog/loggers.go
blob: ec481edd8ac5b5fa1e77a98cadc09ef52f46ab68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package ethlog

import (
    "fmt"
    "io"
    "log"
    "os"
    "sync"
)

type LogSystem interface {
    GetLogLevel() LogLevel
    SetLogLevel(i LogLevel)
    Println(v ...interface{})
    Printf(format string, v ...interface{})
}

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...))}
}

func (msg *logMessage) send(logger LogSystem) {
    if msg.format {
        logger.Printf(msg.msg)
    } else {
        logger.Println(msg.msg)
    }
}

var logMessages chan (*logMessage)
var logSystems []LogSystem
var quit chan bool

type LogLevel uint8

const (
    Silence LogLevel = iota
    ErrorLevel
    WarnLevel
    InfoLevel
    DebugLevel
    DebugDetailLevel
)

// log messages are dispatched to log writers
func start() {
out:
    for {
        select {
        case msg := <-logMessages:
            for _, logSystem := range logSystems {
                if logSystem.GetLogLevel() >= msg.LogLevel {
                    msg.send(logSystem)
                }
            }
        case <-quit:
            break out
        }
    }
}

// waits until log messages are drained (dispatched to log writers)
func Flush() {
    quit <- true

done:
    for {
        select {
        case <-logMessages:
        default:
            break done
        }
    }
}

type Logger struct {
    tag string
}

func NewLogger(tag string) *Logger {
    return &Logger{tag}
}

func AddLogSystem(logSystem LogSystem) {
    var mutex = &sync.Mutex{}
    mutex.Lock()
    defer mutex.Unlock()
    if logSystems == nil {
        logMessages = make(chan *logMessage)
        quit = make(chan bool, 1)
        go start()
    }
    logSystems = append(logSystems, logSystem)
}

func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
    if logMessages != nil {
        msg := newPrintlnLogMessage(level, logger.tag, v...)
        logMessages <- msg
    }
}

func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
    if logMessages != nil {
        msg := newPrintfLogMessage(level, logger.tag, format, v...)
        logMessages <- msg
    }
}

func (logger *Logger) Errorln(v ...interface{}) {
    logger.sendln(ErrorLevel, v...)
}

func (logger *Logger) Warnln(v ...interface{}) {
    logger.sendln(WarnLevel, v...)
}

func (logger *Logger) Infoln(v ...interface{}) {
    logger.sendln(InfoLevel, v...)
}

func (logger *Logger) Debugln(v ...interface{}) {
    logger.sendln(DebugLevel, v...)
}

func (logger *Logger) DebugDetailln(v ...interface{}) {
    logger.sendln(DebugDetailLevel, v...)
}

func (logger *Logger) Errorf(format string, v ...interface{}) {
    logger.sendf(ErrorLevel, format, v...)
}

func (logger *Logger) Warnf(format string, v ...interface{}) {
    logger.sendf(WarnLevel, format, v...)
}

func (logger *Logger) Infof(format string, v ...interface{}) {
    logger.sendf(InfoLevel, format, v...)
}

func (logger *Logger) Debugf(format string, v ...interface{}) {
    logger.sendf(DebugLevel, format, v...)
}

func (logger *Logger) DebugDetailf(format string, v ...interface{}) {
    logger.sendf(DebugDetailLevel, format, v...)
}

func (logger *Logger) Fatalln(v ...interface{}) {
    logger.sendln(ErrorLevel, v...)
    Flush()
    os.Exit(0)
}

func (logger *Logger) Fatalf(format string, v ...interface{}) {
    logger.sendf(ErrorLevel, format, v...)
    Flush()
    os.Exit(0)
}

type StdLogSystem struct {
    logger *log.Logger
    level  LogLevel
}

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) SetLogLevel(i LogLevel) {
    t.level = i
}

func (t *StdLogSystem) GetLogLevel() LogLevel {
    return t.level
}

func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) *StdLogSystem {
    logger := log.New(writer, "", flags)
    return &StdLogSystem{logger, level}
}