Skip to content

Commit e6e28c7

Browse files
committed
topic specific log level config
1 parent c581ce7 commit e6e28c7

File tree

9 files changed

+64
-7
lines changed

9 files changed

+64
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ testwaku: | build deps rln-deps librln
222222
wakunode2: | build deps librln
223223
echo -e $(BUILD_MSG) "build/$@" && \
224224
\
225-
$(ENV_SCRIPT) nim wakunode2 $(NIM_PARAMS) waku.nims
225+
$(ENV_SCRIPT) nim wakunode2 $(NIM_PARAMS) -d:chronicles_runtime_filtering:on waku.nims
226226

227227
benchmarks: | build deps librln
228228
echo -e $(BUILD_MSG) "build/$@" && \

apps/wakunode2/wakunode2.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ when isMainModule:
9696
quit(QuitFailure)
9797

9898
c_signal(ansi_c.SIGSEGV, handleSigsegv)
99-
99+
logging.setTopicConfig(wakuNodeConf.logTopicsConfig)
100100
info "Node setup complete"
101101

102102
runForever()

simulations/mixnet/config1.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ rendezvous = true
2424
listen-address = "127.0.0.1"
2525
nat = "extip:127.0.0.1"
2626
ip-colocation-limit=0
27+
log-topic-config=["wakumix:TRACE"]
2728
#staticnode = ["/ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmPiEs2ozjjJF2iN2Pe2FYeMC9w4caRHKYdLdAfjgbWM6o", "/ip4/127.0.0.1/tcp/60003/p2p/16Uiu2HAmTEDHwAziWUSz6ZE23h5vxG2o4Nn7GazhMor4bVuMXTrA","/ip4/127.0.0.1/tcp/60004/p2p/16Uiu2HAmPwRKZajXtfb1Qsv45VVfRZgK3ENdfmnqzSrVm3BczF6f","/ip4/127.0.0.1/tcp/60005/p2p/16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu"]

tools/confutils/cli_args.nim

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import
2-
std/[strutils, strformat, sequtils],
2+
std/[strutils, strformat, sequtils, enumutils],
33
results,
44
chronicles,
55
chronos,
@@ -71,6 +71,13 @@ type WakuNodeConf* = object
7171
name: "log-format"
7272
.}: logging.LogFormat
7373

