Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ logging statements will be erased at compile-time from the final code.
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.
string of case-sensitive topic names, supporting wildcard matching.

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`
e.g. `-d:chronicles_enabled_topics:MyTopic:DEBUG,TopicsStartingWith*:5`

### chronicles_required_topics

Expand Down
20 changes: 19 additions & 1 deletion chronicles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ macro expandIt*(T: type, expandedProps: untyped): untyped =
when defined(debugLogImpl):
echo result.repr

proc wildcard(str, match: string): bool =
var strIndex = 0

for matchIndex, ch in match:
if ch == '*':
while strIndex < str.len:
if wildcard(str[strIndex ..< ^0], match[matchIndex + 1 .. ^1]):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[] allocates a new string for every call which would be prohibitively expensive

return true
strIndex.inc()
elif str.len <= strIndex:
return false
elif ch == '?' or ch == str[strIndex]:
strIndex.inc()
else:
return false

strIndex == str.len

template chroniclesUsedMagic(x: untyped) =
# Force the compiler to mark any symbol in the x
# as used without actually generate any code.
Expand Down Expand Up @@ -274,7 +292,7 @@ macro logIMPL(lineInfo: static InstInfo,
return
else:
for topic in enabledTopics:
if topic.name == t:
if wildcard(t, topic.name):
if topic.logLevel != NONE:
if severity >= topic.logLevel:
enabledTopicsMatch = true
Expand Down
17 changes: 17 additions & 0 deletions tests/topics/enabled_wildcard.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
program="../topics_and_loglvls"
chronicles_sinks="textlines[stdout]"
chronicles_colors=None
chronicles_timestamps=None
chronicles_log_level=TRACE
chronicles_enabled_topics="*:INFO foo:TRACE"
[Output]
stdout="""WRN inside main topics="main" tid=32613 b=10 arg=50 a=1
INF inside main topics="main" tid=32613 b=10 arg=50 a=1
WRN inside foo topics="foo" tid=32613 b=10 arg=10
INF inside foo topics="foo" tid=32613 b=10 arg=10
DBG inside foo topics="foo" tid=32613 b=10 arg=10
WRN inside foobar topics="foo bar" tid=32613 b=10 arg=20
INF inside foobar topics="foo bar" tid=32613 b=10 arg=20
DBG inside foobar topics="foo bar" tid=32613 b=10 arg=20
INF after main topics="general" tid=32613
"""