@@ -6,6 +6,7 @@ use std::io::prelude::*;
66use std:: path:: PathBuf ;
77use std:: process:: { Command , Output } ;
88use std:: {
9+ fmt:: Write as _,
910 fs:: { self , File , OpenOptions } ,
1011 path:: Path ,
1112} ;
@@ -67,7 +68,7 @@ impl std::fmt::Debug for ProcessFailed {
6768
6869trait CommandHelper {
6970 fn capture_outputs (
70- & self ,
71+ & mut self ,
7172 cant_fail : bool ,
7273 name : & str ,
7374 stdout : Option < & PathBuf > ,
@@ -76,26 +77,48 @@ trait CommandHelper {
7677 ) -> Result < ( ) , TestError > ;
7778}
7879
79- impl CommandHelper for Output {
80+ impl CommandHelper for Command {
81+ #[ tracing:: instrument( skip_all, fields( stdout = tracing:: field:: Empty , stderr = tracing:: field:: Empty ) ) ]
8082 fn capture_outputs (
81- & self ,
83+ & mut self ,
8284 cant_fail : bool ,
8385 name : & str ,
8486 stdout : Option < & PathBuf > ,
8587 stderr : Option < & PathBuf > ,
8688 previous_processes_stderr : & [ PathBuf ] ,
8789 ) -> Result < ( ) , TestError > {
90+ let output = self . get_output ( true ) ?;
91+ let out_payload = String :: from_utf8_lossy ( & output. stdout ) ;
8892 if let Some ( out) = stdout {
89- let out_payload = String :: from_utf8_lossy ( & self . stdout ) ;
9093 file_helper ( & out_payload, out) ?;
9194 } ;
9295
96+ let err_payload = String :: from_utf8_lossy ( & output. stderr ) ;
9397 if let Some ( err) = stderr {
94- let err_payload = String :: from_utf8_lossy ( & self . stderr ) ;
9598 file_helper ( & err_payload, err) ?;
9699 } ;
97-
98- if cant_fail && !self . status . success ( ) {
100+ if cant_fail && !output. status . success ( ) {
101+ let span = tracing:: Span :: current ( ) ;
102+ let mut message = format ! ( "Process failed: {}" , self . display( ) ) ;
103+ if !out_payload. trim ( ) . is_empty ( ) {
104+ span. record (
105+ "stdout" ,
106+ tracing:: field:: display (
107+ stdout. map ( |p| p. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ,
108+ ) ,
109+ ) ;
110+ write ! ( message, "\n stdout: \n {}" , out_payload) . unwrap ( ) ;
111+ }
112+ if !err_payload. trim ( ) . is_empty ( ) {
113+ span. record (
114+ "stderr" ,
115+ tracing:: field:: display (
116+ stderr. map ( |p| p. display ( ) . to_string ( ) ) . unwrap_or_default ( ) ,
117+ ) ,
118+ ) ;
119+ write ! ( message, "\n stderr: \n {}" , err_payload) . unwrap ( ) ;
120+ }
121+ tracing:: error!( message=%message) ;
99122 return Err ( ProcessFailed {
100123 command : name. into ( ) ,
101124 stdout : stdout. cloned ( ) ,
@@ -125,18 +148,17 @@ impl TestCase {
125148 . with_context ( || anyhow ! ( "when setting up case for {}" , self . name( ) ) ) ?;
126149 // Run `cargo check`, capturing stderr to a log file
127150 let cargo_check_err_file = path_helper_base ( & chip_dir, & [ "cargo-check.err.log" ] ) ;
128- let output = Command :: new ( "cargo" )
151+ Command :: new ( "cargo" )
129152 . arg ( "check" )
130153 . current_dir ( & chip_dir)
131- . output ( )
154+ . capture_outputs (
155+ true ,
156+ "cargo check" ,
157+ None ,
158+ Some ( & cargo_check_err_file) ,
159+ & process_stderr_paths,
160+ )
132161 . with_context ( || "failed to check" ) ?;
133- output. capture_outputs (
134- true ,
135- "cargo check" ,
136- None ,
137- Some ( & cargo_check_err_file) ,
138- & process_stderr_paths,
139- ) ?;
140162 process_stderr_paths. push ( cargo_check_err_file) ;
141163 Ok ( if opts. verbose > 1 {
142164 Some ( process_stderr_paths)
@@ -180,9 +202,8 @@ impl TestCase {
180202 . arg ( "--vcs" )
181203 . arg ( "none" )
182204 . arg ( & chip_dir)
183- . output ( )
184- . with_context ( || "Failed to cargo init" ) ?
185- . capture_outputs ( true , "cargo init" , None , None , & [ ] ) ?;
205+ . capture_outputs ( true , "cargo init" , None , None , & [ ] )
206+ . with_context ( || "Failed to cargo init" ) ?;
186207 let svd_toml = path_helper_base ( & chip_dir, & [ "Cargo.toml" ] ) ;
187208 let mut file = OpenOptions :: new ( )
188209 . write ( true )
@@ -242,23 +263,22 @@ impl TestCase {
242263 if let Some ( command) = command {
243264 svd2rust_bin. arg ( command) ;
244265 }
245- let output = svd2rust_bin
266+ svd2rust_bin
246267 . args ( [ "-i" , & chip_svd] )
247268 . args ( [ "--target" , target] )
248269 . current_dir ( & chip_dir)
249- . get_output ( ) ?;
250- output. capture_outputs (
251- true ,
252- "svd2rust" ,
253- Some ( & lib_rs_file) . filter ( |_| {
254- !matches ! (
255- self . arch,
256- Target :: CortexM | Target :: Msp430 | Target :: XtensaLX
257- )
258- } ) ,
259- Some ( & svd2rust_err_file) ,
260- & [ ] ,
261- ) ?;
270+ . capture_outputs (
271+ true ,
272+ "svd2rust" ,
273+ Some ( & lib_rs_file) . filter ( |_| {
274+ !matches ! (
275+ self . arch,
276+ Target :: CortexM | Target :: Msp430 | Target :: XtensaLX
277+ )
278+ } ) ,
279+ Some ( & svd2rust_err_file) ,
280+ & [ ] ,
281+ ) ?;
262282 process_stderr_paths. push ( svd2rust_err_file) ;
263283 match self . arch {
264284 Target :: CortexM | Target :: Mips | Target :: Msp430 | Target :: XtensaLX => {
@@ -287,20 +307,19 @@ impl TestCase {
287307 let new_lib_rs_file = path_helper_base ( & chip_dir, & [ "lib.rs" ] ) ;
288308 std:: fs:: rename ( lib_rs_file, & new_lib_rs_file)
289309 . with_context ( || "While moving lib.rs file" ) ?;
290- let output = Command :: new ( form_bin_path)
310+ Command :: new ( form_bin_path)
291311 . arg ( "--input" )
292312 . arg ( & new_lib_rs_file)
293313 . arg ( "--outdir" )
294314 . arg ( & src_dir)
295- . output ( )
315+ . capture_outputs (
316+ true ,
317+ "form" ,
318+ None ,
319+ Some ( & form_err_file) ,
320+ & process_stderr_paths,
321+ )
296322 . with_context ( || "failed to form" ) ?;
297- output. capture_outputs (
298- true ,
299- "form" ,
300- None ,
301- Some ( & form_err_file) ,
302- & process_stderr_paths,
303- ) ?;
304323 std:: fs:: remove_file ( & new_lib_rs_file)
305324 . with_context ( || "While removing lib.rs file after form" ) ?;
306325 }
@@ -322,15 +341,14 @@ impl TestCase {
322341 let output = Command :: new ( rustfmt_bin_path)
323342 . arg ( entry)
324343 . args ( [ "--edition" , "2021" ] )
325- . output ( )
344+ . capture_outputs (
345+ false ,
346+ "rustfmt" ,
347+ None ,
348+ Some ( & rustfmt_err_file) ,
349+ & process_stderr_paths,
350+ )
326351 . with_context ( || "failed to format" ) ?;
327- output. capture_outputs (
328- false ,
329- "rustfmt" ,
330- None ,
331- Some ( & rustfmt_err_file) ,
332- & process_stderr_paths,
333- ) ?;
334352 }
335353
336354 process_stderr_paths. push ( rustfmt_err_file) ;
0 commit comments