Skip to content

Commit aa88cd4

Browse files
authored
chronicles: bump and logging updates (#7319)
* chronicles: bump and logging updates * formatting uses `formatIt` also for object fields * respect [`NO_COLOR`](https://no-color.org/) * less memory allocation per log statement * outputs fully disabled when not used (instead of being formatted and then discarded) * directly produce nocolored output instead of coloring then filtering * bump * oops * oops * better * bump * move nooutput back * move nooutput back * move import * setup logging before file limits * bump
1 parent 0f8ec2d commit aa88cd4

9 files changed

+22
-60
lines changed

beacon_chain/nimbus_beacon_node.nim.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
22
-d:"chronicles_runtime_filtering=on"
3-
-d:"chronicles_disable_thread_id"
3+
-d:"chronicles_thread_ids=no"
44

55
@if release:
66
-d:"chronicles_line_numbers:0"

beacon_chain/nimbus_binary_common.nim

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import
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

3939
type
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

8143
proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
8244
# Updates log levels (without clearing old ones)
@@ -93,7 +55,7 @@ proc updateLogLevel*(logLevel: string) {.raises: [ValueError].} =
9355

9456
proc 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+
11175
proc 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:

beacon_chain/nimbus_light_client.nim.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
22
-d:"chronicles_runtime_filtering=on"
3-
-d:"chronicles_disable_thread_id"
3+
-d:"chronicles_thread_ids=no"
44

55
@if release:
66
-d:"chronicles_line_numbers:0"

beacon_chain/nimbus_signing_node.nim.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
22
-d:"chronicles_runtime_filtering=on"
3-
-d:"chronicles_disable_thread_id"
3+
-d:"chronicles_thread_ids=no"
44

55
@if release:
66
-d:"chronicles_line_numbers:0"

beacon_chain/nimbus_validator_client.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,6 @@ programMain:
571571
# and avoid using system resources (such as urandom) after that
572572
rng = HmacDrbgContext.new()
573573

574-
setupFileLimits()
575574
setupLogging(config.logLevel, config.logStdout, config.logFile)
575+
setupFileLimits()
576576
waitFor runValidatorClient(config, rng)

beacon_chain/nimbus_validator_client.nim.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-d:"chronicles_sinks=textlines[dynamic],json[dynamic]"
22
-d:"chronicles_runtime_filtering=on"
3-
-d:"chronicles_disable_thread_id"
3+
-d:"chronicles_thread_ids=no"
44

55
@if release:
66
-d:"chronicles_line_numbers:0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-d:"chronicles_runtime_filtering=on"
2-
-d:"chronicles_disable_thread_id"
2+
-d:"chronicles_thread_ids=no"

vendor/nim-chronicles

Submodule nim-chronicles updated 93 files

vendor/nim-eth

0 commit comments

Comments
 (0)