-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xtask: redirect stdout and stderr into log files
- Loading branch information
1 parent
34b1c87
commit a577f77
Showing
8 changed files
with
172 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
/demo/sovereign/target | ||
/demo/sovereign/demo-rollup/demo_data | ||
/target | ||
/test_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<dyn Fn(&str, duct::Expression) -> 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}, erorr: {e}")); | ||
let _ = log_file | ||
.flush() | ||
.map_err(|e| warn!("Error writing into {log_path}, erorr: {e}")); | ||
cmd.stderr_to_stdout().stdout_file(log_file) | ||
}; | ||
|
||
Box::new(with_logs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters