11
11
12
12
import
13
13
# Standard library
14
- std/ [tables, strutils, terminal, typetraits],
14
+ std/ [os, tables, strutils, terminal, typetraits],
15
15
16
16
# Nimble packages
17
+ stew/ staticfor,
17
18
chronos, confutils, presto, toml_serialization, metrics,
18
19
chronicles, chronicles/ helpers as chroniclesHelpers, chronicles/ topics_registry,
19
20
stew/ io2, metrics/ chronos_httpserver,
@@ -38,51 +39,18 @@ export
38
39
39
40
type
40
41
SlotStartProc * [T] = proc (node: T, wallTime: BeaconTime ,
41
- lastSlot: Slot ): Future [bool ] {.gcsafe ,
42
- raises : [].}
42
+ lastSlot: Slot ): Future [bool ] {.gcsafe , raises : [].}
43
43
44
- # silly chronicles, colors is a compile-time property
45
- when defaultChroniclesStream.outputs.type .arity == 2 :
46
- func stripAnsi (v: string ): string =
47
- var
48
- res = newStringOfCap (v.len)
49
- i: int
50
-
51
- while i < v.len:
52
- let c = v[i]
53
- if c == '\x1b ' :
54
- var
55
- x = i + 1
56
- found = false
57
-
58
- while x < v.len: # look for [..m
59
- let c2 = v[x]
60
- if x == i + 1 :
61
- if c2 != '[' :
62
- break
63
- else :
64
- if c2 in {'0' .. '9' } + {';' }:
65
- discard # keep looking
66
- elif c2 == 'm' :
67
- i = x + 1
68
- found = true
69
- break
70
- else :
71
- break
72
- inc x
73
-
74
- if found: # skip adding c
75
- continue
76
- res.add c
77
- inc i
78
-
79
- res
44
+ proc noOutput (logLevel: LogLevel , msg: LogOutputStr ) = discard
80
45
81
46
proc updateLogLevel * (logLevel: string ) {.raises : [ValueError ].} =
82
47
# Updates log levels (without clearing old ones)
83
48
let directives = logLevel.split (" ;" )
84
49
try :
85
- setLogLevel (parseEnum [LogLevel ](directives[0 ].capitalizeAscii ()))
50
+ let level = parseEnum [LogLevel ](directives[0 ].capitalizeAscii ())
51
+ staticFor i, 0 ..< defaultChroniclesStream.outputs.type .arity:
52
+ if defaultChroniclesStream.outputs[i].writer != noOutput:
53
+ setLogLevel (level, i)
86
54
except ValueError :
87
55
raise (ref ValueError )(msg: " Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL" )
88
56
@@ -93,7 +61,7 @@ proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
93
61
94
62
proc detectTTY * (stdoutKind: StdoutLogKind ): StdoutLogKind =
95
63
if stdoutKind == StdoutLogKind .Auto :
96
- if isatty (stdout):
64
+ if getEnv ( " NO_COLOR " ).len == 0 and isatty (stdout):
97
65
# On a TTY, let's be fancy
98
66
StdoutLogKind .Colors
99
67
else :
@@ -129,10 +97,6 @@ proc setupLogging*(
129
97
when defaultChroniclesStream.outputs.type .arity != 2 :
130
98
warn " Logging configuration options not enabled in the current build"
131
99
else :
132
- # Naive approach where chronicles will form a string and we will discard
133
- # it, even if it could have skipped the formatting phase
134
-
135
- proc noOutput (logLevel: LogLevel , msg: LogOutputStr ) = discard
136
100
proc writeAndFlush (f: File , msg: LogOutputStr ) =
137
101
try :
138
102
f.write (msg)
@@ -143,9 +107,6 @@ proc setupLogging*(
143
107
proc stdoutFlush (logLevel: LogLevel , msg: LogOutputStr ) =
144
108
writeAndFlush (stdout, msg)
145
109
146
- proc noColorsFlush (logLevel: LogLevel , msg: LogOutputStr ) =
147
- writeAndFlush (stdout, stripAnsi (msg))
148
-
149
110
let fileWriter =
150
111
if logFile.isSome ():
151
112
let
@@ -171,14 +132,15 @@ proc setupLogging*(
171
132
172
133
defaultChroniclesStream.outputs[1 ].writer = fileWriter
173
134
174
- let tmp = detectTTY (stdoutKind)
175
-
176
- case tmp
177
- of StdoutLogKind .Auto : raiseAssert " checked above"
135
+ case detectTTY (stdoutKind)
136
+ of StdoutLogKind .Auto :
137
+ raiseAssert " Auto-detection done in detectTTY"
178
138
of StdoutLogKind .Colors :
179
139
defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
140
+ defaultChroniclesStream.outputs[0 ].colors = true
180
141
of StdoutLogKind .NoColors :
181
- defaultChroniclesStream.outputs[0 ].writer = noColorsFlush
142
+ defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
143
+ defaultChroniclesStream.outputs[0 ].colors = false
182
144
of StdoutLogKind .Json :
183
145
defaultChroniclesStream.outputs[0 ].writer = noOutput
184
146
@@ -190,6 +152,9 @@ proc setupLogging*(
190
152
of StdoutLogKind .None :
191
153
defaultChroniclesStream.outputs[0 ].writer = noOutput
192
154
155
+ staticFor i, 0 ..< defaultChroniclesStream.outputs.type .arity:
156
+ setLogEnabled (defaultChroniclesStream.outputs[i].writer != noOutput, i)
157
+
193
158
if logFile.isSome ():
194
159
warn " The --log-file option is deprecated. Consider redirecting the standard output to a file instead"
195
160
try :
0 commit comments