Cherry pick the best parts of #45

This commit is contained in:
Zahary Karadjov 2019-03-24 23:57:13 +02:00
parent 2079744de6
commit 3586140622
9 changed files with 51 additions and 73 deletions

View File

@ -346,6 +346,10 @@ proc setTopicState*(name: string,
logLevel = LogLevel.NONE): bool
```
The log levels available at runtime - and therefor to `setLogLevel()` - are
those greater than or equal to the one set at compile time by
`chronicles_log_level`.
It is also possible for a specific topic to overrule the global `LogLevel`, set
by `setLogLevel`, by setting the optional `logLevel` parameter in
`setTopicState` to a valid `LogLevel`.

View File

@ -113,7 +113,7 @@ when runtimeFilteringEnabled:
proc runtimeTopicFilteringCode*(logLevel: LogLevel, topics: seq[string]): NimNode =
# This proc generates the run-time code used for topic filtering.
# Each logging statement has a statically known list of associated topics.
# For each of the topics in the list, we consult a TLS TopicState value
# For each of the topics in the list, we consult a global TopicState value
# created in topicStateIMPL. `break chroniclesLogStmt` exits a named
# block surrounding the entire log statement.
result = newStmtList()
@ -340,21 +340,11 @@ template log*(stream: type,
bindSym("activeChroniclesScope", brForceOpen), props)
template logFn(name, severity) {.dirty.} =
template `name`*(eventName: static[string],
props: varargs[untyped]) {.dirty.} =
template `name`*(eventName: static[string], props: varargs[untyped]) {.dirty.} =
log(severity, eventName, props)
bind logIMPL, bindSym, brForceOpen
logIMPL(instantiationInfo(), activeChroniclesStream(),
activeChroniclesStream().Record, eventName, severity,
bindSym("activeChroniclesScope", brForceOpen), props)
template `name`*(stream: type,
eventName: static[string],
props: varargs[untyped]) {.dirty.} =
bind logIMPL, bindSym, brForceOpen
logIMPL(instantiationInfo(), stream, stream.Record, eventName, severity,
bindSym("activeChroniclesScope", brForceOpen), props)
template `name`*(stream: type, eventName: static[string], props: varargs[untyped]) {.dirty.} =
log(stream, severity, eventName, props)
logFn trace , LogLevel.TRACE
logFn debug , LogLevel.DEBUG

View File

@ -90,6 +90,13 @@ proc open*(o: var FileOutput, path: string, mode = fmAppend): bool =
o.outPath = path
o.mode = mode
proc open*(o: var FileOutput, file: File): bool =
if o.outFile != nil:
close(o.outFile)
o.outPath = ""
o.outFile = file
proc openOutput(o: var FileOutput) =
doAssert o.outPath.len > 0 and o.mode != fmRead
createDir o.outPath.splitFile.dir

View File

@ -66,7 +66,7 @@ proc topicsMatch*(logStmtLevel: LogLevel,
var
hasEnabledTopics = gTotalEnabledTopics > 0
enabledTopicsMatch = false
normalTopicsMatch = logStmtTopics.len == 0
normalTopicsMatch = logStmtTopics.len == 0 and logStmtLevel >= gActiveLogLevel
requiredTopicsCount = gTotalRequiredTopics
for topic in logStmtTopics:

View File

@ -1,19 +0,0 @@
program=topics_and_loglvls_threads
chronicles_sinks="textlines[stdout]"
chronicles_colors=None
chronicles_timestamps=None
chronicles_line_numbers=on
[Output]
stdout="""WRN inside main topics="main" tid=2588 file=topics_and_loglvls_threads.nim:9 a=1 arg=50 b=10
INF inside main topics="main" tid=2588 file=topics_and_loglvls_threads.nim:10 a=1 arg=50 b=10
DBG inside main topics="main" tid=2588 file=topics_and_loglvls_threads.nim:11 a=1 arg=50 b=10
WRN inside foo topics="foo" tid=2588 file=topics_and_loglvls_threads.nim:18 arg=10 b=10
INF inside foo topics="foo" tid=2588 file=topics_and_loglvls_threads.nim:19 arg=10 b=10
DBG inside foo topics="foo" tid=2588 file=topics_and_loglvls_threads.nim:20 arg=10 b=10
WRN inside foobar topics="foo bar" tid=2588 file=topics_and_loglvls_threads.nim:27 arg=20 b=10
INF inside foobar topics="foo bar" tid=2588 file=topics_and_loglvls_threads.nim:28 arg=20 b=10
DBG inside foobar topics="foo bar" tid=2588 file=topics_and_loglvls_threads.nim:29 arg=20 b=10
INF after main topics="general" tid=2588 file=topics_and_loglvls_threads.nim:35
INF exiting tid=2588 file=topics_and_loglvls_threads.nim:36 msg="bye bye"
"""

View File

@ -0,0 +1,16 @@
program="runtime_filtering_no_scopes"
chronicles_sinks="textlines[stdout,file]"
chronicles_runtime_filtering="on"
chronicles_colors=None
chronicles_timestamps=None
[Output]
stdout="""> start by printing both:
INF from foo tid=0
INF from bar tid=0
> set global log level to WARN; info() is now disabled:
"""
runtime_filtering_no_scopes.log="""INF from foo tid=0
INF from bar tid=0
"""

View File

@ -0,0 +1,18 @@
import chronicles
proc foo =
info "from foo"
proc bar =
info "from bar"
echo "> start by printing both:"
foo()
bar()
echo "> set global log level to WARN; info() is now disabled:"
setLogLevel(WARN)
foo()
bar()

View File

@ -1,36 +0,0 @@
import chronicles
proc main(arg: int) =
logScope:
topics = "main"
arg
a = 1
warn("inside main", b = 10)
info("inside main", b = 10)
debug("inside main", b = 10)
proc foo(arg: int) =
logScope:
topics = "foo"
arg
warn("inside foo", b = 10)
info("inside foo", b = 10)
debug("inside foo", b = 10)
proc foobar(arg: int) =
logScope:
topics = "foo bar"
arg
warn("inside foobar", b = 10)
info("inside foobar", b = 10)
debug("inside foobar", b = 10)
main(50)
foo(10)
foobar(20)
info("after main", topics = "general")
info("exiting", msg = "bye bye")

View File

@ -1,2 +0,0 @@
--threads:on