From 8905f541a617b755be61fd8ab86d5691c0398879 Mon Sep 17 00:00:00 2001 From: gabriele-0201 Date: Tue, 13 Feb 2024 12:42:39 +0100 Subject: [PATCH] xtask: redirect stdout and stderr into log files --- .gitignore | 1 + xtask/src/build.rs | 31 +++++++------- xtask/src/cli.rs | 42 +++++++++++++------ xtask/src/logging.rs | 51 +++++++++++++++++++++++ xtask/src/main.rs | 15 +------ xtask/src/shim.rs | 21 ++++++---- xtask/src/sovereign.rs | 94 ++++++++++++++++++++++++------------------ xtask/src/zombienet.rs | 12 +++--- 8 files changed, 172 insertions(+), 95 deletions(-) create mode 100644 xtask/src/logging.rs diff --git a/.gitignore b/.gitignore index 6535bff6..420e1ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /demo/sovereign/target /demo/sovereign/demo-rollup/demo_data /target +/test_log diff --git a/xtask/src/build.rs b/xtask/src/build.rs index 5dfed2fe..d052ac26 100644 --- a/xtask/src/build.rs +++ b/xtask/src/build.rs @@ -1,6 +1,5 @@ -use crate::{cli::test::BuildParams, run_maybe_quiet}; +use crate::{cli::test::BuildParams, logging::create_with_logs}; use duct::cmd; -use tracing::info; // TODO: https://github.com/thrumdev/blobs/issues/225 @@ -9,28 +8,30 @@ pub fn build(params: BuildParams) -> anyhow::Result<()> { return Ok(()); } + tracing::info!("Building logs redirected {}", params.log_path); + let with_logs = create_with_logs(params.log_path); + // `it is advisable to use CARGO environmental variable to get the right cargo` // quoted by xtask readme let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); - info!("Start building sugondat-node"); - run_maybe_quiet( + with_logs( + "Building sugondat-node", cmd!(&cargo, "build", "-p", "sugondat-node", "--release"), - params.quiet, - )?; + ) + .run()?; - info!("Start building sugondat-node"); - run_maybe_quiet( + with_logs( + "Building sugondat-shim", cmd!(&cargo, "build", "-p", "sugondat-shim", "--release"), - params.quiet, - )?; - - info!("Start building sovereign demo-rollup"); + ) + .run()?; - run_maybe_quiet( + with_logs( + "Building sovereign demo-rollup", cmd!(&cargo, "build", "--release").dir("demo/sovereign/demo-rollup"), - params.quiet, - )?; + ) + .run()?; Ok(()) } diff --git a/xtask/src/cli.rs b/xtask/src/cli.rs index 75332a4e..3909d369 100644 --- a/xtask/src/cli.rs +++ b/xtask/src/cli.rs @@ -36,32 +36,48 @@ pub mod test { /// Skip building required binaries /// (sugondat-node, sugondat-shim, sov-demo-rollup and sov-cli) #[clap(default_value = "false")] - #[arg(long = "build-skip", value_name = "skip", id = "build.skip")] + #[arg(long = "skip-build", value_name = "skip", id = "build.skip")] pub skip: bool, - /// Don't print to stdout during the build process - #[arg(long = "build-quiet", value_name = "quiet", id = "build.quiet")] - pub quiet: bool, + /// Build process stdout and stderr are redirected into this file + #[arg( + long = "build-log-path", + value_name = "log-path", + id = "build.log-path" + )] + #[clap(default_value = "test_log/build.log")] + pub log_path: String, } #[derive(clap::Args, Debug, Clone)] pub struct ShimParams { - /// Don't print shim process stdout - #[arg(long = "shim-quiet", value_name = "quiet", id = "shim.quiet")] - pub quiet: bool, + /// Shim process stdout and stderr are redirected into this file + #[arg(long = "shim-log-path", value_name = "log-path", id = "shim.log-path")] + #[clap(default_value = "test_log/shim.log")] + pub log_path: String, } #[derive(clap::Args, Debug, Clone)] pub struct ZombienetParams { - /// Don't print zombienet process stdout - #[arg(long = "zombienet-quiet", value_name = "quiet", id = "zombienet.quiet")] - pub quiet: bool, + /// Zombienet process stdout and stderr are redirected into this file + #[arg( + long = "zombienet-log-path", + value_name = "log-path", + id = "zombienet.log-path" + )] + #[clap(default_value = "test_log/zombienet.log")] + pub log_path: String, } #[derive(clap::Args, Debug, Clone)] pub struct SovereignParams { - /// Don't print sovereing rollup processes stdout - #[arg(long = "sovereign-quiet", value_name = "quiet", id = "sovereign.quiet")] - pub quiet: bool, + /// Sovereign rollup process stdout and stderr are redirected into this file + #[arg( + long = "sovereign-log-path", + value_name = "log-path", + id = "sovereign.log-path" + )] + #[clap(default_value = "test_log/sovereign.log")] + pub log_path: String, } } diff --git a/xtask/src/logging.rs b/xtask/src/logging.rs new file mode 100644 index 00000000..77ec2fd6 --- /dev/null +++ b/xtask/src/logging.rs @@ -0,0 +1,51 @@ +use std::io::Write; +use std::path::Path; +use tracing::{info, warn}; + +fn create_log_file(log_path: &String) -> std::io::Result<()> { + if let Some(prefix) = Path::new(&log_path).parent() { + std::fs::create_dir_all(prefix)?; + } + std::fs::File::create(&log_path)?; + Ok(()) +} + +// If the log file cannot be created due to any reasons, +// such as lack of permission to create files or new folders in the path, +// things will be printed to stdout instead of being redirected to the logs file +// +// The returned closure will accept a description of the command and the command itself as a duct::Expression. +// The description will be printed to both stdout and the log file, if possible, while +// to the expression will be added the redirection of the logs, if possible. +pub fn create_with_logs( + log_path: String, +) -> Box duct::Expression> { + let without_logs = |description: &str, cmd: duct::Expression| -> duct::Expression { + info!("{description}"); + cmd + }; + + if let Err(e) = create_log_file(&log_path) { + warn!("Impossible redirect to {log_path}, using stdout instead. Error: {e}"); + return Box::new(without_logs); + } + + let with_logs = move |description: &str, cmd: duct::Expression| -> duct::Expression { + // The file has just been created + let mut log_file = std::fs::File::options() + .append(true) + .open(&log_path) + .unwrap(); + + info!("{description}"); + let _ = log_file + .write(format!("{}/n", description).as_bytes()) + .map_err(|e| warn!("Error writing into {log_path}, error: {e}")); + let _ = log_file + .flush() + .map_err(|e| warn!("Error writing into {log_path}, error: {e}")); + cmd.stderr_to_stdout().stdout_file(log_file) + }; + + Box::new(with_logs) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2cc3ec42..eea2761b 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,5 +1,6 @@ mod build; mod cli; +mod logging; mod shim; mod sovereign; mod zombienet; @@ -50,17 +51,3 @@ fn init_logging() -> anyhow::Result<()> { .try_init()?; Ok(()) } - -fn run_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result { - match quiet { - true => cmd.stdout_null().run(), - false => cmd.run(), - } -} - -fn start_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result { - match quiet { - true => cmd.stdout_null().start(), - false => cmd.start(), - } -} diff --git a/xtask/src/shim.rs b/xtask/src/shim.rs index bb3ab086..dc1ab9f1 100644 --- a/xtask/src/shim.rs +++ b/xtask/src/shim.rs @@ -1,4 +1,4 @@ -use crate::{cli::test::ShimParams, run_maybe_quiet, start_maybe_quiet}; +use crate::{cli::test::ShimParams, logging::create_with_logs}; use duct::cmd; use tracing::info; @@ -7,18 +7,21 @@ pub struct Shim(duct::Handle); impl Shim { // Try launching the shim, it requires an up an running sugondat-node pub fn try_new(params: ShimParams) -> anyhow::Result { + tracing::info!("Shim logs redirected to {}", params.log_path); + let with_logs = create_with_logs(params.log_path); + // Wait for the shim to be connected, which indicates that the network is ready - info!("Wait for the network to be ready"); - run_maybe_quiet( + with_logs( + "Wait for the network to be ready", cmd!("sugondat-shim", "query", "block", "--wait", "1",).dir("target/release/"), - params.quiet, - )?; + ) + .run()?; - info!("Launching Shim"); - let shim_handle = start_maybe_quiet( + let shim_handle = with_logs( + "Launching Shim", cmd!("sugondat-shim", "serve", "--submit-dev-alice").dir("target/release/"), - params.quiet, - )?; + ) + .start()?; Ok(Self(shim_handle)) } diff --git a/xtask/src/sovereign.rs b/xtask/src/sovereign.rs index 77eb7ee0..c7ef4970 100644 --- a/xtask/src/sovereign.rs +++ b/xtask/src/sovereign.rs @@ -1,9 +1,12 @@ -use crate::{cli::test::SovereignParams, start_maybe_quiet}; +use crate::{cli::test::SovereignParams, logging::create_with_logs}; use anyhow::bail; use duct::cmd; use tracing::info; -pub struct Sovereign(duct::Handle); +pub struct Sovereign { + process: duct::Handle, + with_logs: Box duct::Expression>, +} impl Sovereign { // Try launching the sovereing rollup using zombienet @@ -11,20 +14,27 @@ impl Sovereign { info!("Deleting rollup db if it already exists"); cmd!("rm", "-r", "demo/sovereign/demo-rollup/demo_data") .unchecked() + .stderr_null() + .stdout_null() .run()?; + info!("Sovereign logs redirected to {}", params.log_path); + let with_logs = create_with_logs(params.log_path.clone()); + //TODO: https://github.com/thrumdev/blobs/issues/227 - info!("Launching sovereign rollup"); #[rustfmt::skip] - let sovereign_handle = start_maybe_quiet( + let sovereign_handle = with_logs( + "Launching sovereign rollup", cmd!( "sh", "-c", "cd demo/sovereign/demo-rollup && ./../target/release/sov-demo-rollup" ), - params.quiet, - )?; + ).start()?; - Ok(Self(sovereign_handle)) + Ok(Self { + process: sovereign_handle, + with_logs, + }) } // All the networks must be up (relaychain and sugondat-node), including the sovereign rollup." @@ -34,37 +44,43 @@ impl Sovereign { //TODO: https://github.com/thrumdev/blobs/issues/227 let cli = "../target/release/sov-cli"; let test_data_path = "../test-data/"; - let run_cli_cmd = |args: &str| { - let args = [ - "-c", - &format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args), - ]; - - duct::cmd("sh", args).run() - }; - - info!("setup rpc endpoint"); - run_cli_cmd("rpc set-url http://127.0.0.1:12345")?; - - info!("import keys"); - run_cli_cmd(&format!( - "keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json", - test_data_path - ))?; - - info!("create and mint a new token"); - run_cli_cmd(&format!( - "transactions import from-file bank --path {}requests/create_token.json", - test_data_path - ))?; - - run_cli_cmd(&format!( - "transactions import from-file bank --path {}requests/mint.json", - test_data_path - ))?; - - info!("submit batch with two transactions"); - run_cli_cmd("rpc submit-batch by-nickname token_deployer")?; + let run_cli_cmd = + |description: &str, args: &str| -> std::io::Result { + let args = [ + "-c", + &format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args), + ]; + + (self.with_logs)(description, duct::cmd("sh", args)).run() + }; + + run_cli_cmd("setup rpc endpoint", "rpc set-url http://127.0.0.1:12345")?; + + run_cli_cmd( + "import keys", + &format!("keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json", test_data_path), + )?; + + run_cli_cmd( + "create a new token", + &format!( + "transactions import from-file bank --path {}requests/create_token.json", + test_data_path + ), + )?; + + run_cli_cmd( + "mint just created token", + &format!( + "transactions import from-file bank --path {}requests/mint.json", + test_data_path + ), + )?; + + run_cli_cmd( + "submit batch with two transactions", + "rpc submit-batch by-nickname token_deployer", + )?; // TODO: https://github.com/thrumdev/blobs/issues/226 info!("waiting for the rollup to process the transactions"); @@ -90,6 +106,6 @@ impl Drop for Sovereign { // duct::Handle does not implement kill on drop fn drop(&mut self) { info!("Sovereign rollup process is going to be killed"); - let _ = self.0.kill(); + let _ = self.process.kill(); } } diff --git a/xtask/src/zombienet.rs b/xtask/src/zombienet.rs index cc540a80..e96c9186 100644 --- a/xtask/src/zombienet.rs +++ b/xtask/src/zombienet.rs @@ -1,4 +1,4 @@ -use crate::{cli::test::ZombienetParams, start_maybe_quiet}; +use crate::{cli::test::ZombienetParams, logging::create_with_logs}; use duct::cmd; use tracing::info; @@ -43,12 +43,14 @@ impl Zombienet { cd to 'sugondat/chain' and run 'cargo build --release' and add the result into your PATH." )?; - info!("Launching zombienet"); + tracing::info!("Zombienet logs redirected to {}", params.log_path); + let with_logs = create_with_logs(params.log_path); + #[rustfmt::skip] - let zombienet_handle = start_maybe_quiet( + let zombienet_handle = with_logs( + "Launching zombienet", cmd!("zombienet", "spawn", "-p", "native", "--dir", "zombienet", "testnet.toml"), - params.quiet, - )?; + ).start()?; Ok(Self(zombienet_handle)) }