From 1351b0d2223f8d3944bacaff0907fef49d3d4037 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:13:41 +1000 Subject: [PATCH] Support logging to file --- codex/codex.nim | 8 +++++++- codex/conf.nim | 32 +++++++++++++++++++++++++++++++- config.nims | 2 +- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/codex/codex.nim b/codex/codex.nim index 0b725bb7..29c1aefc 100644 --- a/codex/codex.nim +++ b/codex/codex.nim @@ -129,7 +129,13 @@ proc bootstrapInteractions( return (client, host, validator) proc start*(s: CodexServer) {.async.} = - notice "Starting codex node" + notice "Starting codex node", + persistence = s.config.persistence, + validator = s.config.validator, + ethAccount = s.config.ethAccount, + simProofFailures = s.config.simulateProofFailures, + dataDir = s.config.dataDir, + logFile = s.config.logFile await s.repoStore.start() s.maintenance.start() diff --git a/codex/conf.nim b/codex/conf.nim index d9488076..a03d420b 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -28,8 +28,11 @@ import pkg/metrics import pkg/metrics/chronos_httpserver import pkg/stew/shims/net as stewnet import pkg/stew/shims/parseutils +import pkg/stew/byteutils import pkg/libp2p import pkg/ethers +import pkg/questionable +import pkg/questionable/results import ./discovery import ./stores @@ -259,6 +262,13 @@ type hidden .}: int + logFile* {. + desc: "Logs to file" + defaultValue: string.none + name: "log-file" + hidden + .}: Option[string] + of initNode: discard @@ -444,9 +454,10 @@ proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} = warn "Unrecognized logging topic", topic = topicName proc setupLogging*(conf: CodexConf) = - when defaultChroniclesStream.outputs.type.arity != 2: + when defaultChroniclesStream.outputs.type.arity != 3: warn "Logging configuration options not enabled in the current build" else: + var logFile: ?IoHandle proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard proc writeAndFlush(f: File, msg: LogOutputStr) = try: @@ -461,6 +472,25 @@ proc setupLogging*(conf: CodexConf) = proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) = writeAndFlush(stdout, stripAnsi(msg)) + proc fileFlush(logLevel: LogLevel, msg: LogOutputStr) = + if file =? logFile: + if error =? file.writeFile(stripAnsi(msg).toBytes).errorOption: + error "failed to write to log file", errorCode = $error + + defaultChroniclesStream.outputs[2].writer = noOutput + if logFilePath =? conf.logFile and logFilePath.len > 0: + let logFileHandle = openFile( + logFilePath, + {OpenFlags.Write, OpenFlags.Create, OpenFlags.Truncate} + ) + if logFileHandle.isErr: + error "failed to open log file", + path = logFilePath, + errorCode = $logFileHandle.error + else: + logFile = logFileHandle.option + defaultChroniclesStream.outputs[2].writer = fileFlush + defaultChroniclesStream.outputs[1].writer = noOutput defaultChroniclesStream.outputs[0].writer = diff --git a/config.nims b/config.nims index aa0c1f0f..3c02c598 100644 --- a/config.nims +++ b/config.nims @@ -111,7 +111,7 @@ switch("define", "libp2p_pki_schemes=secp256k1") #TODO this infects everything in this folder, ideally it would only # apply to codex.nim, but since codex.nims is used for other purpose # we can't use it. And codex.cfg doesn't work -switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic]") +switch("define", "chronicles_sinks=textlines[dynamic],json[dynamic],textlines[dynamic]") # begin Nimble config (version 1) when system.fileExists("nimble.paths"):