Skip to content

Commit

Permalink
chore: libwaku - allow to properly set the log level in libwaku and u…
Browse files Browse the repository at this point in the history
…nify a little (#2708)

* waku.nimble: set properly chronicles compilation flags for static libwaku
* adapt examples to new log setup
  • Loading branch information
Ivansete-status authored May 17, 2024
1 parent e7b0777 commit 3faffdb
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 34 deletions.
14 changes: 3 additions & 11 deletions apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ else:
{.push raises: [].}

import
std/[options, strutils, os, sequtils, net],
std/[options, strutils, sequtils, net],
chronicles,
chronos,
metrics,
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/waku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ int main(int argc, char** argv) {
\"port\": %d, \
\"key\": \"%s\", \
\"relay\": %s, \
\"logLevel\": \"DEBUG\" \
}", cfgNode.host,
cfgNode.port,
cfgNode.key,
Expand Down
2 changes: 1 addition & 1 deletion examples/filter_subscriber.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/golang/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -447,6 +448,7 @@ func main() {
Port: 30304,
NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710",
EnableRelay: true,
LogLevel: "DEBUG",
}

node, err := WakuNew(config)
Expand Down
2 changes: 1 addition & 1 deletion examples/lightpush_publisher.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/nodejs/waku.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var cfg = `{
"port": 60001,
"key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9",
"relay": true
"logLevel": "DEBUG"
}`

function event_handler(event) {
Expand Down
3 changes: 2 additions & 1 deletion examples/publisher.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion examples/python/waku.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn main() {
\"host\": \"127.0.0.1\",\
\"port\": 60000, \
\"key\": \"0d714a1fada214dead6dc9c7274581ec20ff292451866e7d6d677dc818e8ccd2\", \
\"relay\": true \
\"relay\": true ,\
\"logLevel\": \"DEBUG\"
}";

unsafe {
Expand Down
3 changes: 2 additions & 1 deletion examples/subscriber.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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[])[]
Expand Down
11 changes: 1 addition & 10 deletions examples/wakustealthcommitments/wakustealthcommitments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions waku.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 19 additions & 3 deletions waku/common/logging.nim
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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)
2 changes: 1 addition & 1 deletion waku/factory/waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3faffdb

Please sign in to comment.