Skip to content

Commit bcd3589

Browse files
gabriele-0201pepyakin
authored andcommitted
xtask: redirect stdout and stderr into log files
1 parent 54b767b commit bcd3589

File tree

8 files changed

+172
-95
lines changed

8 files changed

+172
-95
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/demo/sovereign/target
99
/demo/sovereign/demo-rollup/demo_data
1010
/target
11+
/test_log

xtask/src/build.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{cli::test::BuildParams, run_maybe_quiet};
1+
use crate::{cli::test::BuildParams, logging::create_with_logs};
22
use duct::cmd;
3-
use tracing::info;
43

54
// TODO: https://github.com/thrumdev/blobs/issues/225
65

@@ -9,28 +8,30 @@ pub fn build(params: BuildParams) -> anyhow::Result<()> {
98
return Ok(());
109
}
1110

11+
tracing::info!("Building logs redirected {}", params.log_path);
12+
let with_logs = create_with_logs(params.log_path);
13+
1214
// `it is advisable to use CARGO environmental variable to get the right cargo`
1315
// quoted by xtask readme
1416
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
1517

16-
info!("Start building sugondat-node");
17-
run_maybe_quiet(
18+
with_logs(
19+
"Building sugondat-node",
1820
cmd!(&cargo, "build", "-p", "sugondat-node", "--release"),
19-
params.quiet,
20-
)?;
21+
)
22+
.run()?;
2123

22-
info!("Start building sugondat-node");
23-
run_maybe_quiet(
24+
with_logs(
25+
"Building sugondat-shim",
2426
cmd!(&cargo, "build", "-p", "sugondat-shim", "--release"),
25-
params.quiet,
26-
)?;
27-
28-
info!("Start building sovereign demo-rollup");
27+
)
28+
.run()?;
2929

30-
run_maybe_quiet(
30+
with_logs(
31+
"Building sovereign demo-rollup",
3132
cmd!(&cargo, "build", "--release").dir("demo/sovereign/demo-rollup"),
32-
params.quiet,
33-
)?;
33+
)
34+
.run()?;
3435

3536
Ok(())
3637
}

xtask/src/cli.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,48 @@ pub mod test {
3636
/// Skip building required binaries
3737
/// (sugondat-node, sugondat-shim, sov-demo-rollup and sov-cli)
3838
#[clap(default_value = "false")]
39-
#[arg(long = "build-skip", value_name = "skip", id = "build.skip")]
39+
#[arg(long = "skip-build", value_name = "skip", id = "build.skip")]
4040
pub skip: bool,
4141

42-
/// Don't print to stdout during the build process
43-
#[arg(long = "build-quiet", value_name = "quiet", id = "build.quiet")]
44-
pub quiet: bool,
42+
/// Build process stdout and stderr are redirected into this file
43+
#[arg(
44+
long = "build-log-path",
45+
value_name = "log-path",
46+
id = "build.log-path"
47+
)]
48+
#[clap(default_value = "test_log/build.log")]
49+
pub log_path: String,
4550
}
4651

4752
#[derive(clap::Args, Debug, Clone)]
4853
pub struct ShimParams {
49-
/// Don't print shim process stdout
50-
#[arg(long = "shim-quiet", value_name = "quiet", id = "shim.quiet")]
51-
pub quiet: bool,
54+
/// Shim process stdout and stderr are redirected into this file
55+
#[arg(long = "shim-log-path", value_name = "log-path", id = "shim.log-path")]
56+
#[clap(default_value = "test_log/shim.log")]
57+
pub log_path: String,
5258
}
5359

5460
#[derive(clap::Args, Debug, Clone)]
5561
pub struct ZombienetParams {
56-
/// Don't print zombienet process stdout
57-
#[arg(long = "zombienet-quiet", value_name = "quiet", id = "zombienet.quiet")]
58-
pub quiet: bool,
62+
/// Zombienet process stdout and stderr are redirected into this file
63+
#[arg(
64+
long = "zombienet-log-path",
65+
value_name = "log-path",
66+
id = "zombienet.log-path"
67+
)]
68+
#[clap(default_value = "test_log/zombienet.log")]
69+
pub log_path: String,
5970
}
6071

6172
#[derive(clap::Args, Debug, Clone)]
6273
pub struct SovereignParams {
63-
/// Don't print sovereing rollup processes stdout
64-
#[arg(long = "sovereign-quiet", value_name = "quiet", id = "sovereign.quiet")]
65-
pub quiet: bool,
74+
/// Sovereign rollup process stdout and stderr are redirected into this file
75+
#[arg(
76+
long = "sovereign-log-path",
77+
value_name = "log-path",
78+
id = "sovereign.log-path"
79+
)]
80+
#[clap(default_value = "test_log/sovereign.log")]
81+
pub log_path: String,
6682
}
6783
}

