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 } ;
4
3
use std:: path:: Path ;
5
4
use std:: process:: { Command , Stdio } ;
5
+ use crate :: packages:: Package ;
6
6
7
7
fn run_command ( command : & str , stdin_buffer : Option < Vec < u8 > > ) -> bool {
8
8
debug ! ( "Running command: {}" , command) ;
@@ -13,8 +13,8 @@ fn run_command(command: &str, stdin_buffer: Option<Vec<u8>>) -> bool {
13
13
14
14
let mut child = Command :: new ( cmd)
15
15
. 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
18
18
. stdin ( Stdio :: piped ( ) ) // Set stdin to piped to write the buffer later
19
19
. spawn ( ) // Spawn the command
20
20
. expect ( "Failed to spawn command" ) ;
@@ -27,8 +27,37 @@ fn run_command(command: &str, stdin_buffer: Option<Vec<u8>>) -> bool {
27
27
. expect ( "Failed to write to stdin" ) ;
28
28
}
29
29
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 {
32
61
Ok ( status) => status. success ( ) ,
33
62
Err ( e) => {
34
63
error ! ( "Command failed to complete: {}" , e) ;
0 commit comments