[#17] implement compile time topic log level filter
This commit is contained in:
parent
056b22de9f
commit
5cf32a7d1c
|
@ -295,6 +295,13 @@ When the list includes multiple topics, any of them is considered a match.
|
|||
> In both contexts, the list of topics is written as a comma or space-separated
|
||||
string of case-sensitive topic names.
|
||||
|
||||
In the list of topics, you can also optionally provide a log level after the
|
||||
topic, separated with a colon from the topic. If a log level is provided it will
|
||||
overrule the `chronicles_log_level` setting. The log level can be defined as
|
||||
`LogLevel` values or directly as the corresponding integer values.
|
||||
|
||||
e.g. `-d:chronicles_enabled_topics:MyTopic:DEBUG,AnotherTopic:5`
|
||||
|
||||
### chronicles_required_topics
|
||||
|
||||
Similar to `chronicles_enabled_topics`, but requires the logging statements
|
||||
|
|
|
@ -165,7 +165,7 @@ macro logIMPL(lineInfo: static InstInfo,
|
|||
severity: static[LogLevel],
|
||||
scopes: typed,
|
||||
logStmtBindings: varargs[untyped]): untyped =
|
||||
if not loggingEnabled or severity < enabledLogLevel: return
|
||||
if not loggingEnabled: return
|
||||
clearEmptyVarargs logStmtBindings
|
||||
|
||||
# First, we merge the lexical bindings with the additional
|
||||
|
@ -184,6 +184,7 @@ macro logIMPL(lineInfo: static InstInfo,
|
|||
# This is the compile-time topic filtering code, which has a similar
|
||||
# logic to the generated run-time filtering code:
|
||||
var enabledTopicsMatch = enabledTopics.len == 0
|
||||
var enabledTopicsLevel = enabledLogLevel
|
||||
var requiredTopicsCount = requiredTopics.len
|
||||
var topicsNode = newLit("")
|
||||
var activeTopics: seq[string] = @[]
|
||||
|
@ -196,15 +197,19 @@ macro logIMPL(lineInfo: static InstInfo,
|
|||
if topicsNode.kind notin {nnkStrLit, nnkTripleStrLit}:
|
||||
error "Please specify the 'topics' list as a space separated string literal", topicsNode
|
||||
|
||||
activeTopics = topicsNode.strVal.split(Whitespace)
|
||||
activeTopics = topicsNode.strVal.split({','} + Whitespace)
|
||||
|
||||
for t in activeTopics:
|
||||
if t in disabledTopics:
|
||||
return
|
||||
elif t in enabledTopics:
|
||||
enabledTopicsMatch = true
|
||||
elif t in requiredTopics:
|
||||
dec requiredTopicsCount
|
||||
else:
|
||||
for topic in enabledTopics:
|
||||
if topic.name == t:
|
||||
enabledTopicsMatch = true
|
||||
if topic.logLevel != None:
|
||||
enabledTopicsLevel = topic.logLevel
|
||||
if t in requiredTopics:
|
||||
dec requiredTopicsCount
|
||||
|
||||
# Handling file name and line numbers on/off (lineNumbersEnabled) for particular log statements
|
||||
if finalBindings.hasKey("chroniclesLineNumbers"):
|
||||
|
@ -215,7 +220,7 @@ macro logIMPL(lineInfo: static InstInfo,
|
|||
useLineNumbers = chroniclesLineNumbers == "true"
|
||||
finalBindings.del("chroniclesLineNumbers")
|
||||
|
||||
if not enabledTopicsMatch or requiredTopicsCount > 0:
|
||||
if not enabledTopicsMatch or requiredTopicsCount > 0 or severity < enabledTopicsLevel:
|
||||
return
|
||||
|
||||
var code = newStmtList()
|
||||
|
|
|
@ -88,6 +88,10 @@ type
|
|||
Configuration* = object
|
||||
streams*: seq[StreamSpec]
|
||||
|
||||
Topic* = object
|
||||
name*: string
|
||||
logLevel*: LogLevel
|
||||
|
||||
const defaultChroniclesStreamName* = "defaultChroniclesStream"
|
||||
|
||||
proc handleYesNoOption(optName: string,
|
||||
|
@ -124,10 +128,26 @@ template handleEnumOption(E, varName: untyped): auto =
|
|||
|
||||
template topicsAsSeq(topics: string): untyped =
|
||||
when topics.len > 0:
|
||||
topics.split(Whitespace)
|
||||
topics.split({','} + Whitespace)
|
||||
else:
|
||||
newSeq[string](0)
|
||||
|
||||
template topicsWithLogLevelAsSeq(topics: string): untyped =
|
||||
var sequence = newSeq[Topic](0)
|
||||
when topics.len > 0:
|
||||
for topic in split(topics, {','} + Whitespace):
|
||||
var values = topic.split(':')
|
||||
if values.len > 1:
|
||||
if values[1].isDigit:
|
||||
sequence.add(Topic(name: values[0],
|
||||
logLevel: LogLevel(parseInt(values[1]))))
|
||||
else:
|
||||
sequence.add(Topic(name: values[0],
|
||||
logLevel: handleEnumOption(LogLevel, values[1])))
|
||||
else:
|
||||
sequence.add(Topic(name: values[0], logLevel: None))
|
||||
sequence
|
||||
|
||||
proc logFormatFromIdent(n: NimNode): LogFormat =
|
||||
let format = $n
|
||||
case format.toLowerAscii
|
||||
|
@ -285,7 +305,7 @@ const
|
|||
|
||||
textBlockIndent* = repeat(' ', chronicles_indent)
|
||||
|
||||
enabledTopics* = topicsAsSeq chronicles_enabled_topics
|
||||
enabledTopics* = topicsWithLogLevelAsSeq chronicles_enabled_topics
|
||||
disabledTopics* = topicsAsSeq chronicles_disabled_topics
|
||||
requiredTopics* = topicsAsSeq chronicles_required_topics
|
||||
lineNumbersEnabled* = handleYesNoOption chronicles_line_numbers
|
||||
|
|
Loading…
Reference in New Issue