diff --git a/cmd/ethrex/cli.rs b/cmd/ethrex/cli.rs index 65710e31b73..2c82c31a02e 100644 --- a/cmd/ethrex/cli.rs +++ b/cmd/ethrex/cli.rs @@ -1,7 +1,9 @@ use std::{ + fmt::Display, fs::{File, metadata, read_dir}, io::{self, Write}, path::{Path, PathBuf}, + str::FromStr, time::{Duration, Instant}, }; @@ -110,6 +112,14 @@ pub struct Options { long_help = "Possible values: info, debug, trace, warn, error", help_heading = "Node options")] pub log_level: Level, + #[arg( + long = "log.color", + default_value_t = LogColor::Auto, + help = "Output logs with ANSI color codes.", + long_help = "Possible values: auto, always, never", + help_heading = "Node options" + )] + pub log_color: LogColor, #[arg( help = "Maximum size of the mempool in number of transactions", long = "mempool.maxsize", @@ -291,6 +301,7 @@ impl Default for Options { ws_addr: Default::default(), ws_port: Default::default(), log_level: Level::INFO, + log_color: Default::default(), authrpc_addr: Default::default(), authrpc_port: Default::default(), authrpc_jwtsecret: Default::default(), @@ -434,6 +445,40 @@ impl Subcommand { } } +#[derive(Clone, Debug, Default, PartialEq)] +pub enum LogColor { + #[default] + Auto, + Always, + Never, +} + +impl Display for LogColor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + LogColor::Auto => write!(f, "auto"), + LogColor::Always => write!(f, "always"), + LogColor::Never => write!(f, "never"), + } + } +} + +impl FromStr for LogColor { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "auto" => Ok(LogColor::Auto), + "always" => Ok(LogColor::Always), + "never" => Ok(LogColor::Never), + _ => Err(format!( + "Invalid log color '{}'. Expected: auto, always, or never", + s + )), + } + } +} + pub fn remove_db(datadir: &Path, force: bool) { init_datadir(datadir); diff --git a/cmd/ethrex/initializers.rs b/cmd/ethrex/initializers.rs index 839e7fc7508..43957fdacd9 100644 --- a/cmd/ethrex/initializers.rs +++ b/cmd/ethrex/initializers.rs @@ -1,5 +1,5 @@ use crate::{ - cli::Options, + cli::{LogColor, Options}, utils::{ display_chain_initialization, get_client_version, init_datadir, parse_socket_addr, read_jwtsecret_file, read_node_config_file, @@ -58,7 +58,9 @@ pub fn init_tracing(opts: &Options) -> reload::Handle { let mut layer = fmt::layer(); - if !std::io::stdout().is_terminal() { + if opts.log_color == LogColor::Never + || (opts.log_color == LogColor::Auto && !std::io::stdout().is_terminal()) + { layer = layer.with_ansi(false); } diff --git a/docs/CLI.md b/docs/CLI.md index 3dc58f33926..b7176ee2013 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -56,6 +56,11 @@ Node options: [default: INFO] + --log.color + Possible values: auto, always, never + + [default: auto] + --mempool.maxsize Maximum size of the mempool in number of transactions @@ -214,6 +219,11 @@ Node options: [default: INFO] + --log.color + Possible values: auto, always, never + + [default: auto] + --mempool.maxsize Maximum size of the mempool in number of transactions