2022-12-07 11:30:32 +00:00
|
|
|
## This code has been copied and addapted from `status-im/nimbu-eth2` project.
|
|
|
|
## Link: https://github.com/status-im/nimbus-eth2/blob/c585b0a5b1ae4d55af38ad7f4715ad455e791552/beacon_chain/nimbus_binary_common.nim
|
2024-05-17 14:28:54 +00:00
|
|
|
import
|
|
|
|
std/[typetraits, os, strutils],
|
|
|
|
chronicles,
|
|
|
|
chronicles/log_output,
|
|
|
|
chronicles/topics_registry
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2023-11-06 11:30:34 +00:00
|
|
|
export chronicles.LogLevel
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
type LogFormat* = enum
|
|
|
|
TEXT
|
|
|
|
JSON
|
2022-12-07 11:30:32 +00:00
|
|
|
|
|
|
|
## Utils
|
|
|
|
|
|
|
|
proc stripAnsi(v: string): string =
|
|
|
|
## Copied from: https://github.com/status-im/nimbus-eth2/blob/stable/beacon_chain/nimbus_binary_common.nim#L41
|
|
|
|
## Silly chronicles, colors is a compile-time property
|
|
|
|
var
|
|
|
|
res = newStringOfCap(v.len)
|
|
|
|
i: int
|
|
|
|
|
|
|
|
while i < v.len:
|
|
|
|
let c = v[i]
|
|
|
|
if c == '\x1b':
|
|
|
|
var
|
|
|
|
x = i + 1
|
|
|
|
found = false
|
|
|
|
|
|
|
|
while x < v.len: # look for [..m
|
|
|
|
let c2 = v[x]
|
|
|
|
if x == i + 1:
|
|
|
|
if c2 != '[':
|
|
|
|
break
|
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
if c2 in {'0' .. '9'} + {';'}:
|
2022-12-07 11:30:32 +00:00
|
|
|
discard # keep looking
|
|
|
|
elif c2 == 'm':
|
|
|
|
i = x + 1
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc x
|
|
|
|
|
|
|
|
if found: # skip adding c
|
|
|
|
continue
|
|
|
|
res.add c
|
|
|
|
inc i
|
|
|
|
|
|
|
|
res
|
|
|
|
|
|
|
|
proc writeAndFlush(f: File, s: LogOutputStr) =
|
|
|
|
try:
|
|
|
|
f.write(s)
|
|
|
|
f.flushFile()
|
2023-04-04 13:34:53 +00:00
|
|
|
except CatchableError:
|
2022-12-07 11:30:32 +00:00
|
|
|
logLoggingFailure(cstring(s), getCurrentException())
|
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
2024-05-17 14:28:54 +00:00
|
|
|
proc setupLogLevel(level: LogLevel) =
|
2022-12-07 11:30:32 +00:00
|
|
|
# TODO: Support per topic level configuratio
|
|
|
|
topics_registry.setLogLevel(level)
|
|
|
|
|
2024-05-17 14:28:54 +00:00
|
|
|
proc setupLogFormat(format: LogFormat, color = true) =
|
2024-03-15 23:08:47 +00:00
|
|
|
proc noOutputWriter(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
discard
|
2022-12-07 11:30:32 +00:00
|
|
|
|
2023-11-06 11:30:34 +00:00
|
|
|
proc stdoutOutputWriter(logLevel: LogLevel, msg: LogOutputStr) =
|
2022-12-07 11:30:32 +00:00
|
|
|
writeAndFlush(io.stdout, msg)
|
|
|
|
|
2023-11-06 11:30:34 +00:00
|
|
|
proc stdoutNoColorOutputWriter(logLevel: LogLevel, msg: LogOutputStr) =
|
2022-12-07 11:30:32 +00:00
|
|
|
writeAndFlush(io.stdout, stripAnsi(msg))
|
|
|
|
|
|
|
|
when defaultChroniclesStream.outputs.type.arity == 2:
|
2024-03-15 23:08:47 +00:00
|
|
|
case format
|
2022-12-07 11:30:32 +00:00
|
|
|
of LogFormat.Text:
|
2024-03-15 23:08:47 +00:00
|
|
|
defaultChroniclesStream.outputs[0].writer =
|
|
|
|
if color: stdoutOutputWriter else: stdoutNoColorOutputWriter
|
2022-12-07 11:30:32 +00:00
|
|
|
defaultChroniclesStream.outputs[1].writer = noOutputWriter
|
|
|
|
of LogFormat.Json:
|
|
|
|
defaultChroniclesStream.outputs[0].writer = noOutputWriter
|
|
|
|
defaultChroniclesStream.outputs[1].writer = stdoutOutputWriter
|
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
{.
|
|
|
|
warning:
|
|
|
|
"the present module should be compiled with '-d:chronicles_default_output_device=dynamic' " &
|
|
|
|
"and '-d:chronicles_sinks=\"textlines,json\"' options"
|
|
|
|
.}
|
2024-05-17 14:28:54 +00:00
|
|
|
|
|
|
|
proc setupLog*(level: LogLevel, format: LogFormat) =
|
|
|
|
## Logging setup
|
|
|
|
# Adhere to NO_COLOR initiative: https://no-color.org/
|
|
|
|
let color =
|
|
|
|
try:
|
|
|
|
not parseBool(os.getEnv("NO_COLOR", "false"))
|
|
|
|
except CatchableError:
|
|
|
|
true
|
|
|
|
|
|
|
|
setupLogLevel(level)
|
|
|
|
setupLogFormat(format, color)
|