From 6cf57d99126976f9dfb79c398bb6515446f8a313 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Mon, 15 Jul 2024 11:41:17 +0200 Subject: [PATCH] Remove duplicate logging code from fluffy (#2488) Code was copied back when nimbus-eth2 was not a dependency. Now that is an established dependency for many parts, lets reuse the original code. --- fluffy/fluffy.nim | 2 +- fluffy/logging.nim | 148 +------------------ fluffy/tools/portal_bridge/portal_bridge.nim | 2 +- 3 files changed, 7 insertions(+), 145 deletions(-) diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index 7c67cf368..d61779627 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -43,7 +43,7 @@ func optionToOpt[T](o: Option[T]): Opt[T] = Opt.none(T) proc run(config: PortalConf) {.raises: [CatchableError].} = - setupLogging(config.logLevel, config.logStdout) + setupLogging(config.logLevel, config.logStdout, none(OutFile)) notice "Launching Fluffy", version = fullVersionStr, cmdParams = commandLineParams() diff --git a/fluffy/logging.nim b/fluffy/logging.nim index 7b8106bdf..348467424 100644 --- a/fluffy/logging.nim +++ b/fluffy/logging.nim @@ -1,152 +1,14 @@ -# Nimbus Fluffy +# Fluffy # Copyright (c) 2023-2024 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -# Note: -# Code taken from nimbus-eth2/beacon_chain/nimbus_binary_common with minor -# adjustments. The write to file logic is removed as it never was an option -# in Fluffy. - {.push raises: [].} -import - std/[strutils, tables, terminal, typetraits], - pkg/chronicles, - pkg/chronicles/helpers, - chronicles/topics_registry, - pkg/results +# logging.nim is only importing and re-exporting to make the logging import +# clean without bring in additional code from nimbus_binary_common. +from beacon_chain/nimbus_binary_common import setupLogging, StdoutLogKind -export results - -type StdoutLogKind* {.pure.} = enum - Auto = "auto" - Colors = "colors" - NoColors = "nocolors" - Json = "json" - None = "none" - -# silly chronicles, colors is a compile-time property -proc stripAnsi(v: string): string = - var - res = newStringOfCap(v.len) - i: int - - while i < v.len: - let c = v[i] - if c == '\x1b': - var - x = i + 1 - found = false - - while x < v.len: # look for [..m - let c2 = v[x] - if x == i + 1: - if c2 != '[': - break - else: - if c2 in {'0' .. '9'} + {';'}: - discard # keep looking - elif c2 == 'm': - i = x + 1 - found = true - break - else: - break - inc x - - if found: # skip adding c - continue - res.add c - inc i - - res - -proc updateLogLevel(logLevel: string) {.raises: [ValueError].} = - # Updates log levels (without clearing old ones) - let directives = logLevel.split(";") - try: - setLogLevel(parseEnum[LogLevel](directives[0].capitalizeAscii())) - except ValueError: - raise (ref ValueError)( - msg: "Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL" - ) - - if directives.len > 1: - for topicName, settings in parseTopicDirectives(directives[1 ..^ 1]): - if not setTopicState(topicName, settings.state, settings.logLevel): - warn "Unrecognized logging topic", topic = topicName - -proc detectTTY(stdoutKind: StdoutLogKind): StdoutLogKind = - if stdoutKind == StdoutLogKind.Auto: - if isatty(stdout): - # On a TTY, let's be fancy - StdoutLogKind.Colors - else: - # When there's no TTY, we output no colors because this matches what - # released binaries were doing before auto-detection was around and - # looks decent in systemd-captured journals. - StdoutLogKind.NoColors - else: - stdoutKind - -proc setupLogging*(logLevel: string, stdoutKind: StdoutLogKind) = - # In the cfg file for fluffy, we create two formats: textlines and json. - # Here, we either write those logs to an output, or not, depending on the - # given configuration. - # Arguably, if we don't use a format, chronicles should not create it. - - when defaultChroniclesStream.outputs.type.arity != 2: - warn "Logging configuration options not enabled in the current build" - else: - # Naive approach where chronicles will form a string and we will discard - # it, even if it could have skipped the formatting phase - proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = - discard - - proc writeAndFlush(f: File, msg: LogOutputStr) = - try: - f.write(msg) - f.flushFile() - except IOError as err: - logLoggingFailure(cstring(msg), err) - - proc stdoutFlush(logLevel: LogLevel, msg: LogOutputStr) = - writeAndFlush(stdout, msg) - - proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) = - writeAndFlush(stdout, stripAnsi(msg)) - - defaultChroniclesStream.outputs[1].writer = noOutput - - let tmp = detectTTY(stdoutKind) - - case tmp - of StdoutLogKind.Auto: - raiseAssert "checked in detectTTY" - of StdoutLogKind.Colors: - defaultChroniclesStream.outputs[0].writer = stdoutFlush - of StdoutLogKind.NoColors: - defaultChroniclesStream.outputs[0].writer = noColorsFlush - of StdoutLogKind.Json: - defaultChroniclesStream.outputs[0].writer = noOutput - - let prevWriter = defaultChroniclesStream.outputs[1].writer - defaultChroniclesStream.outputs[1].writer = proc( - logLevel: LogLevel, msg: LogOutputStr - ) = - stdoutFlush(logLevel, msg) - prevWriter(logLevel, msg) - of StdoutLogKind.None: - defaultChroniclesStream.outputs[0].writer = noOutput - - try: - updateLogLevel(logLevel) - except ValueError as err: - try: - stderr.write "Invalid value for --log-level. " & err.msg - except IOError: - echo "Invalid value for --log-level. " & err.msg - quit 1 +export setupLogging, StdoutLogKind diff --git a/fluffy/tools/portal_bridge/portal_bridge.nim b/fluffy/tools/portal_bridge/portal_bridge.nim index 0857988d3..692fce1df 100644 --- a/fluffy/tools/portal_bridge/portal_bridge.nim +++ b/fluffy/tools/portal_bridge/portal_bridge.nim @@ -59,7 +59,7 @@ when isMainModule: let config = PortalBridgeConf.load() {.push raises: [].} - setupLogging(config.logLevel, config.logStdout) + setupLogging(config.logLevel, config.logStdout, none(OutFile)) case config.cmd of PortalBridgeCmd.beacon: