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
This commit is contained in:
Ivan FB 2024-05-17 16:28:54 +02:00 committed by GitHub
parent e7b0777d25
commit 3faffdbcaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 48 additions and 34 deletions

View File

@ -4,7 +4,7 @@ else:
{.push raises: [].} {.push raises: [].}
import import
std/[options, strutils, os, sequtils, net], std/[options, strutils, sequtils, net],
chronicles, chronicles,
chronos, chronos,
metrics, metrics,
@ -43,16 +43,8 @@ when isMainModule:
error "failure while loading the configuration", error = error error "failure while loading the configuration", error = error
quit(QuitFailure) quit(QuitFailure)
## Logging setup ## Also called within Waku.init. The call to startRestServerEsentials needs the following line
# Adhere to NO_COLOR initiative: https://no-color.org/ logging.setupLog(conf.logLevel, conf.logFormat)
let color =
try:
not parseBool(os.getEnv("NO_COLOR", "false"))
except CatchableError:
true
logging.setupLogLevel(conf.logLevel)
logging.setupLogFormat(conf.logFormat, color)
case conf.cmd case conf.cmd
of generateRlnKeystore: of generateRlnKeystore:

View File

@ -276,7 +276,8 @@ int main(int argc, char** argv) {
\"store\": %s, \ \"store\": %s, \
\"storeMessageDbUrl\": \"%s\", \ \"storeMessageDbUrl\": \"%s\", \
\"storeMessageRetentionPolicy\": \"%s\", \ \"storeMessageRetentionPolicy\": \"%s\", \
\"storeMaxNumDbConnections\": %d \ \"storeMaxNumDbConnections\": %d , \
\"logLevel\": \"DEBUG\" \
}", cfgNode.host, }", cfgNode.host,
cfgNode.port, cfgNode.port,
cfgNode.key, cfgNode.key,

View File

@ -219,6 +219,7 @@ int main(int argc, char** argv) {
\"port\": %d, \ \"port\": %d, \
\"key\": \"%s\", \ \"key\": \"%s\", \
\"relay\": %s, \ \"relay\": %s, \
\"logLevel\": \"DEBUG\" \
}", cfgNode.host, }", cfgNode.host,
cfgNode.port, cfgNode.port,
cfgNode.key, cfgNode.key,

View File

@ -69,7 +69,7 @@ proc maintainSubscription(
proc setupAndSubscribe(rng: ref HmacDrbgContext) = proc setupAndSubscribe(rng: ref HmacDrbgContext) =
let filterPeer = parsePeerInfo(FilterPeer).get() let filterPeer = parsePeerInfo(FilterPeer).get()
setupLogLevel(logging.LogLevel.NOTICE) setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT)
notice "starting filter subscriber" notice "starting filter subscriber"
var var

View File

