diff --git a/Cargo.lock b/Cargo.lock index 5c54ea02..11725ff2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2684,6 +2684,7 @@ dependencies = [ "tikv-jemallocator", "tokio", "tracing", + "tracing-appender", "tracing-flame", "tracing-subscriber", ] @@ -4204,6 +4205,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.27" diff --git a/Cargo.toml b/Cargo.toml index 4c496614..37966150 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ tarpc = { version = "0.34", features = ["tokio1", "serde1"] } thiserror = "1.0" tokio = { version = "1", features = ["full", "macros"] } tracing = "0.1" +tracing-appender = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } nix = { version = "0.28", features = ["process"] } diff --git a/crates/aot/Cargo.toml b/crates/aot/Cargo.toml index b9f28c05..9c9d3c34 100644 --- a/crates/aot/Cargo.toml +++ b/crates/aot/Cargo.toml @@ -31,6 +31,7 @@ snarkvm.workspace = true snot-common.workspace = true tokio.workspace = true tracing-flame = "0.2.0" +tracing-appender.workspace = true tracing-subscriber.workspace = true tracing.workspace = true crossterm = { version = "0.27", optional = true } diff --git a/crates/aot/src/cli.rs b/crates/aot/src/cli.rs index fedee1af..c483314c 100644 --- a/crates/aot/src/cli.rs +++ b/crates/aot/src/cli.rs @@ -1,11 +1,13 @@ -use anyhow::Result; -use clap::Parser; -use crossterm::tty::IsTty; use std::{ fs::File, io::{self, BufWriter}, path::PathBuf, }; + +use anyhow::Result; +use clap::Parser; +use crossterm::tty::IsTty; +use tracing_appender::non_blocking::WorkerGuard; use tracing_flame::FlushGuard; use tracing_subscriber::{layer::SubscriberExt, Layer}; @@ -48,7 +50,7 @@ impl Cli { /// 5 => info, debug, trace, snarkos_node_router=trace /// 6 => info, debug, trace, snarkos_node_tcp=trace /// ``` - pub fn init_logger(&self) -> Option>> { + pub fn init_logger(&self) -> (Option>>, Vec) { let verbosity = self.verbosity; match verbosity { @@ -101,6 +103,7 @@ impl Cli { }); let mut layers = vec![]; + let mut guards = vec![]; let guard = if self.enable_profiling { let (flame_layer, guard) = @@ -111,7 +114,7 @@ impl Cli { None }; - if let Some(logfile) = &self.log { + if let Some(logfile) = self.log.as_ref() { // Create the directories tree for a logfile if it doesn't exist. let logfile_dir = logfile .parent() @@ -120,19 +123,16 @@ impl Cli { std::fs::create_dir_all(logfile_dir) .expect("Failed to create a directories: '{logfile_dir}', please check if user has permissions"); } - // Create a file to write logs to. - // TODO: log rotation - let logfile = File::options() - .append(true) - .create(true) - .open(logfile) - .expect("Failed to open the file for writing logs"); + + let file_appender = tracing_appender::rolling::daily(&logfile_dir, logfile); + let (non_blocking, file_guard) = tracing_appender::non_blocking(file_appender); + guards.push(file_guard); // Add layer redirecting logs to the file layers.push( tracing_subscriber::fmt::Layer::default() .with_ansi(false) - .with_writer(logfile) + .with_writer(non_blocking) .with_filter(filter2) .boxed(), ); @@ -141,27 +141,34 @@ impl Cli { // Initialize tracing. // Add layer using LogWriter for stdout / terminal if matches!(self.command, Command::Run(_)) { + let (stdout, g) = tracing_appender::non_blocking(io::stdout()); + guards.push(g); + layers.push( tracing_subscriber::fmt::Layer::default() .with_ansi(io::stdout().is_tty()) + .with_writer(stdout) .with_filter(filter) .boxed(), ); } else { + let (stderr, g) = tracing_appender::non_blocking(io::stderr()); + guards.push(g); layers.push( tracing_subscriber::fmt::Layer::default() - .with_writer(io::stderr) + .with_writer(stderr) .boxed(), ); - } + }; let subscriber = tracing_subscriber::registry::Registry::default().with(layers); + tracing::subscriber::set_global_default(subscriber).unwrap(); - guard + (guard, guards) } pub fn run(self) -> Result<()> { - self.init_logger(); + let _guards = self.init_logger(); match self.command { Command::Genesis(command) => command.parse(), diff --git a/crates/aot/src/runner.rs b/crates/aot/src/runner.rs index b89ab8f7..5b51b113 100644 --- a/crates/aot/src/runner.rs +++ b/crates/aot/src/runner.rs @@ -34,11 +34,6 @@ pub struct Runner { #[clap(long = "private-key")] pub private_key: PrivateKey, - /// A path to the log file - #[clap(long = "log")] - #[serde_clap_default(PathBuf::from("snarkos.log"))] - pub log: PathBuf, - /// Specify the IP address and port for the node server #[clap(long = "node")] #[serde_clap_default(4130)] diff --git a/scripts/start.sh b/scripts/start.sh index 27df49b7..c137368b 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -32,14 +32,14 @@ STORAGE="${LEDGER}_${MODE}_${INDEX}" rm -rf $STORAGE cp -r $LEDGER $STORAGE - -$BINARY run --type $MODE \ +echo "Starting $MODE $INDEX" "${TEST_PATH}/${MODE}_${INDEX}.log" +$BINARY --log "${TEST_PATH}/${MODE}_${INDEX}.log" \ + run --type $MODE \ --bft "500$INDEX" \ --rest "303$INDEX" \ --node "413$INDEX" \ --genesis $GENESIS \ --ledger $STORAGE \ - --log "${TEST_PATH}/${MODE}_${INDEX}.log" \ --private-key $(pk 0) \ --peers "127.0.0.1:4130,127.0.0.1:4131,127.0.0.1:4132,127.0.0.1:4133,127.0.0.1:4134" \ --validators "127.0.0.1:5000,127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003,127.0.0.1:5004,127.0.0.1:5005"