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
17
chronos, confutils, presto, toml_serialization, metrics,
@@ -38,45 +38,7 @@ export
38
38
39
39
type
40
40
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 : [].}
80
42
81
43
proc updateLogLevel * (logLevel: string ) {.raises : [ValueError ].} =
82
44
# Updates log levels (without clearing old ones)
@@ -93,7 +55,7 @@ proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
93
55
94
56
proc detectTTY * (stdoutKind: StdoutLogKind ): StdoutLogKind =
95
57
if stdoutKind == StdoutLogKind .Auto :
96
- if isatty (stdout):
58
+ if getEnv ( " NO_COLOR " ).len == 0 and isatty (stdout):
97
59
# On a TTY, let's be fancy
98
60
StdoutLogKind .Colors
99
61
else :
@@ -108,6 +70,8 @@ when defaultChroniclesStream.outputs.type.arity == 2:
108
70
from std/ os import splitFile
109
71
from " ." / filepath import secureCreatePath
110
72
73
+ import stew/ staticfor
74
+
111
75
proc setupFileLimits * () =
112
76
when not defined (windows):
113
77
# In addition to databases and sockets, we need a file descriptor for every
@@ -129,9 +93,6 @@ proc setupLogging*(
129
93
when defaultChroniclesStream.outputs.type .arity != 2 :
130
94
warn " Logging configuration options not enabled in the current build"
131
95
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
96
proc noOutput (logLevel: LogLevel , msg: LogOutputStr ) = discard
136
97
proc writeAndFlush (f: File , msg: LogOutputStr ) =
137
98
try :
@@ -143,9 +104,6 @@ proc setupLogging*(
143
104
proc stdoutFlush (logLevel: LogLevel , msg: LogOutputStr ) =
144
105
writeAndFlush (stdout, msg)
145
106
146
- proc noColorsFlush (logLevel: LogLevel , msg: LogOutputStr ) =
147
- writeAndFlush (stdout, stripAnsi (msg))
148
-
149
107
let fileWriter =
150
108
if logFile.isSome ():
151
109
let
@@ -171,14 +129,15 @@ proc setupLogging*(
171
129
172
130
defaultChroniclesStream.outputs[1 ].writer = fileWriter
173
131
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"
178
135
of StdoutLogKind .Colors :
179
136
defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
137
+ defaultChroniclesStream.outputs[0 ].colors = true
180
138
of StdoutLogKind .NoColors :
181
- defaultChroniclesStream.outputs[0 ].writer = noColorsFlush
139
+ defaultChroniclesStream.outputs[0 ].writer = stdoutFlush
140
+ defaultChroniclesStream.outputs[0 ].colors = false
182
141
of StdoutLogKind .Json :
183
142
defaultChroniclesStream.outputs[0 ].writer = noOutput
184
143
@@ -190,6 +149,9 @@ proc setupLogging*(
190
149
of StdoutLogKind .None :
191
150
defaultChroniclesStream.outputs[0 ].writer = noOutput
192
151
152
+ staticFor i, 0 ..< defaultChroniclesStream.outputs.type .arity:
153
+ setLogEnabled (defaultChroniclesStream.outputs[i].writer != noOutput, i)
154
+
193
155
if logFile.isSome ():
194
156
warn " The --log-file option is deprecated. Consider redirecting the standard output to a file instead"
195
157
try :
0 commit comments