@ -193,6 +193,7 @@ type WakuConfig struct {
Port int `json:"port,omitempty"` Port int `json:"port,omitempty"`
NodeKey string `json:"key,omitempty"` NodeKey string `json:"key,omitempty"`
EnableRelay bool `json:"relay"` EnableRelay bool `json:"relay"`
LogLevel string `json:"logLevel"`
} }
type WakuNode struct { type WakuNode struct {
@ -447,6 +448,7 @@ func main() {
Port: 30304, Port: 30304,
NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710", NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710",
EnableRelay: true, EnableRelay: true,
LogLevel: "DEBUG",
} }
node, err := WakuNew(config) node, err := WakuNew(config)

View File

@ -42,7 +42,7 @@ proc publishMessages(
proc setupAndPublish(rng: ref HmacDrbgContext) = proc setupAndPublish(rng: ref HmacDrbgContext) =
let lightpushPeer = parsePeerInfo(LightpushPeer).get() let lightpushPeer = parsePeerInfo(LightpushPeer).get()
setupLogLevel(logging.LogLevel.NOTICE) setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT)
notice "starting lightpush publisher" notice "starting lightpush publisher"
var var

View File

@ -21,6 +21,7 @@ var cfg = `{
"port": 60001, "port": 60001,
"key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9", "key": "364d111d729a6eb6d3e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9",
"relay": true "relay": true
"logLevel": "DEBUG"
}` }`
function event_handler(event) { function event_handler(event) {

View File

@ -38,7 +38,8 @@ const discv5Port = 9000
proc setupAndPublish(rng: ref HmacDrbgContext) {.async.} = proc setupAndPublish(rng: ref HmacDrbgContext) {.async.} =
# use notice to filter all waku messaging # 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 notice "starting publisher", wakuPort = wakuPort, discv5Port = discv5Port
let let
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[]).get() nodeKey = crypto.PrivateKey.random(Secp256k1, rng[]).get()

View File

@ -49,7 +49,8 @@ json_config = "{ \
\"host\": \"%s\", \ \"host\": \"%s\", \
\"port\": %d, \ \"port\": %d, \
\"key\": \"%s\", \ \"key\": \"%s\", \
\"relay\": %s \ \"relay\": %s ,\
\"logLevel\": \"DEBUG\" \
}" % (args.host, }" % (args.host,
int(args.port), int(args.port),
args.key, args.key,

View File

@ -63,7 +63,8 @@ fn main() {
\"host\": \"127.0.0.1\",\ \"host\": \"127.0.0.1\",\
\"port\": 60000, \ \"port\": 60000, \
\"key\": \"0d714a1fada214dead6dc9c7274581ec20ff292451866e7d6d677dc818e8ccd2\", \ \"key\": \"0d714a1fada214dead6dc9c7274581ec20ff292451866e7d6d677dc818e8ccd2\", \
\"relay\": true \ \"relay\": true ,\
\"logLevel\": \"DEBUG\"
}"; }";
unsafe { unsafe {

View File

@ -36,7 +36,8 @@ const discv5Port = 8000
proc setupAndSubscribe(rng: ref HmacDrbgContext) {.async.} = proc setupAndSubscribe(rng: ref HmacDrbgContext) {.async.} =
# use notice to filter all waku messaging # 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 notice "starting subscriber", wakuPort = wakuPort, discv5Port = discv5Port
let let
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]

View File

@ -11,16 +11,7 @@ logScope:
when isMainModule: when isMainModule:
## Logging setup ## Logging setup
setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT)
# 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)
info "Starting Waku Stealth Commitment Protocol" info "Starting Waku Stealth Commitment Protocol"
info "Starting Waku Node" info "Starting Waku Node"

View File

@ -105,8 +105,14 @@ task chat2bridge, "Build chat2bridge":
### C Bindings ### C Bindings
task libwakuStatic, "Build the cbindings waku node library": task libwakuStatic, "Build the cbindings waku node library":
let name = "libwaku" 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": task libwakuDynamic, "Build the cbindings waku node library":
let name = "libwaku" 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"

View File

@ -1,6 +1,10 @@
## This code has been copied and addapted from `status-im/nimbu-eth2` project. ## 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 ## 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 export chronicles.LogLevel
@ -61,11 +65,11 @@ proc writeAndFlush(f: File, s: LogOutputStr) =
## Setup ## Setup
proc setupLogLevel*(level: LogLevel) = proc setupLogLevel(level: LogLevel) =
# TODO: Support per topic level configuratio # TODO: Support per topic level configuratio
topics_registry.setLogLevel(level) topics_registry.setLogLevel(level)
proc setupLogFormat*(format: LogFormat, color = true) = proc setupLogFormat(format: LogFormat, color = true) =
proc noOutputWriter(logLevel: LogLevel, msg: LogOutputStr) = proc noOutputWriter(logLevel: LogLevel, msg: LogOutputStr) =
discard discard
@ -90,3 +94,15 @@ proc setupLogFormat*(format: LogFormat, color = true) =
"the present module should be compiled with '-d:chronicles_default_output_device=dynamic' " & "the present module should be compiled with '-d:chronicles_default_output_device=dynamic' " &
"and '-d:chronicles_sinks=\"textlines,json\"' options" "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)

View File

@ -92,7 +92,7 @@ proc init*(T: type Waku, conf: WakuNodeConf): Result[Waku, string] =
var confCopy = conf var confCopy = conf
let rng = crypto.newRng() let rng = crypto.newRng()
logging.setupLogLevel(confCopy.logLevel) logging.setupLog(conf.logLevel, conf.logFormat)
case confCopy.clusterId case confCopy.clusterId