forked from rokath/trice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineTransformerANSI.go
230 lines (207 loc) · 7.4 KB
/
lineTransformerANSI.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
// Copyright 2020 Thomas.Hoehenleitner [at] seerose.net
// Use of this source code is governed by a license that can be found in the LICENSE file.
package emitter
// TODO: Now the color is resettet after each string. This is needed only after the last string in a line.
import (
"fmt"
"io"
"strings"
"unicode"
"github.com/mgutz/ansi"
)
// LineTransformerANSI imnplements a Linewriter interface.
// It uses an internal Linewriter lw to write to.
// It converts the channel information to color data using colorPalette.
// In case of a remote display the lineTranslator should be used there.
type LineTransformerANSI struct {
lw LineWriter
colorPalette string
}
// NewLineTransformerANSI translates lines to ANSI colors according to colorPalette.
// It provides a Linewriter interface and uses internally a Linewriter.
func NewLineTransformerANSI(lw LineWriter, colorPalette string) *LineTransformerANSI {
p := &LineTransformerANSI{lw, colorPalette}
return p
}
var (
// LogLevel is usable to suppress less important logs.
LogLevel = "all"
// log level colors
colorizeFATAL = ansi.ColorFunc("121+i")
colorizeCRITICAL = ansi.ColorFunc("121+i")
colorizeEMERGENCY = ansi.ColorFunc("121+i")
colorizeERROR = ansi.ColorFunc("11:red")
colorizeWARNING = ansi.ColorFunc("11+i:red")
colorizeATTENTION = ansi.ColorFunc("11:green")
colorizeINFO = ansi.ColorFunc("121+i")
colorizeDEBUG = ansi.ColorFunc("130+i")
colorizeTRACE = ansi.ColorFunc("121+i")
// user mode colors
colorizeTIME = ansi.ColorFunc("108:blue")
colorizeMESSAGE = ansi.ColorFunc("green+h:black")
colorizeREAD = ansi.ColorFunc("101:black")
colorizeWRITE = ansi.ColorFunc("101+i:black")
colorizeDIAG = ansi.ColorFunc("161+B")
colorizeINTERRUPT = ansi.ColorFunc("13+i")
colorizeSIGNAL = ansi.ColorFunc("118+i")
colorizeTEST = ansi.ColorFunc("yellow+h:black")
// colorizeDEFAULT = ansi.ColorFunc("121+i")
// colorizeNOTICE = ansi.ColorFunc("121+i")
// colorizeALERT = ansi.ColorFunc("121+i")
// colorizeASSERT = ansi.ColorFunc("121+i")
// colorizeALARM = ansi.ColorFunc("121+i")
// colorizeCYCLE = ansi.ColorFunc("11:red")
// colorizeVERBOSE = ansi.ColorFunc("121+i")
)
func isLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
type ColorChannel struct {
events int
channel []string
colorize func(string) string
}
var ColorChannels = []ColorChannel{
// log level
{0, []string{"Fatal", "fatal", "FATAL"}, colorizeFATAL},
{0, []string{"Critical", "critical", "CRITICAL", "crit", "Crit", "CRIT"}, colorizeCRITICAL},
{0, []string{"Emergency", "emergency", "EMERGENCY"}, colorizeEMERGENCY},
{0, []string{"Error", "e", "err", "error", "E", "ERR", "ERROR"}, colorizeERROR},
{0, []string{"Warning", "w", "wrn", "warning", "W", "WRN", "WARNING", "Warn", "warn", "WARN"}, colorizeWARNING},
{0, []string{"att", "attention", "Attention", "ATT", "ATTENTION"}, colorizeATTENTION},
{0, []string{"Info", "i", "inf", "info", "informal", "I", "INF", "INFO", "INFORMAL"}, colorizeINFO},
{0, []string{"Debug", "d", "db", "dbg", "debug", "D", "DB", "DBG", "DEBUG"}, colorizeDEBUG},
{0, []string{"Trace", "trace", "TRACE"}, colorizeTRACE},
// user modes
{0, []string{"Timestamp", "tim", "time", "TIM", "TIME", "TIMESTAMP", "timestamp"}, colorizeTIME},
{0, []string{"m", "msg", "message", "M", "MSG", "MESSAGE"}, colorizeMESSAGE},
{0, []string{"rd", "rd_", "RD", "RD_"}, colorizeREAD},
{0, []string{"wr", "wr_", "WR", "WR_"}, colorizeWRITE},
{0, []string{"dia", "diag", "Diag", "DIA", "DIAG"}, colorizeDIAG},
{0, []string{"int", "isr", "ISR", "INT", "interrupt", "Interrupt", "INTERRUPT"}, colorizeINTERRUPT},
{0, []string{"s", "sig", "signal", "S", "SIG", "SIGNAL"}, colorizeSIGNAL},
{0, []string{"t", "tst", "test", "T", "TST", "TEST"}, colorizeTEST},
// {0, []string{"Default", "DEFAULT", "default"}, colorizeDEFAULT},
// {0, []string{"Notice", "NOTICE", "notice", "Note", "note", "NOTE"}, colorizeNOTICE},
// {0, []string{"Alert", "alert", "ALERT"}, colorizeALERT},
// {0, []string{"Assert", "assert", "ASSERT"}, colorizeASSERT},
// {0, []string{"Alarm", "alarm", "ALARM"}, colorizeALARM},
// {0, []string{"cycle", "CYCLE"}, colorizeCYCLE},
// {0, []string{"Verbose", "verbose", "VERBOSE"}, colorizeVERBOSE},
}
// ColorChannelEvents returns count of occurred channel events.
// If ch is unknown, the returned value is -1.
func ColorChannelEvents(ch string) int {
for _, s := range ColorChannels {
for _, c := range s.channel {
if c == ch {
return s.events
}
}
}
return -1
}
// PrintColorChannelEvents shows the amount of occurred channel events.
func PrintColorChannelEvents(w io.Writer) {
for _, s := range ColorChannels {
if s.events != 0 {
fmt.Fprintf(w, "%6d times: ", s.events)
for _, c := range s.channel {
fmt.Fprint(w, c, " ")
}
fmt.Fprintln(w, "")
}
}
}
// channelVariants returns all variants of ch as string slice.
// If ch is not inside ansiSel nil is returned.
func channelVariants(ch string) []string {
for _, s := range ColorChannels {
for _, c := range s.channel {
if c == ch {
return s.channel
}
}
}
return nil
}
// isChannel returns true if ch is any ansiSel string.
func isChannel(ch string) bool {
cv := channelVariants(ch)
return cv != nil
}
// colorize prefixes s with an ansi color code according to this conditions:
// If p.colorPalette is "off", do nothing.
// If p.colorPalette is "none" remove only lower case channel info "col:"
// If "COL:" is begin of string add ANSI color code according to COL:
// If "col:" is begin of string replace "col:" with ANSI color code according to col:
// Additionally, if global variable LogLevel is not the default "all", but found inside
// ColorChannels, logs with higher index positions are suppressed.
// As special case LogLevel == "off" does not output anything.
func (p *LineTransformerANSI) colorize(s string) (r string) {
if LogLevel == "off" {
return // do not log at all
}
var logLev int // numeric log level
var logThreshold int // numeric log thereshold
r = s
sc := strings.SplitN(s, ":", 2)
if len(sc) < 2 { // no color separator (no log level)
return // do nothing, return unchanged string
}
for i, cc := range ColorChannels {
for _, c := range cc.channel {
if c == sc[0] {
ColorChannels[i].events++ // count event
logLev = i
}
if c == LogLevel {
logThreshold = i
}
}
}
if LogLevel != "all" && logLev > logThreshold {
r = "" // suppress unwanted logs
return
}
if p.colorPalette == "off" {
return // do nothing (despite event counting)
}
if isChannel(sc[0]) && isLower(sc[0]) {
r = sc[1] // remove channel info
}
if p.colorPalette == "none" {
return
}
for _, cs := range ColorChannels {
for _, c := range cs.channel {
if c == sc[0] {
return cs.colorize(r)
}
}
}
return
}
// writeLine consumes a full line, translates it and writes it to the internal Linewriter.
// It adds ANSI color Codes and replaces col: channel information.
// It treats each sub string separately and and a color reset code at the end.
func (p *LineTransformerANSI) writeLine(line []string) {
var colored bool
l := make([]string, 0, 10)
for _, s := range line {
cs := p.colorize(s)
l = append(l, cs)
if cs != s {
colored = true
}
}
if (p.colorPalette == "default" || p.colorPalette == "color") && 1 < len(l) && colored {
l = append(l, ansi.Reset)
}
p.lw.writeLine(l)
}