|
1 |
| -use std::path::Path; |
2 |
| -use std::{io::Write, path::PathBuf}; |
3 |
| -use tracing::{info, warn}; |
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | +use tracing::warn; |
4 | 3 |
|
5 | 4 | // If log_path is relative it will be made absolute relative to the project_path
|
6 | 5 | //
|
7 | 6 | // The absolute path of where the log file is created is returned
|
8 |
| -fn create_log_file(project_path: &Path, log_path: &String) -> std::io::Result<PathBuf> { |
| 7 | +pub fn create_log_file(project_path: &Path, log_path: &String) -> Option<String> { |
9 | 8 | let mut log_path: PathBuf = Path::new(&log_path).to_path_buf();
|
10 | 9 |
|
11 | 10 | if log_path.is_relative() {
|
12 | 11 | log_path = project_path.join(log_path);
|
13 | 12 | }
|
14 | 13 |
|
15 | 14 | if let Some(prefix) = log_path.parent() {
|
16 |
| - std::fs::create_dir_all(prefix)?; |
| 15 | + std::fs::create_dir_all(prefix) |
| 16 | + .map_err(|e| warn!("Impossible redirect logs, using stdout instead. Error: {e}")) |
| 17 | + .ok()?; |
17 | 18 | }
|
18 |
| - std::fs::File::create(&log_path)?; |
19 |
| - Ok(log_path) |
| 19 | + |
| 20 | + std::fs::File::create(&log_path) |
| 21 | + .map_err(|e| warn!("Impossible redirect logs, using stdout instead. Error: {e}")) |
| 22 | + .ok()?; |
| 23 | + Some(log_path.to_string_lossy().to_string()) |
20 | 24 | }
|
21 | 25 |
|
22 |
| -// If the log file cannot be created due to any reasons, |
23 |
| -// such as lack of permission to create files or new folders in the path, |
24 |
| -// things will be printed to stdout instead of being redirected to the logs file |
| 26 | +// This macro will accept a description of the command, an xshell::Shell, a command |
| 27 | +// (a string on which format will be called), the log_path, and optionally the process group |
| 28 | +// used to create the command. |
25 | 29 | //
|
26 |
| -// The returned closure will accept a description of the command and the command itself as a duct::Expression. |
27 |
| -// The description will be printed to both stdout and the log file, if possible, while |
28 |
| -// to the expression will be added the redirection of the logs, if possible. |
29 |
| -pub fn create_with_logs( |
30 |
| - project_path: &Path, |
31 |
| - log_path: String, |
32 |
| -) -> Box<dyn Fn(&str, duct::Expression) -> duct::Expression> { |
33 |
| - let without_logs = |description: &str, cmd: duct::Expression| -> duct::Expression { |
34 |
| - info!("{description}"); |
35 |
| - cmd |
36 |
| - }; |
37 |
| - |
38 |
| - let log_path = match create_log_file(project_path, &log_path) { |
39 |
| - Ok(log_path) => log_path, |
40 |
| - Err(e) => { |
41 |
| - warn!("Impossible redirect logs, using stdout instead. Error: {e}"); |
42 |
| - return Box::new(without_logs); |
43 |
| - } |
44 |
| - }; |
| 30 | +// The description will be logged with the info log level, and the command will be created |
| 31 | +// using the provided shell and converted to a tokio::process::Command to redirect stdout and stderr. |
| 32 | +// |
| 33 | +// If log is None, then things will be redirected to stdout. |
| 34 | +// Instead, if it contains a path, it will be used to redirect the command's output. |
| 35 | +// |
| 36 | +// The provided path needs to be a valid path, `create_log_file` should be used before use this macro |
| 37 | +#[macro_export] |
| 38 | +macro_rules! cmd_with_logs { |
| 39 | + ($description:expr, $sh:expr, $cmd:literal, $log:expr $(,$gpid:literal)?) => {{ |
| 40 | + use std::process::Stdio; |
| 41 | + tracing::info!("{}", $description); |
| 42 | + let (stdout, stderr) = match $log { |
| 43 | + None => (Stdio::inherit(), Stdio::inherit()), |
| 44 | + Some(ref log_path) => { |
| 45 | + // redirecting stdout and stderr into log_path |
| 46 | + let mut log_out_file = std::fs::File::options() |
| 47 | + .append(true) |
| 48 | + .create(true) |
| 49 | + .open(&log_path) |
| 50 | + .expect("Log file does not exist"); |
| 51 | + let log_err_file = log_out_file.try_clone()?; |
45 | 52 |
|
46 |
| - let with_logs = move |description: &str, cmd: duct::Expression| -> duct::Expression { |
47 |
| - // The file has just been created |
48 |
| - let mut log_file = std::fs::File::options() |
49 |
| - .append(true) |
50 |
| - .open(&log_path) |
51 |
| - .unwrap(); |
| 53 | + use std::io::Write; |
| 54 | + let _ = log_out_file |
| 55 | + .write(format!("{}\n", $description).as_bytes()) |
| 56 | + .map_err(|e| tracing::warn!("Error writing into {log_path}, error: {e}")); |
| 57 | + let _ = log_out_file |
| 58 | + .flush() |
| 59 | + .map_err(|e| tracing::warn!("Error writing into {log_path}, error: {e}")); |
| 60 | + (Stdio::from(log_out_file), Stdio::from(log_err_file)) |
| 61 | + } |
| 62 | + }; |
| 63 | + #[allow(unused_mut)] |
| 64 | + let mut std_cmd = std::process::Command::from(xshell::cmd!($sh, $cmd)); |
| 65 | + $( |
| 66 | + use std::os::unix::process::CommandExt; |
| 67 | + std_cmd.process_group($gpid); |
| 68 | + )? |
| 69 | + tokio::process::Command::from(std_cmd) |
| 70 | + .stderr(stderr) |
| 71 | + .stdout(stdout) |
| 72 | + }}; |
| 73 | +} |
52 | 74 |
|
53 |
| - info!("{description}"); |
54 |
| - let log_path = log_path.to_string_lossy(); |
55 |
| - let _ = log_file |
56 |
| - .write(format!("{}\n", description).as_bytes()) |
57 |
| - .map_err(|e| warn!("Error writing into {log_path}, error: {e}",)); |
58 |
| - let _ = log_file |
59 |
| - .flush() |
60 |
| - .map_err(|e| warn!("Error writing into {log_path}, error: {e}",)); |
61 |
| - cmd.stderr_to_stdout().stdout_file(log_file) |
62 |
| - }; |
| 75 | +#[macro_export] |
| 76 | +macro_rules! spawn_with_logs { |
| 77 | + ($description:expr, $sh:expr, $cmd:literal, $log:expr) => {{ |
| 78 | + crate::cmd_with_logs!($description, $sh, $cmd, $log) |
| 79 | + .kill_on_drop(true) |
| 80 | + .spawn() |
| 81 | + }}; |
| 82 | +} |
63 | 83 |
|
64 |
| - Box::new(with_logs) |
| 84 | +#[macro_export] |
| 85 | +macro_rules! run_with_logs { |
| 86 | + ($description:expr, $sh:expr, $cmd:literal, $log:expr) => {{ |
| 87 | + let exit_status: anyhow::Result<std::process::ExitStatus> = |
| 88 | + crate::spawn_with_logs!($description, $sh, $cmd, $log)? |
| 89 | + .wait() |
| 90 | + .await |
| 91 | + .map_err(|e| e.into()); |
| 92 | + match exit_status?.code() { |
| 93 | + Some(code) if code != 0 => Err(anyhow::anyhow!( |
| 94 | + "{}, exit with status code: {code}", |
| 95 | + $description |
| 96 | + )), |
| 97 | + _ => Ok(()), |
| 98 | + } |
| 99 | + }}; |
65 | 100 | }
|
0 commit comments