logging: non-debug costless, avoid allocating constant string messages
This commit is contained in:
parent
c26c751f9b
commit
05703fc86f
|
@ -7,29 +7,36 @@
|
|||
|
||||
import strformat, terminal
|
||||
|
||||
# TODO replace by nim-chronicles
|
||||
|
||||
type
|
||||
Logger* = object
|
||||
name*: string
|
||||
|
||||
var DEBUG* = defined(nimbusdebug)
|
||||
const DEBUG* = defined(nimbusdebug)
|
||||
|
||||
proc log*(l: Logger, msg: string, color: ForegroundColor = fgBlack) =
|
||||
if DEBUG:
|
||||
# Note: to make logging costless:
|
||||
# - DEBUG should be a const and dispatch should use `when` for compile-time specialization
|
||||
# - Passing a string to a proc, even a const string and inline proc, might trigger a heap allocation
|
||||
# use a template instead.
|
||||
|
||||
template log*(l: Logger, msg: string, color: ForegroundColor = fgBlack) =
|
||||
when DEBUG:
|
||||
styledWriteLine(stdout, color, &"#{l.name}: {msg}", resetStyle)
|
||||
|
||||
proc debug*(l: Logger, msg: string) =
|
||||
if DEBUG:
|
||||
template debug*(l: Logger, msg: string) =
|
||||
when DEBUG:
|
||||
l.log(msg)
|
||||
|
||||
proc trace*(l: Logger, msg: string) =
|
||||
if DEBUG:
|
||||
template trace*(l: Logger, msg: string) =
|
||||
when DEBUG:
|
||||
l.log(msg, fgBlue)
|
||||
|
||||
proc getLogger*(name: string): Logger =
|
||||
result = Logger(name: name)
|
||||
|
||||
proc disableLogging* =
|
||||
DEBUG = false
|
||||
# proc disableLogging* =
|
||||
# DEBUG = false
|
||||
|
||||
proc enableLogging* =
|
||||
DEBUG = true
|
||||
# proc enableLogging* =
|
||||
# DEBUG = true
|
||||
|
|
|
@ -12,7 +12,7 @@ import unittest, macros, strformat,
|
|||
# TODO: quicktest
|
||||
# PS: parametrize can be easily immitated, but still quicktests would be even more useful
|
||||
|
||||
disableLogging()
|
||||
# disableLogging()
|
||||
|
||||
proc gasMeters: seq[GasMeter] =
|
||||
@[newGasMeter(10), newGasMeter(100), newGasMeter(999)]
|
||||
|
|
Loading…
Reference in New Issue