1111
1212import
1313 # Standard library
14- std/ [tables, strutils, terminal, typetraits],
14+ std/ [os, tables, strutils, terminal, typetraits],
1515
1616 # Nimble packages
1717 chronos, confutils, presto, toml_serialization, metrics,
@@ -38,45 +38,7 @@ export
3838
3939type
4040 SlotStartProc * [T] = proc (node: T, wallTime: BeaconTime ,
41- lastSlot: Slot ): Future [bool ] {.gcsafe ,
42- raises : [].}
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
41+ lastSlot: Slot ): Future [bool ] {.gcsafe , raises : [].}
8042
8143proc updateLogLevel * (logLevel: string ) {.raises : [ValueError ].} =
8244 # Updates log levels (without clearing old ones)
@@ -93,7 +55,7 @@ proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
9355
9456proc detectTTY * (stdoutKind: StdoutLogKind ): StdoutLogKind =
9557 if stdoutKind == StdoutLogKind .Auto :
96- if isatty (stdout):
58+ if getEnv ( " NO_COLOR " ).len == 0 and isatty (stdout):
9759 # On a TTY, let's be fancy
9860 StdoutLogKind .Colors
9961 else :
@@ -108,6 +70,8 @@ when defaultChroniclesStream.outputs.type.arity == 2:
10870 from std/ os import splitFile
10971 from " ." / filepath import secureCreatePath
11072
73+ import stew/ staticfor
74+
11175proc setupFileLimits * () =
11276 when not defined (windows):
11377 # In addition to databases and sockets, we need a file descriptor for every
@@ -129,9 +93,6 @@ proc setupLogging*(
12993 when defaultChroniclesStream.outputs.type .arity != 2 :
13094 warn " Logging configuration options not enabled in the current build"
13195 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-
13596 proc noOutput (logLevel: LogLevel , msg: LogOutputStr ) = discard
13697 proc writeAndFlush (f: File , msg: LogOutputStr ) =
13798 try :
@@ -143,9 +104,6 @@ proc setupLogging*(
143104 proc stdoutFlush (logLevel: LogLevel , msg: LogOutputStr ) =
144105 writeAndFlush (stdout, msg)
145106
146- proc noColorsFlush (logLevel: LogLevel , msg: LogOutputStr ) =
147- writeAndFlush (stdout, stripAnsi (msg))
148-
149107 let fileWriter =
150108 if logFile.isSome ():
151109 let
@@ -171,14 +129,15 @@ proc setupLogging*(
171129
172130 defaultChroniclesStream.outputs[1 ].writer = fileWriter
173131
174- let tmp = detectTTY (stdoutKind)
175-
176- case tmp
177- of StdoutLogKind .Auto : raiseAssert " checked above"
132+ case detectTTY (stdoutKind)
133+ of StdoutLogKind .Auto :
134+ raiseAssert " Auto-detection done in detectTTY"
178135 of StdoutLogKind .Colors :
179136 defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
137+ defaultChroniclesStream.outputs[0 ].colors = true
180138 of StdoutLogKind .NoColors :
181- defaultChroniclesStream.outputs[0 ].writer = noColorsFlush
139+ defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
140+ defaultChroniclesStream.outputs[0 ].colors = false
182141 of StdoutLogKind .Json :
183142 defaultChroniclesStream.outputs[0 ].writer = noOutput
184143
@@ -190,6 +149,9 @@ proc setupLogging*(
190149 of StdoutLogKind .None :
191150 defaultChroniclesStream.outputs[0 ].writer = noOutput
192151
152+ staticFor i, 0 ..< defaultChroniclesStream.outputs.type .arity:
153+ setLogEnabled (defaultChroniclesStream.outputs[i].writer != noOutput, i)
154+
193155 if logFile.isSome ():
194156 warn " The --log-file option is deprecated. Consider redirecting the standard output to a file instead"
195157 try :
0 commit comments