xtask/src/logging.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::io::Write;
2+
use std::path::Path;
3+
use tracing::{info, warn};
4+
5+
fn create_log_file(log_path: &String) -> std::io::Result<()> {
6+
if let Some(prefix) = Path::new(&log_path).parent() {
7+
std::fs::create_dir_all(prefix)?;
8+
}
9+
std::fs::File::create(&log_path)?;
10+
Ok(())
11+
}
12+
13+
// If the log file cannot be created due to any reasons,
14+
// such as lack of permission to create files or new folders in the path,
15+
// things will be printed to stdout instead of being redirected to the logs file
16+
//
17+
// The returned closure will accept a description of the command and the command itself as a duct::Expression.
18+
// The description will be printed to both stdout and the log file, if possible, while
19+
// to the expression will be added the redirection of the logs, if possible.
20+
pub fn create_with_logs(
21+
log_path: String,
22+
) -> Box<dyn Fn(&str, duct::Expression) -> duct::Expression> {
23+
let without_logs = |description: &str, cmd: duct::Expression| -> duct::Expression {
24+
info!("{description}");
25+
cmd
26+
};
27+
28+
if let Err(e) = create_log_file(&log_path) {
29+
warn!("Impossible redirect to {log_path}, using stdout instead. Error: {e}");
30+
return Box::new(without_logs);
31+
}
32+
33+
let with_logs = move |description: &str, cmd: duct::Expression| -> duct::Expression {
34+
// The file has just been created
35+
let mut log_file = std::fs::File::options()
36+
.append(true)
37+
.open(&log_path)
38+
.unwrap();
39+
40+
info!("{description}");
41+
let _ = log_file
42+
.write(format!("{}/n", description).as_bytes())
43+
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
44+
let _ = log_file
45+
.flush()
46+
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
47+
cmd.stderr_to_stdout().stdout_file(log_file)
48+
};
49+
50+
Box::new(with_logs)
51+
}

xtask/src/main.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod build;
22
mod cli;
3+
mod logging;
34
mod shim;
45
mod sovereign;
56
mod zombienet;
@@ -50,17 +51,3 @@ fn init_logging() -> anyhow::Result<()> {
5051
.try_init()?;
5152
Ok(())
5253
}
53-
54-
fn run_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result<std::process::Output> {
55-
match quiet {
56-
true => cmd.stdout_null().run(),
57-
false => cmd.run(),
58-
}
59-
}
60-
61-
fn start_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result<duct::Handle> {
62-
match quiet {
63-
true => cmd.stdout_null().start(),
64-
false => cmd.start(),
65-
}
66-
}

xtask/src/shim.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{cli::test::ShimParams, run_maybe_quiet, start_maybe_quiet};
1+
use crate::{cli::test::ShimParams, logging::create_with_logs};
22
use duct::cmd;
33
use tracing::info;
44

