1111
1212import
1313 # Standard library
14- std/ [tables, strutils, terminal, typetraits],
14+ std/ [os, tables, strutils, terminal, typetraits],
1515
1616 # Nimble packages
17+ stew/ staticfor,
1718 chronos, confutils, presto, toml_serialization, metrics,
1819 chronicles, chronicles/ helpers as chroniclesHelpers, chronicles/ topics_registry,
1920 stew/ io2, metrics/ chronos_httpserver,
@@ -38,51 +39,18 @@ export
3839
3940type
4041 SlotStartProc * [T] = proc (node: T, wallTime: BeaconTime ,
41- lastSlot: Slot ): Future [bool ] {.gcsafe ,
42- raises : [].}
42+ lastSlot: Slot ): Future [bool ] {.gcsafe , raises : [].}
4343
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
8045
8146proc updateLogLevel * (logLevel: string ) {.raises : [ValueError ].} =
8247 # Updates log levels (without clearing old ones)
8348 let directives = logLevel.split (" ;" )
8449 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)
8654 except ValueError :
8755 raise (ref ValueError )(msg: " Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL" )
8856
@@ -93,7 +61,7 @@ proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
9361
9462proc detectTTY * (stdoutKind: StdoutLogKind ): StdoutLogKind =
9563 if stdoutKind == StdoutLogKind .Auto :
96- if isatty (stdout):
64+ if getEnv ( " NO_COLOR " ).len == 0 and isatty (stdout):
9765 # On a TTY, let's be fancy
9866 StdoutLogKind .Colors
9967 else :
@@ -129,10 +97,6 @@ proc setupLogging*(
12997 when defaultChroniclesStream.outputs.type .arity != 2 :
13098 warn " Logging configuration options not enabled in the current build"
13199 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
136100 proc writeAndFlush (f: File , msg: LogOutputStr ) =
137101 try :
138102 f.write (msg)
@@ -143,9 +107,6 @@ proc setupLogging*(
143107 proc stdoutFlush (logLevel: LogLevel , msg: LogOutputStr ) =
144108 writeAndFlush (stdout, msg)
145109
146- proc noColorsFlush (logLevel: LogLevel , msg: LogOutputStr ) =
147- writeAndFlush (stdout, stripAnsi (msg))
148-
149110 let fileWriter =
150111 if logFile.isSome ():
151112 let
@@ -171,14 +132,15 @@ proc setupLogging*(
171132
172133 defaultChroniclesStream.outputs[1 ].writer = fileWriter
173134
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"
178138 of StdoutLogKind .Colors :
179139 defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
140+ defaultChroniclesStream.outputs[0 ].colors = true
180141 of StdoutLogKind .NoColors :
181- defaultChroniclesStream.outputs[0 ].writer = noColorsFlush
142+ defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
143+ defaultChroniclesStream.outputs[0 ].colors = false
182144 of StdoutLogKind .Json :
183145 defaultChroniclesStream.outputs[0 ].writer = noOutput
184146
@@ -190,6 +152,9 @@ proc setupLogging*(
190152 of StdoutLogKind .None :
191153 defaultChroniclesStream.outputs[0 ].writer = noOutput
192154
155+ staticFor i, 0 ..< defaultChroniclesStream.outputs.type .arity:
156+ setLogEnabled (defaultChroniclesStream.outputs[i].writer != noOutput, i)
157+
193158 if logFile.isSome ():
194159 warn " The --log-file option is deprecated. Consider redirecting the standard output to a file instead"
195160 try :
0 commit comments