From 3faffdbcaaa0dcda4c78bb987b7704067bbde3e1 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Fri, 17 May 2024 16:28:54 +0200 Subject: [PATCH] chore: libwaku - allow to properly set the log level in libwaku and unify a little (#2708) * waku.nimble: set properly chronicles compilation flags for static libwaku * adapt examples to new log setup --- apps/wakunode2/wakunode2.nim | 14 +++--------- examples/cbindings/waku_example.c | 3 ++- examples/cpp/waku.cpp | 1 + examples/filter_subscriber.nim | 2 +- examples/golang/waku.go | 2 ++ examples/lightpush_publisher.nim | 2 +- examples/nodejs/waku.js | 1 + examples/publisher.nim | 3 ++- examples/python/waku.py | 3 ++- examples/rust/src/main.rs | 3 ++- examples/subscriber.nim | 3 ++- .../wakustealthcommitments.nim | 11 +--------- waku.nimble | 10 +++++++-- waku/common/logging.nim | 22 ++++++++++++++++--- waku/factory/waku.nim | 2 +- 15 files changed, 48 insertions(+), 34 deletions(-) diff --git a/apps/wakunode2/wakunode2.nim b/apps/wakunode2/wakunode2.nim index 34073e25d4..474eef1f7a 100644 --- a/apps/wakunode2/wakunode2.nim +++ b/apps/wakunode2/wakunode2.nim @@ -4,7 +4,7 @@ else: {.push raises: [].} import - std/[options, strutils, os, sequtils, net], + std/[options, strutils, sequtils, net], chronicles, chronos, metrics, @@ -43,16 +43,8 @@ when isMainModule: error "failure while loading the configuration", error = error quit(QuitFailure) - ## Logging setup - # Adhere to NO_COLOR initiative: https://no-color.org/ - let color = - try: - not parseBool(os.getEnv("NO_COLOR", "false")) - except CatchableError: - true - - logging.setupLogLevel(conf.logLevel) - logging.setupLogFormat(conf.logFormat, color) + ## Also called within Waku.init. The call to startRestServerEsentials needs the following line + logging.setupLog(conf.logLevel, conf.logFormat) case conf.cmd of generateRlnKeystore: diff --git a/examples/cbindings/waku_example.c b/examples/cbindings/waku_example.c index 69e51687f7..87e07977e5 100644 --- a/examples/cbindings/waku_example.c +++ b/examples/cbindings/waku_example.c @@ -276,7 +276,8 @@ int main(int argc, char** argv) { \"store\": %s, \ \"storeMessageDbUrl\": \"%s\", \ \"storeMessageRetentionPolicy\": \"%s\", \ - \"storeMaxNumDbConnections\": %d \ + \"storeMaxNumDbConnections\": %d , \ + \"logLevel\": \"DEBUG\" \ }", cfgNode.host, cfgNode.port, cfgNode.key, diff --git a/examples/cpp/waku.cpp b/examples/cpp/waku.cpp index ec8346520b..cab7bacd84 100644 --- a/examples/cpp/waku.cpp +++ b/examples/cpp/waku.cpp @@ -219,6 +219,7 @@ int main(int argc, char** argv) { \"port\": %d, \ \"key\": \"%s\", \ \"relay\": %s, \ + \"logLevel\": \"DEBUG\" \ }", cfgNode.host, cfgNode.port, cfgNode.key, diff --git a/examples/filter_subscriber.nim b/examples/filter_subscriber.nim index bc6e587b1a..9f850a8799 100644 --- a/examples/filter_subscriber.nim +++ b/examples/filter_subscriber.nim @@ -69,7 +69,7 @@ proc maintainSubscription( proc setupAndSubscribe(rng: ref HmacDrbgContext) = let filterPeer = parsePeerInfo(FilterPeer).get() - setupLogLevel(logging.LogLevel.NOTICE) + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) notice "starting filter subscriber" var diff --git a/examples/golang/waku.go b/examples/golang/waku.go index 587aa9f6ec..22c8b7b0e2 100644 --- a/examples/golang/waku.go +++ b/examples/golang/waku.go @@ -193,6 +193,7 @@ type WakuConfig struct { Port int `json:"port,omitempty"` NodeKey string `json:"key,omitempty"` EnableRelay bool `json:"relay"` + LogLevel string `json:"logLevel"` } type WakuNode struct { @@ -447,6 +448,7 @@ func main() { Port: 30304, NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710", EnableRelay: true, + LogLevel: "DEBUG", } node, err := WakuNew(config) diff --git a/examples/lightpush_publisher.nim b/examples/lightpush_publisher.nim index 241c52bfcc..8dee61fd1b 100644 --- a/examples/lightpush_publisher.nim +++ b/examples/lightpush_publisher.nim @@ -42,7 +42,7 @@ proc publishMessages( proc setupAndPublish(rng: ref HmacDrbgContext) = let lightpushPeer = parsePeerInfo(LightpushPeer).get() - setupLogLevel(logging.LogLevel.NOTICE) + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) notice "starting lightpush publisher" var diff --git a/examples/nodejs/waku.js b/examples/nodejs/waku.js index 87d01378c5..258e348493 100644 --- a/examples/nodejs/waku.js +++ b/examples/nodejs/waku.js @@ -21,6 +21,7 @@ var cfg = `{ "port": 60001, "key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9", "relay": true + "logLevel": "DEBUG" }` function event_handler(event) { diff --git a/examples/publisher.nim b/examples/publisher.nim index 2a95c68e2d..c266cda9f8 100644 --- a/examples/publisher.nim +++ b/examples/publisher.nim @@ -38,7 +38,8 @@ const discv5Port = 9000 proc setupAndPublish(rng: ref HmacDrbgContext) {.async.} = # use notice to filter all waku messaging - setupLogLevel(logging.LogLevel.NOTICE) + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) + notice "starting publisher", wakuPort = wakuPort, discv5Port = discv5Port let nodeKey = crypto.PrivateKey.random(Secp256k1, rng[]).get() diff --git a/examples/python/waku.py b/examples/python/waku.py index dbe0a573fb..4d5f5643e5 100644 --- a/examples/python/waku.py +++ b/examples/python/waku.py @@ -49,7 +49,8 @@ def call_waku(func): \"host\": \"%s\", \ \"port\": %d, \ \"key\": \"%s\", \ - \"relay\": %s \ + \"relay\": %s ,\ + \"logLevel\": \"DEBUG\" \ }" % (args.host, int(args.port), args.key, diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs index 1580bfb4b9..f5ff07c644 100644 --- a/examples/rust/src/main.rs +++ b/examples/rust/src/main.rs @@ -63,7 +63,8 @@ fn main() { \"host\": \"127.0.0.1\",\ \"port\": 60000, \ \"key\": \"0d714a1fada214dead6dc9c7274581ec20ff292451866e7d6d677dc818e8ccd2\", \ - \"relay\": true \ + \"relay\": true ,\ + \"logLevel\": \"DEBUG\" }"; unsafe { diff --git a/examples/subscriber.nim b/examples/subscriber.nim index 842a3a02de..45f06e1b9b 100644 --- a/examples/subscriber.nim +++ b/examples/subscriber.nim @@ -36,7 +36,8 @@ const discv5Port = 8000 proc setupAndSubscribe(rng: ref HmacDrbgContext) {.async.} = # use notice to filter all waku messaging - setupLogLevel(logging.LogLevel.NOTICE) + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) + notice "starting subscriber", wakuPort = wakuPort, discv5Port = discv5Port let nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] diff --git a/examples/wakustealthcommitments/wakustealthcommitments.nim b/examples/wakustealthcommitments/wakustealthcommitments.nim index 7d2655ed1d..a6cf4a5627 100644 --- a/examples/wakustealthcommitments/wakustealthcommitments.nim +++ b/examples/wakustealthcommitments/wakustealthcommitments.nim @@ -11,16 +11,7 @@ logScope: when isMainModule: ## Logging setup - - # Adhere to NO_COLOR initiative: https://no-color.org/ - let color = - try: - not parseBool(os.getEnv("NO_COLOR", "false")) - except CatchableError: - true - - logging.setupLogLevel(logging.LogLevel.INFO) - logging.setupLogFormat(logging.LogFormat.TEXT, color) + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) info "Starting Waku Stealth Commitment Protocol" info "Starting Waku Node" diff --git a/waku.nimble b/waku.nimble index f1169423f9..1ca4f79139 100644 --- a/waku.nimble +++ b/waku.nimble @@ -105,8 +105,14 @@ task chat2bridge, "Build chat2bridge": ### C Bindings task libwakuStatic, "Build the cbindings waku node library": let name = "libwaku" - buildLibrary name, "library/", "-d:chronicles_log_level=ERROR", "static" + buildLibrary name, + "library/", + """-d:chronicles_line_numbers -d:chronicles_runtime_filtering=on -d:chronicles_sinks="textlines,json" -d:chronicles_default_output_device=Dynamic -d:chronicles_disabled_topics="eth,dnsdisc.client" """, + "static" task libwakuDynamic, "Build the cbindings waku node library": let name = "libwaku" - buildLibrary name, "library/", "-d:chronicles_log_level=ERROR", "dynamic" + buildLibrary name, + "library/", + """-d:chronicles_line_numbers -d:chronicles_runtime_filtering=on -d:chronicles_sinks="textlines,json" -d:chronicles_default_output_device=Dynamic -d:chronicles_disabled_topics="eth,dnsdisc.client" """, + "dynamic" diff --git a/waku/common/logging.nim b/waku/common/logging.nim index 7b242ba9c2..a955d3d684 100644 --- a/waku/common/logging.nim +++ b/waku/common/logging.nim @@ -1,6 +1,10 @@ ## This code has been copied and addapted from `status-im/nimbu-eth2` project. ## Link: https://github.com/status-im/nimbus-eth2/blob/c585b0a5b1ae4d55af38ad7f4715ad455e791552/beacon_chain/nimbus_binary_common.nim -import std/typetraits, chronicles, chronicles/log_output, chronicles/topics_registry +import + std/[typetraits, os, strutils], + chronicles, + chronicles/log_output, + chronicles/topics_registry export chronicles.LogLevel @@ -61,11 +65,11 @@ proc writeAndFlush(f: File, s: LogOutputStr) = ## Setup -proc setupLogLevel*(level: LogLevel) = +proc setupLogLevel(level: LogLevel) = # TODO: Support per topic level configuratio topics_registry.setLogLevel(level) -proc setupLogFormat*(format: LogFormat, color = true) = +proc setupLogFormat(format: LogFormat, color = true) = proc noOutputWriter(logLevel: LogLevel, msg: LogOutputStr) = discard @@ -90,3 +94,15 @@ proc setupLogFormat*(format: LogFormat, color = true) = "the present module should be compiled with '-d:chronicles_default_output_device=dynamic' " & "and '-d:chronicles_sinks=\"textlines,json\"' options" .} + +proc setupLog*(level: LogLevel, format: LogFormat) = + ## Logging setup + # Adhere to NO_COLOR initiative: https://no-color.org/ + let color = + try: + not parseBool(os.getEnv("NO_COLOR", "false")) + except CatchableError: + true + + setupLogLevel(level) + setupLogFormat(format, color) diff --git a/waku/factory/waku.nim b/waku/factory/waku.nim index 9b85508868..a461f65c5e 100644 --- a/waku/factory/waku.nim +++ b/waku/factory/waku.nim @@ -92,7 +92,7 @@ proc init*(T: type Waku, conf: WakuNodeConf): Result[Waku, string] = var confCopy = conf let rng = crypto.newRng() - logging.setupLogLevel(confCopy.logLevel) + logging.setupLog(conf.logLevel, conf.logFormat) case confCopy.clusterId