Skip to content

Commit 81fa5c0

Browse files
committed
feat: redirect command stdout and stderr to logs
1 parent c9c9617 commit 81fa5c0

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/runner.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::packages::Package;
2-
use log::{debug, error, warn};
3-
use std::io::{stdin, IsTerminal, Read, Write};
1+
use log::{debug, error, info, warn};
2+
use std::io::{stdin, IsTerminal, Read, Write, BufRead, BufReader};
43
use std::path::Path;
54
use std::process::{Command, Stdio};
5+
use crate::packages::Package;
66

77
fn run_command(command: &str, stdin_buffer: Option<Vec<u8>>) -> bool {
88
debug!("Running command: {}", command);
@@ -13,8 +13,8 @@ fn run_command(command: &str, stdin_buffer: Option<Vec<u8>>) -> bool {
1313

1414
let mut child = Command::new(cmd)
1515
.args(args)
16-
.stdout(Stdio::inherit()) // Inherit stdout
17-
.stderr(Stdio::inherit()) // Inherit stderr
16+
.stdout(Stdio::piped()) // Redirect stdout to a pipe
17+
.stderr(Stdio::piped()) // Redirect stderr to a pipe
1818
.stdin(Stdio::piped()) // Set stdin to piped to write the buffer later
1919
.spawn() // Spawn the command
2020
.expect("Failed to spawn command");
@@ -27,8 +27,37 @@ fn run_command(command: &str, stdin_buffer: Option<Vec<u8>>) -> bool {
2727
.expect("Failed to write to stdin");
2828
}
2929

30-
// Wait for the command to complete and check the status
31-
match child.wait() {
30+
// Function to log output from a pipe
31+
fn log_output<R: BufRead>(reader: R, log_fn: impl Fn(&str)) {
32+
for line in reader.lines() {
33+
match line {
34+
Ok(line) => log_fn(&line),
35+
Err(e) => error!("Failed to read line from output: {}", e),
36+
}
37+
}
38+
}
39+
40+
// Read the child's stdout and stderr in separate threads and log them
41+
let stdout = child.stdout.take().expect("Failed to open stdout");
42+
let stderr = child.stderr.take().expect("Failed to open stderr");
43+
44+
let stdout_thread = std::thread::spawn(move || {
45+
log_output(BufReader::new(stdout), |line| info!("{}", line));
46+
});
47+
48+
let stderr_thread = std::thread::spawn(move || {
49+
log_output(BufReader::new(stderr), |line| error!("{}", line));
50+
});
51+
52+
// Wait for the command to complete
53+
let status = child.wait();
54+
55+
// Wait for the logging threads to finish
56+
let _ = stdout_thread.join();
57+
let _ = stderr_thread.join();
58+
59+
// Check the command status
60+
match status {
3261
Ok(status) => status.success(),
3362
Err(e) => {
3463
error!("Command failed to complete: {}", e);

0 commit comments

Comments
 (0)