Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cmd/ethrex/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{
fmt::Display,
fs::{File, metadata, read_dir},
io::{self, Write},
path::{Path, PathBuf},
str::FromStr,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<Self, Self::Err> {
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);

Expand Down
6 changes: 4 additions & 2 deletions cmd/ethrex/initializers.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -58,7 +58,9 @@ pub fn init_tracing(opts: &Options) -> reload::Handle<EnvFilter, Registry> {

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);
}

Expand Down
10 changes: 10 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Node options:

[default: INFO]

--log.color <LOG_COLOR>
Possible values: auto, always, never

[default: auto]

--mempool.maxsize <MEMPOOL_MAX_SIZE>
Maximum size of the mempool in number of transactions

Expand Down Expand Up @@ -214,6 +219,11 @@ Node options:

[default: INFO]

--log.color <LOG_COLOR>
Possible values: auto, always, never

[default: auto]

--mempool.maxsize <MEMPOOL_MAX_SIZE>
Maximum size of the mempool in number of transactions

Expand Down
Loading