@@ -15,7 +15,6 @@ import (
15
15
16
16
const (
17
17
timeFormat = "2006-01-02T15:04:05-0700"
18
- termTimeFormat = "01-02|15:04:05.000"
19
18
floatFormat = 'f'
20
19
termMsgJust = 40
21
20
termCtxMaxPadding = 40
@@ -49,40 +48,43 @@ type TerminalStringer interface {
49
48
50
49
func (h * TerminalHandler ) TerminalFormat (buf []byte , r slog.Record , usecolor bool ) []byte {
51
50
msg := escapeMessage (r .Message )
52
- var color = 0
51
+ var color = ""
53
52
if usecolor {
54
53
switch r .Level {
55
54
case LevelCrit :
56
- color = 35
55
+ color = " \x1b [35m"
57
56
case slog .LevelError :
58
- color = 31
57
+ color = " \x1b [31m"
59
58
case slog .LevelWarn :
60
- color = 33
59
+ color = " \x1b [33m"
61
60
case slog .LevelInfo :
62
- color = 32
61
+ color = " \x1b [32m"
63
62
case slog .LevelDebug :
64
- color = 36
63
+ color = " \x1b [36m"
65
64
case LevelTrace :
66
- color = 34
65
+ color = " \x1b [34m"
67
66
}
68
67
}
69
68
if buf == nil {
70
69
buf = make ([]byte , 0 , 30 + termMsgJust )
71
70
}
72
71
b := bytes .NewBuffer (buf )
73
- lvl := LevelAlignedString (r .Level )
74
- if color > 0 {
75
- // TODO improve this
76
- fmt .Fprintf (b , "\x1b [%dm%s\x1b [0m[%s] %s " , color , lvl , r .Time .Format (termTimeFormat ), msg )
72
+
73
+ if color != "" { // Start color
74
+ b .WriteString (color )
75
+ b .WriteString (LevelAlignedString (r .Level ))
76
+ b .WriteString ("\x1b [0m" )
77
77
} else {
78
- b .WriteString (lvl )
79
- b .WriteString ("[" )
80
- writeTimeTermFormat (b , r .Time )
81
- b .WriteString ("] " )
82
- b .WriteString (msg )
78
+ b .WriteString (LevelAlignedString (r .Level ))
83
79
}
80
+ b .WriteString ("[" )
81
+ writeTimeTermFormat (b , r .Time )
82
+ b .WriteString ("] " )
83
+ b .WriteString (msg )
84
+
84
85
// try to justify the log output for short messages
85
- length := utf8 .RuneCountInString (msg )
86
+ //length := utf8.RuneCountInString(msg)
87
+ length := len (msg )
86
88
if (r .NumAttrs ()+ len (h .attrs )) > 0 && length < termMsgJust {
87
89
b .Write (spaces [:termMsgJust - length ])
88
90
}
@@ -92,17 +94,18 @@ func (h *TerminalHandler) TerminalFormat(buf []byte, r slog.Record, usecolor boo
92
94
return b .Bytes ()
93
95
}
94
96
95
- func (h * TerminalHandler ) logfmt (buf * bytes.Buffer , r slog.Record , color int ) {
97
+ func (h * TerminalHandler ) logfmt (buf * bytes.Buffer , r slog.Record , color string ) {
96
98
writeAttr := func (attr slog.Attr , first , last bool ) {
97
99
//if !first {
98
100
buf .WriteByte (' ' )
99
101
//}
100
- key := escapeString (attr .Key )
101
- if color > 0 {
102
- // TODO improve this
103
- fmt .Fprintf (buf , "\x1b [%dm%s\x1b [0m=" , color , key )
102
+
103
+ if color != "" {
104
+ buf .WriteString (color )
105
+ buf .Write (appendEscapeString (buf .AvailableBuffer (), attr .Key ))
106
+ buf .WriteString ("\x1b [0m=" )
104
107
} else {
105
- buf .WriteString ( key )
108
+ buf .Write ( appendEscapeString ( buf . AvailableBuffer (), attr . Key ) )
106
109
buf .WriteByte ('=' )
107
110
}
108
111
tmp := buf .AvailableBuffer ()
@@ -302,34 +305,32 @@ func appendU256(dst []byte, n *uint256.Int) []byte {
302
305
// escaping/quoting if needed.
303
306
func appendEscapeString (dst []byte , s string ) []byte {
304
307
needsQuoting := false
308
+ needsEscaping := false
305
309
for _ , r := range s {
306
- // We quote everything below " (0x22) and above~ (0x7E), plus equal-sign
307
- if r <= '"' || r > '~ ' || r == '=' {
310
+ // If it contains spaces or equal-sign, we need to quote it.
311
+ if r == ' ' || r == '=' {
308
312
needsQuoting = true
309
- break
313
+ continue
310
314
}
311
- }
312
- if ! needsQuoting {
313
- return append (dst , []byte (s )... )
314
- }
315
- return strconv .AppendQuote (dst , s )
316
- }
317
-
318
- // escapeString checks if the provided string needs escaping/quoting, and
319
- // calls strconv.Quote if needed
320
- func escapeString (s string ) string {
321
- needsQuoting := false
322
- for _ , r := range s {
323
- // We quote everything below " (0x22) and above~ (0x7E), plus equal-sign
324
- if r <= '"' || r > '~' || r == '=' {
325
- needsQuoting = true
315
+ // We need to escape it, if it contains
316
+ // - character " (0x22) and lower (except space)
317
+ // - characters above ~ (0x7E), plus equal-sign
318
+ if r <= '"' || r > '~' {
319
+ needsEscaping = true
326
320
break
327
321
}
328
322
}
329
- if ! needsQuoting {
330
- return s
323
+ if needsEscaping {
324
+ return strconv . AppendQuote ( dst , s )
331
325
}
332
- return strconv .Quote (s )
326
+ // No escaping needed, but we might have to place within quote-marks, in case
327
+ // it contained a space
328
+ if needsQuoting {
329
+ dst = append (dst , '"' )
330
+ dst = append (dst , []byte (s )... )
331
+ return append (dst , '"' )
332
+ }
333
+ return append (dst , []byte (s )... )
333
334
}
334
335
335
336
// escapeMessage checks if the provided string needs escaping/quoting, similarly
@@ -355,7 +356,7 @@ func escapeMessage(s string) string {
355
356
return strconv .Quote (s )
356
357
}
357
358
358
- // writeTimeTermFormat
359
+ // writeTimeTermFormat writes on the format "01-02|15:04:05.000"
359
360
func writeTimeTermFormat (buf * bytes.Buffer , t time.Time ) {
360
361
_ , month , day := t .Date ()
361
362
writePosIntWidth (buf , int (month ), 2 )
0 commit comments