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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ethers-middleware = { version = "2.0.8", default-features = false }
## misc
bytes = "1.4"
tracing = "0.1.0"
tracing-appender = "0.2"
thiserror = "1.0.37"
serde_json = "1.0.94"
serde = { version = "1.0", default-features = false }
Expand Down
93 changes: 55 additions & 38 deletions bin/reth/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,64 @@ use reth_tracing::{
BoxedLayer, FileWorkerGuard,
};

/// Parse CLI options, set up logging and run the chosen command.
pub fn run() -> eyre::Result<()> {
let opt = Cli::parse();

let mut layers = vec![reth_tracing::stdout(opt.verbosity.directive())];
let _guard = opt.logs.layer()?.map(|(layer, guard)| {
layers.push(layer);
guard
});

reth_tracing::init(layers);

let runner = CliRunner::default();

match opt.command {
Commands::Node(command) => runner.run_command_until_exit(|ctx| command.execute(ctx)),
Commands::Init(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Import(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Db(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Stage(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Debug(command) => runner.run_command_until_exit(|ctx| command.execute(ctx)),
/// The main reth cli interface.
///
/// This is the entrypoint to the executable.
#[derive(Debug, Parser)]
#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)]
pub struct Cli {
/// The command to run
#[clap(subcommand)]
command: Commands,

#[clap(flatten)]
logs: Logs,

#[clap(flatten)]
verbosity: Verbosity,
}

impl Cli {
/// Execute the configured cli command.
pub fn run(self) -> eyre::Result<()> {
let _guard = self.init_tracing()?;

let runner = CliRunner::default();
match self.command {
Commands::Node(command) => runner.run_command_until_exit(|ctx| command.execute(ctx)),
Commands::Init(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Import(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Db(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Stage(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Debug(command) => runner.run_command_until_exit(|ctx| command.execute(ctx)),
}
}

/// Initializes tracing with the configured options.
///
/// If file logging is enabled, this function returns a guard that must be kept alive to ensure
/// that all logs are flushed to disk.
pub fn init_tracing(&self) -> eyre::Result<Option<FileWorkerGuard>> {
let mut layers = vec![reth_tracing::stdout(self.verbosity.directive())];
let guard = self.logs.layer()?.map(|(layer, guard)| {
layers.push(layer);
guard
});

reth_tracing::init(layers);
Ok(guard.flatten())
}
}

/// Convenience function for parsing CLI options, set up logging and run the chosen command.
#[inline]
pub fn run() -> eyre::Result<()> {
Cli::parse().run()
}

/// Commands to be executed
#[derive(Debug, Subcommand)]
pub enum Commands {
Expand Down Expand Up @@ -73,20 +104,6 @@ pub enum Commands {
Debug(debug_cmd::Command),
}

#[derive(Debug, Parser)]
#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)]
struct Cli {
/// The command to run
#[clap(subcommand)]
command: Commands,

#[clap(flatten)]
logs: Logs,

#[clap(flatten)]
verbosity: Verbosity,
}

/// The log configuration.
#[derive(Debug, Args)]
#[command(next_help_heading = "Logging")]
Expand Down
2 changes: 1 addition & 1 deletion crates/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ description = "tracing helpers"
[dependencies]
tracing.workspace = true
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }
tracing-appender = "0.2"
tracing-appender.workspace = true
tracing-journald = "0.3"
1 change: 1 addition & 0 deletions crates/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ where
///
/// The boxed layer and a guard is returned. When the guard is dropped the buffer for the log
/// file is immediately flushed to disk. Any events after the guard is dropped may be missed.
#[must_use = "tracing guard must be kept alive to flush events to disk"]
pub fn file<S>(
filter: EnvFilter,
dir: impl AsRef<Path>,
Expand Down