@@ -7,18 +7,21 @@ pub struct Shim(duct::Handle);
77
impl Shim {
88
// Try launching the shim, it requires an up an running sugondat-node
99
pub fn try_new(params: ShimParams) -> anyhow::Result<Self> {
10+
tracing::info!("Shim logs redirected to {}", params.log_path);
11+
let with_logs = create_with_logs(params.log_path);
12+
1013
// Wait for the shim to be connected, which indicates that the network is ready
11-
info!("Wait for the network to be ready");
12-
run_maybe_quiet(
14+
with_logs(
15+
"Wait for the network to be ready",
1316
cmd!("sugondat-shim", "query", "block", "--wait", "1",).dir("target/release/"),
14-
params.quiet,
15-
)?;
17+
)
18+
.run()?;
1619

17-
info!("Launching Shim");
18-
let shim_handle = start_maybe_quiet(
20+
let shim_handle = with_logs(
21+
"Launching Shim",
1922
cmd!("sugondat-shim", "serve", "--submit-dev-alice").dir("target/release/"),
20-
params.quiet,
21-
)?;
23+
)
24+
.start()?;
2225

2326
Ok(Self(shim_handle))
2427
}

xtask/src/sovereign.rs

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1-
use crate::{cli::test::SovereignParams, start_maybe_quiet};
1+
use crate::{cli::test::SovereignParams, logging::create_with_logs};
22
use anyhow::bail;
33
use duct::cmd;
44
use tracing::info;
55

6-
pub struct Sovereign(duct::Handle);
6+
pub struct Sovereign {
7+
process: duct::Handle,
8+
with_logs: Box<dyn Fn(&str, duct::Expression) -> duct::Expression>,
9+
}
710

811
impl Sovereign {
912
// Try launching the sovereing rollup using zombienet
1013
pub fn try_new(params: SovereignParams) -> anyhow::Result<Self> {
1114
info!("Deleting rollup db if it already exists");
1215
cmd!("rm", "-r", "demo/sovereign/demo-rollup/demo_data")
1316
.unchecked()
17+
.stderr_null()
18+
.stdout_null()
1419
.run()?;
1520

21+
info!("Sovereign logs redirected to {}", params.log_path);
22+
let with_logs = create_with_logs(params.log_path.clone());
23+
1624
//TODO: https://github.com/thrumdev/blobs/issues/227
17-
info!("Launching sovereign rollup");
1825
#[rustfmt::skip]
19-
let sovereign_handle = start_maybe_quiet(
26+
let sovereign_handle = with_logs(
27+
"Launching sovereign rollup",
2028
cmd!(
2129
"sh", "-c",
2230
"cd demo/sovereign/demo-rollup && ./../target/release/sov-demo-rollup"
2331
),
24-
params.quiet,
25-
)?;
32+
).start()?;
2633

27-
Ok(Self(sovereign_handle))
34+
Ok(Self {
35+
process: sovereign_handle,
36+
with_logs,
37+
})
2838
}
2939

3040
// All the networks must be up (relaychain and sugondat-node), including the sovereign rollup."
@@ -34,37 +44,43 @@ impl Sovereign {
3444
//TODO: https://github.com/thrumdev/blobs/issues/227
3545
let cli = "../target/release/sov-cli";
3646
let test_data_path = "../test-data/";
37-
let run_cli_cmd = |args: &str| {
38-
let args = [
39-
"-c",
40-
&format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args),
41-
];
42-
43-
duct::cmd("sh", args).run()
44-
};
45-
46-
info!("setup rpc endpoint");
47-
run_cli_cmd("rpc set-url http://127.0.0.1:12345")?;
48-
49-
info!("import keys");
50-
run_cli_cmd(&format!(
51-
"keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json",
52-
test_data_path
53-
))?;
54-
55-
info!("create and mint a new token");
56-
run_cli_cmd(&format!(
57-
"transactions import from-file bank --path {}requests/create_token.json",
58-
test_data_path
59-
))?;
60-
61-
run_cli_cmd(&format!(
62-
"transactions import from-file bank --path {}requests/mint.json",
63-
test_data_path
64-
))?;
65-
66-
info!("submit batch with two transactions");
67-
run_cli_cmd("rpc submit-batch by-nickname token_deployer")?;
47+
let run_cli_cmd =
48+
|description: &str, args: &str| -> std::io::Result<std::process::Output> {
49+
let args = [
50+
"-c",
51+
&format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args),
52+
];
53+
54+
(self.with_logs)(description, duct::cmd("sh", args)).run()
55+
};
56+
57+
run_cli_cmd("setup rpc endpoint", "rpc set-url http://127.0.0.1:12345")?;
58+
59+
run_cli_cmd(
60+
"import keys",
61+
&format!("keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json", test_data_path),
62+
)?;
63+
64+
run_cli_cmd(
65+
"create a new token",
66+
&format!(
67+
"transactions import from-file bank --path {}requests/create_token.json",
68+
test_data_path
69+
),
70+
)?;
71+
72+
run_cli_cmd(
73+
"mint just created token",
74+
&format!(
75+
"transactions import from-file bank --path {}requests/mint.json",
76+
test_data_path
77+
),
78+
)?;
79+
80+
run_cli_cmd(
81+
"submit batch with two transactions",
82+
"rpc submit-batch by-nickname token_deployer",
83+
)?;
6884

6985
// TODO: https://github.com/thrumdev/blobs/issues/226
7086
info!("waiting for the rollup to process the transactions");
@@ -90,6 +106,6 @@ impl Drop for Sovereign {
90106
// duct::Handle does not implement kill on drop
91107
fn drop(&mut self) {
92108
info!("Sovereign rollup process is going to be killed");
93-
let _ = self.0.kill();
109+
let _ = self.process.kill();
94110
}
95111
}

xtask/src/zombienet.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{cli::test::ZombienetParams, start_maybe_quiet};
1+
use crate::{cli::test::ZombienetParams, logging::create_with_logs};
22
use duct::cmd;
33
use tracing::info;
44

@@ -43,12 +43,14 @@ impl Zombienet {
4343
cd to 'sugondat/chain' and run 'cargo build --release' and add the result into your PATH."
4444
)?;
4545

46-
info!("Launching zombienet");
46+
tracing::info!("Zombienet logs redirected to {}", params.log_path);
47+
let with_logs = create_with_logs(params.log_path);
48+
4749
#[rustfmt::skip]
48-
let zombienet_handle = start_maybe_quiet(
50+
let zombienet_handle = with_logs(
51+
"Launching zombienet",
4952
cmd!("zombienet", "spawn", "-p", "native", "--dir", "zombienet", "testnet.toml"),
50-
params.quiet,
51-
)?;
53+
).start()?;
5254

5355
Ok(Self(zombienet_handle))
5456
}

0 commit comments

Comments
 (0)