From 2b1aacb2636d966475d0bea0ff7a981c068f46e9 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Tue, 31 Jan 2023 15:19:43 +0100 Subject: [PATCH 1/3] chronicles_enabled_topics wildcard support --- README.md | 4 ++-- chronicles.nim | 23 ++++++++++++++++++++++- tests/topics/enabled_wildcard.test | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/topics/enabled_wildcard.test diff --git a/README.md b/README.md index f19c49f..8b21946 100644 --- a/README.md +++ b/README.md @@ -303,14 +303,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 diff --git a/chronicles.nim b/chronicles.nim index 40237b3..f239e80 100644 --- a/chronicles.nim +++ b/chronicles.nim @@ -215,6 +215,27 @@ 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 str.len <= strIndex: return false + + if ch == '?': + strIndex.inc() + continue + elif ch == '*': + while strIndex < str.len: + if wildcard(str[strIndex ..< ^0], match[matchIndex + 1 .. ^1]): + return true + strIndex.inc() + elif ch == str[strIndex]: + strIndex.inc() + else: + return false + + return strIndex == str.len + macro logIMPL(lineInfo: static InstInfo, Stream: typed, RecordType: type, @@ -258,7 +279,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 diff --git a/tests/topics/enabled_wildcard.test b/tests/topics/enabled_wildcard.test new file mode 100644 index 0000000..34561da --- /dev/null +++ b/tests/topics/enabled_wildcard.test @@ -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 +""" From 1b70a51b677a6abe107639e656a850ce6f6a5a4c Mon Sep 17 00:00:00 2001 From: Tanguy Date: Tue, 31 Jan 2023 16:16:35 +0100 Subject: [PATCH 2/3] fix wildcard --- chronicles.nim | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/chronicles.nim b/chronicles.nim index f239e80..7f9725a 100644 --- a/chronicles.nim +++ b/chronicles.nim @@ -219,17 +219,14 @@ proc wildcard(str, match: string): bool = var strIndex = 0 for matchIndex, ch in match: - if str.len <= strIndex: return false - - if ch == '?': - strIndex.inc() - continue - elif ch == '*': + if ch == '*': while strIndex < str.len: if wildcard(str[strIndex ..< ^0], match[matchIndex + 1 .. ^1]): return true strIndex.inc() - elif ch == str[strIndex]: + elif str.len <= strIndex: + return false + elif ch == '?' or ch == str[strIndex]: strIndex.inc() else: return false From 946652f95bd189d59374b9ae156336a1f7947ea6 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Tue, 31 Jan 2023 16:16:44 +0100 Subject: [PATCH 3/3] remove nimble.lock --- nimble.lock | 118 ---------------------------------------------------- 1 file changed, 118 deletions(-) delete mode 100644 nimble.lock diff --git a/nimble.lock b/nimble.lock deleted file mode 100644 index 5176cac..0000000 --- a/nimble.lock +++ /dev/null @@ -1,118 +0,0 @@ -{ - "version": 1, - "packages": { - "stew": { - "version": "0.1.0", - "vcsRevision": "6ad35b876fb6ebe0dfee0f697af173acc47906ee", - "url": "https://github.com/status-im/nim-stew.git", - "downloadMethod": "git", - "dependencies": [], - "checksums": { - "sha1": "46d58c4feb457f3241e3347778334e325dce5268" - } - }, - "unittest2": { - "version": "0.0.4", - "vcsRevision": "f180f596c88dfd266f746ed6f8dbebce39c824db", - "url": "https://github.com/status-im/nim-unittest2.git", - "downloadMethod": "git", - "dependencies": [], - "checksums": { - "sha1": "fa309c41eaf6ef57895b9e603f2620a2f6e11780" - } - }, - "testutils": { - "version": "0.4.2", - "vcsRevision": "aa6e5216f4b4ab5aa971cdcdd70e1ec1203cedf2", - "url": "https://github.com/status-im/nim-testutils", - "downloadMethod": "git", - "dependencies": [ - "unittest2" - ], - "checksums": { - "sha1": "94427e0cce0e0c5841edcd3a6530b4e6b857a3cb" - } - }, - "bearssl": { - "version": "0.1.5", - "vcsRevision": "0a82a068280e4e9fb7a4936d92f1d0991c3bb363", - "url": "https://github.com/status-im/nim-bearssl.git", - "downloadMethod": "git", - "dependencies": [ - "unittest2" - ], - "checksums": { - "sha1": "9a34d4a65881261099270a22b34d49c9ca6af372" - } - }, - "httputils": { - "version": "0.3.0", - "vcsRevision": "689da19e9e9cfff4ced85e2b25c6b2b5598ed079", - "url": "https://github.com/status-im/nim-http-utils.git", - "downloadMethod": "git", - "dependencies": [ - "stew" - ], - "checksums": { - "sha1": "4ad3ad68d13c50184180ab4b2eacc0bd7ed2ed44" - } - }, - "chronos": { - "version": "3.0.11", - "vcsRevision": "17fed89c99beac5a92d3668d0d3e9b0e4ac13936", - "url": "https://github.com/status-im/nim-chronos.git", - "downloadMethod": "git", - "dependencies": [ - "stew", - "bearssl", - "httputils", - "unittest2" - ], - "checksums": { - "sha1": "f6fffc87571e5f76af2a77c4ebcc0e00909ced4e" - } - }, - "faststreams": { - "version": "0.3.0", - "vcsRevision": "1b561a9e71b6bdad1c1cdff753418906037e9d09", - "url": "https://github.com/status-im/nim-faststreams.git", - "downloadMethod": "git", - "dependencies": [ - "stew", - "testutils", - "chronos", - "unittest2" - ], - "checksums": { - "sha1": "97edf9797924af48566a0af8267203dc21d80c77" - } - }, - "serialization": { - "version": "0.1.0", - "vcsRevision": "fcd0eadadde0ee000a63df8ab21dc4e9f015a790", - "url": "https://github.com/status-im/nim-serialization.git", - "downloadMethod": "git", - "dependencies": [ - "faststreams", - "unittest2", - "stew" - ], - "checksums": { - "sha1": "fef59519892cac70cccd81b612085caaa5e3e6cf" - } - }, - "json_serialization": { - "version": "0.1.0", - "vcsRevision": "c5f0e2465e8375dfc7aa0f56ccef67cb680bc6b0", - "url": "https://github.com/status-im/nim-json-serialization.git", - "downloadMethod": "git", - "dependencies": [ - "serialization", - "stew" - ], - "checksums": { - "sha1": "d89d79d0679a3a41b350e3ad4be56c0308cc5ec6" - } - } - } -} \ No newline at end of file