/
termlogger.go
250 lines (230 loc) · 6.26 KB
/
termlogger.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package logging
import (
"fmt"
"os"
"runtime/trace"
"strings"
"sync"
"time"
"github.com/nsf/termbox-go"
)
// NewTerminalLogger creates a new TerminalLogger instance using the specified
// MemoryLogger instances.
func NewTerminalLogger(logs []*MemoryLogger) *TerminalLogger {
return &TerminalLogger{
logs: logs,
enabled: []bool{true, true, true, true},
appName: "Horde",
mutex: sync.Mutex{},
}
}
// TerminalLogger is a logger that creates a console logging screen with logs
// that can be toggled runtime.
type TerminalLogger struct {
logs []*MemoryLogger
enabled []bool
appName string
mutex sync.Mutex
traceFile *os.File
}
// Split and pad lines with spaces to get an array of strings
// with length set to maxLen.
func splitAndPadLines(msg string, maxLen int) []string {
l := len(msg)
padding := maxLen - (l % maxLen)
if l != 0 && (l%maxLen) == 0 {
padding = 0
}
lines := (l + padding) / maxLen
ret := make([]string, lines)
msg = msg + strings.Repeat(" ", padding)
for i := range ret {
ret[i] = msg[:maxLen]
msg = msg[maxLen:]
}
return ret
}
// Start launches the logging screen. If there's an error launching the screen
// it will be returned. The method doesn't return until the user presses the
// escape key.
func (t *TerminalLogger) Start() error {
if err := termbox.Init(); err != nil {
return err
}
termbox.HideCursor()
defer termbox.Close()
t.draw()
quit := make(chan bool)
go func() {
counters := make([]int, 4)
for {
redraw := false
for i := 0; i < 4; i++ {
if t.logs[i].NumEntries() > counters[i] {
counters[i] = t.logs[i].NumEntries()
redraw = true
}
}
select {
case <-quit:
return
case <-time.After(75 * time.Millisecond):
// ok
}
if redraw {
t.draw()
}
}
}()
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventInterrupt {
quit <- true
return nil
}
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyCtrlC:
fallthrough
case termbox.KeyCtrlX:
fallthrough
case termbox.KeyEsc:
quit <- true
return nil
case termbox.KeyCtrlD:
t.toggle(DebugLevel)
case termbox.KeyCtrlI:
t.toggle(InfoLevel)
case termbox.KeyCtrlW:
t.toggle(WarningLevel)
case termbox.KeyCtrlE:
t.toggle(ErrorLevel)
case termbox.KeyCtrlT:
t.toggleTrace()
}
}
t.draw()
}
}
// toggle log levels on and off
func (t *TerminalLogger) toggle(level uint) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.enabled[level] = !t.enabled[level]
}
// Draw a string to the screen
func (t *TerminalLogger) drawString(x, y, w int, text string, fg, bg termbox.Attribute) {
pos := x
for _, ch := range text {
termbox.SetCell(pos, y, ch, fg, bg)
pos++
if pos > w {
return
}
}
}
// Draw the title bar
func (t *TerminalLogger) drawTitleBar(w int) {
caption := fmt.Sprintf("%s logs", t.appName)
xpos := w/2 + len(caption)/2
title := fmt.Sprintf("%s%s%s", strings.Repeat(" ", xpos), caption, strings.Repeat(" ", w-xpos-len(t.appName)))
t.drawString(0, 0, w, title, termbox.ColorYellow|termbox.AttrBold, termbox.ColorBlue)
}
// Draw log indicator at the bottom right hand corner
func (t *TerminalLogger) drawIndicator(w, h, pos int, name string, enabled bool, fg, bg termbox.Attribute) {
x := w - (pos * 5)
y := h - 1
if enabled {
t.drawString(x, y, w, fmt.Sprintf(" %s ", name), fg, bg)
return
}
t.drawString(x, y, w, fmt.Sprintf(" %s ", name), termbox.ColorBlue, termbox.ColorBlack)
}
// Draw the status bar
func (t *TerminalLogger) drawStatusBar(w, h int) {
helpStr := fmt.Sprintf("Ctrl+D, I, W, E: Toggle levels (E:%d/W:%d/I:%d/D:%d), Ctrl+T: Toggle trace",
t.logs[ErrorLevel].NumEntries(),
t.logs[WarningLevel].NumEntries(),
t.logs[InfoLevel].NumEntries(),
t.logs[DebugLevel].NumEntries())
t.drawString(0, h-1, w, strings.Repeat(" ", w), termbox.ColorYellow, termbox.ColorBlue)
t.drawString(1, h-1, w, helpStr, termbox.ColorYellow, termbox.ColorBlue)
t.drawIndicator(w, h, 1, "E", t.enabled[ErrorLevel], termbox.ColorWhite, termbox.ColorRed)
t.drawIndicator(w, h, 2, "W", t.enabled[WarningLevel], termbox.ColorBlack, termbox.ColorYellow)
t.drawIndicator(w, h, 3, "I", t.enabled[InfoLevel], termbox.ColorBlack, termbox.ColorCyan)
t.drawIndicator(w, h, 4, "D", t.enabled[DebugLevel], termbox.ColorBlack, termbox.ColorWhite)
t.drawIndicator(w, h, 5, "T", t.traceFile != nil, termbox.ColorYellow, termbox.ColorRed)
}
// Draw the log entries
func (t *TerminalLogger) drawLogs(w, h int) {
enabled := []*MemoryLogger{}
for i := 0; i < 4; i++ {
if t.enabled[i] {
enabled = append(enabled, t.logs[i])
}
}
if len(enabled) == 0 {
return
}
elems := enabled[0].Merge(enabled[1:]...)
index := len(elems) - 1
for i := h - 2; i > 0; i-- {
if index > -1 {
prefix := fmt.Sprintf("%8s %-20s ", elems[index].Time.Format("15:04:05"), elems[index].Location)
prefixLen := len(prefix)
lines := splitAndPadLines(elems[index].Message, w-prefixLen)
fg := termbox.ColorWhite
bg := termbox.ColorDefault
switch elems[index].Level {
case DebugLevel:
fg = termbox.ColorWhite
case InfoLevel:
fg = termbox.ColorBlue | termbox.AttrBold
case WarningLevel:
fg = termbox.ColorYellow | termbox.AttrBold
case ErrorLevel:
fg = termbox.ColorRed | termbox.AttrBold
}
blankPrefix := strings.Repeat(" ", prefixLen+1)
for n := len(lines) - 1; n > 0; n-- {
t.drawString(0, i, w, blankPrefix+lines[n], fg, bg)
i--
}
t.drawString(0, i, w, prefix+lines[0], fg, bg)
index--
}
}
}
// Redraw the screen
func (t *TerminalLogger) draw() {
t.mutex.Lock()
w, h := termbox.Size()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
t.drawTitleBar(w)
t.drawStatusBar(w, h)
t.drawLogs(w, h)
termbox.Flush()
t.mutex.Unlock()
}
func (t *TerminalLogger) toggleTrace() {
if t.traceFile != nil {
trace.Stop()
t.traceFile.Close()
t.traceFile = nil
Info("Trace is completed")
return
}
traceFileName := time.Now().Format("trace_2006-01-02T150405.trace")
var err error
t.traceFile, err = os.Create(traceFileName)
if err != nil {
Error("Unable to create trace file '%s': %v", traceFileName, err)
return
}
Info("Trace started. Trace file name is %s", traceFileName)
if err := trace.Start(t.traceFile); err != nil {
Error("Unable to start the trace: %v", err)
t.traceFile.Close()
return
}
}