Propper logging options (#81)
This commit is contained in:
parent
1a3003e043
commit
bade020d67
|
@ -66,4 +66,4 @@ switch("warning", "ObservableStores:off")
|
||||||
|
|
||||||
# Too many false positives for "Warning: method has lock level <unknown>, but another method has 0 [LockLevel]"
|
# Too many false positives for "Warning: method has lock level <unknown>, but another method has 0 [LockLevel]"
|
||||||
switch("warning", "LockLevel:off")
|
switch("warning", "LockLevel:off")
|
||||||
|
switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic]")
|
||||||
|
|
|
@ -27,8 +27,8 @@ when isMainModule:
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
import system/ansi_c
|
import system/ansi_c
|
||||||
|
|
||||||
let
|
let config = DaggerConf.load()
|
||||||
config = DaggerConf.load()
|
config.setupLogging()
|
||||||
|
|
||||||
case config.cmd:
|
case config.cmd:
|
||||||
of StartUpCommand.noCommand:
|
of StartUpCommand.noCommand:
|
||||||
|
|
|
@ -12,9 +12,12 @@ import pkg/upraises
|
||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
import std/os
|
import std/os
|
||||||
|
import std/terminal
|
||||||
import std/options
|
import std/options
|
||||||
|
import std/typetraits
|
||||||
|
|
||||||
import pkg/chronicles
|
import pkg/chronicles
|
||||||
|
import pkg/chronicles/topics_registry
|
||||||
import pkg/confutils/defs
|
import pkg/confutils/defs
|
||||||
import pkg/libp2p
|
import pkg/libp2p
|
||||||
|
|
||||||
|
@ -30,10 +33,26 @@ type
|
||||||
noCommand,
|
noCommand,
|
||||||
initNode
|
initNode
|
||||||
|
|
||||||
|
LogKind* = enum
|
||||||
|
Auto = "auto"
|
||||||
|
Colors = "colors"
|
||||||
|
NoColors = "nocolors"
|
||||||
|
Json = "json"
|
||||||
|
None = "none"
|
||||||
|
|
||||||
DaggerConf* = object
|
DaggerConf* = object
|
||||||
logLevel* {.
|
logLevel* {.
|
||||||
defaultValue: LogLevel.INFO
|
defaultValue: LogLevel.INFO
|
||||||
desc: "Sets the log level" }: LogLevel
|
desc: "Sets the log level",
|
||||||
|
name: "log-level" }: LogLevel
|
||||||
|
|
||||||
|
logFormat* {.
|
||||||
|
hidden
|
||||||
|
desc: "Specifies what kind of logs should be written to stdout (auto, colors, nocolors, json)"
|
||||||
|
defaultValueDesc: "auto"
|
||||||
|
defaultValue: LogKind.Auto
|
||||||
|
name: "log-format" }: LogKind
|
||||||
|
|
||||||
|
|
||||||
dataDir* {.
|
dataDir* {.
|
||||||
desc: "The directory where dagger will store configuration and data."
|
desc: "The directory where dagger will store configuration and data."
|
||||||
|
@ -99,3 +118,76 @@ proc defaultDataDir*(): string =
|
||||||
func parseCmdArg*(T: type MultiAddress, input: TaintedString): T
|
func parseCmdArg*(T: type MultiAddress, input: TaintedString): T
|
||||||
{.raises: [ValueError, LPError, Defect].} =
|
{.raises: [ValueError, LPError, Defect].} =
|
||||||
MultiAddress.init($input).tryGet()
|
MultiAddress.init($input).tryGet()
|
||||||
|
|
||||||
|
# silly chronicles, colors is a compile-time property
|
||||||
|
proc stripAnsi(v: string): string =
|
||||||
|
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:
|
||||||
|
if c2 in {'0'..'9'} + {';'}:
|
||||||
|
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 setupLogging*(conf: DaggerConf) =
|
||||||
|
when defaultChroniclesStream.outputs.type.arity != 2:
|
||||||
|
warn "Logging configuration options not enabled in the current build"
|
||||||
|
else:
|
||||||
|
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
|
||||||
|
proc writeAndFlush(f: File, msg: LogOutputStr) =
|
||||||
|
try:
|
||||||
|
f.write(msg)
|
||||||
|
f.flushFile()
|
||||||
|
except IOError as err:
|
||||||
|
logLoggingFailure(cstring(msg), err)
|
||||||
|
|
||||||
|
proc stdoutFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
||||||
|
writeAndFlush(stdout, msg)
|
||||||
|
|
||||||
|
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
||||||
|
writeAndFlush(stdout, stripAnsi(msg))
|
||||||
|
|
||||||
|
defaultChroniclesStream.outputs[1].writer = noOutput
|
||||||
|
|
||||||
|
defaultChroniclesStream.outputs[0].writer =
|
||||||
|
case conf.logFormat:
|
||||||
|
of LogKind.Auto:
|
||||||
|
if isatty(stdout):
|
||||||
|
stdoutFlush
|
||||||
|
else:
|
||||||
|
noColorsFlush
|
||||||
|
of LogKind.Colors: stdoutFlush
|
||||||
|
of LogKind.NoColors: noColorsFlush
|
||||||
|
of LogKind.Json:
|
||||||
|
defaultChroniclesStream.outputs[1].writer = stdoutFlush
|
||||||
|
noOutput
|
||||||
|
of LogKind.None:
|
||||||
|
noOutput
|
||||||
|
|
||||||
|
setLogLevel(conf.logLevel)
|
||||||
|
|
Loading…
Reference in New Issue