74+
logTopicsConfig* {.
75+
desc:
76+
"Sets the log level for specific topics. Format: <topic>:<level>. Argument may be repeated. ",
77+
defaultValue: newSeq[LogTopicConfig](0),
78+
name: "log-topic-config"
79+
.}: seq[LogTopicConfig]
80+
7481
rlnRelayCredPath* {.
7582
desc: "The path for persisting rln-relay credential",
7683
defaultValue: "",
@@ -703,6 +710,21 @@ proc isNumber(x: string): bool =
703710
except ValueError:
704711
result = false
705712

713+
proc parseCmdArg*(T: type LogTopicConfig, p: string): T =
714+
let elements = p.split(":")
715+
if elements.len != 2:
716+
raise newException(
717+
ValueError, "Invalid format for logTopicsConfig expected topic:loglevel"
718+
)
719+
720+
var logTopicConfig: LogTopicConfig
721+
try:
722+
let logLevel = parseEnum[LogLevel](elements[1])
723+
logTopicConfig = LogTopicConfig(topic: elements[0], level: logLevel)
724+
except ValueError:
725+
raise newException(ValueError, "Invalid log level")
726+
return logTopicConfig
727+
706728
proc parseCmdArg*(T: type MixNodePubInfo, p: string): T =
707729
let elements = p.split(":")
708730
if elements.len != 2:
@@ -803,6 +825,22 @@ proc readValue*(
803825
except CatchableError:
804826
raise newException(SerializationError, getCurrentExceptionMsg())
805827

828+
proc readValue*(
829+
r: var TomlReader, value: var LogTopicConfig
830+
) {.raises: [SerializationError].} =
831+
try:
832+
value = parseCmdArg(LogTopicConfig, r.readValue(string))
833+
except CatchableError:
834+
raise newException(SerializationError, getCurrentExceptionMsg())
835+
836+
proc readValue*(
837+
r: var EnvvarReader, value: var LogTopicConfig
838+
) {.raises: [SerializationError].} =
839+
try:
840+
value = parseCmdArg(LogTopicConfig, r.readValue(string))
841+
except CatchableError:
842+
raise newException(SerializationError, getCurrentExceptionMsg())
843+
806844
proc readValue*(
807845
r: var TomlReader, value: var MixNodePubInfo
808846
) {.raises: [SerializationError].} =
@@ -911,6 +949,7 @@ proc toWakuConf*(n: WakuNodeConf): ConfResult[WakuConf] =
911949

912950
b.withLogLevel(n.logLevel)
913951
b.withLogFormat(n.logFormat)
952+
b.withLogTopicsConfig(n.logTopicsConfig)
914953

915954
b.rlnRelayConf.withEnabled(n.rlnRelay)
916955
if n.rlnRelayCredPath != "":

waku/common/logging.nim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type LogFormat* = enum
1414
TEXT
1515
JSON
1616

17+
type LogTopicConfig* = object
18+
topic*: string
19+
level*: LogLevel
20+
1721
## Utils
1822

1923
proc stripAnsi(v: string): string =
@@ -63,7 +67,6 @@ proc writeAndFlush(f: syncio.File, s: LogOutputStr) =
6367
## Setup
6468

6569
proc setupLogLevel(level: LogLevel) =
66-
# TODO: Support per topic level configuratio
6770
topics_registry.setLogLevel(level)
6871

6972
proc setupLogFormat(format: LogFormat, color = true) =
@@ -103,3 +106,11 @@ proc setupLog*(level: LogLevel, format: LogFormat) =
103106

104107
setupLogLevel(level)
105108
setupLogFormat(format, color)
109+
110+
proc setTopicConfig*(logTopicsConfig: seq[LogTopicConfig]) =
111+
for topicConf in logTopicsConfig:
112+
if not topics_registry.setTopicState(topicConf.topic, Enabled, topicConf.level):
113+
error "Unknown logging topic or unable to set loglevel",
114+
topic = topicConf.topic, level = $topicConf.level
115+
116+
{.pop.}

waku/factory/conf_builder/waku_conf_builder.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type WakuConfBuilder* = object
102102

103103
logLevel: Option[logging.LogLevel]
104104
logFormat: Option[logging.LogFormat]
105+
logTopicsConfig: seq[LogTopicConfig]
105106

106107
natStrategy: Option[string]
107108

@@ -221,6 +222,11 @@ proc withLogLevel*(b: var WakuConfBuilder, logLevel: logging.LogLevel) =
221222
proc withLogFormat*(b: var WakuConfBuilder, logFormat: logging.LogFormat) =
222223
b.logFormat = some(logFormat)
223224

225+
proc withLogTopicsConfig*(
226+
b: var WakuConfBuilder, logTopicsConfig: seq[LogTopicConfig]
227+
) =
228+
b.logTopicsConfig = logTopicsConfig
229+
224230
proc withP2pTcpPort*(b: var WakuConfBuilder, p2pTcpPort: Port) =
225231
b.p2pTcpPort = some(p2pTcpPort)
226232

@@ -645,6 +651,7 @@ proc build*(
645651
maxMessageSizeBytes: maxMessageSizeBytes,
646652
logLevel: logLevel,
647653
logFormat: logFormat,
654+
logTopicsConfig: builder.logTopicsConfig,
648655
# TODO: Separate builders
649656
endpointConf: EndpointConf(
650657
natStrategy: natStrategy,

waku/factory/waku.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ proc new*(
164164
): Future[Result[Waku, string]] {.async.} =
165165
let rng = crypto.newRng()
166166

167-
logging.setupLog(wakuConf.logLevel, wakuConf.logFormat)
168-
169167
?wakuConf.validate()
170168
wakuConf.logConf()
171169

waku/factory/waku_conf.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type WakuConf* {.requiresInit.} = ref object
126126

127127
logLevel*: logging.LogLevel
128128
logFormat*: logging.LogFormat
129+
logTopicsConfig*: seq[LogTopicConfig]
129130

130131
peerPersistence*: bool
131132
# TODO: should clearly be a uint

waku/waku_mix/protocol.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import
1919
../common/nimchronos
2020

2121
logScope:
22-
topics = "waku mix"
22+
topics = "wakumix"
2323

2424
const mixMixPoolSize = 3
2525

0 commit comments

Comments
 (0)