@@ -20,6 +20,7 @@ use ffi::OsStr;
2020use fmt;
2121use io:: { self , Error , ErrorKind } ;
2222use path;
23+ use str;
2324use sys:: pipe:: { self , AnonPipe } ;
2425use sys:: process as imp;
2526use sys_common:: { AsInner , AsInnerMut , FromInner , IntoInner } ;
@@ -400,6 +401,32 @@ pub struct Output {
400401 pub stderr : Vec < u8 > ,
401402}
402403
404+ // If either stderr or stdout are valid utf8 strings it prints the valid
405+ // strings, otherwise it prints the byte sequence instead
406+ #[ stable( feature = "process_output_debug" , since = "1.7.0" ) ]
407+ impl fmt:: Debug for Output {
408+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
409+
410+ let stdout_utf8 = str:: from_utf8 ( & self . stdout ) ;
411+ let stdout_debug: & fmt:: Debug = match stdout_utf8 {
412+ Ok ( ref str) => str,
413+ Err ( _) => & self . stdout
414+ } ;
415+
416+ let stderr_utf8 = str:: from_utf8 ( & self . stderr ) ;
417+ let stderr_debug: & fmt:: Debug = match stderr_utf8 {
418+ Ok ( ref str) => str,
419+ Err ( _) => & self . stderr
420+ } ;
421+
422+ fmt. debug_struct ( "Output" )
423+ . field ( "status" , & self . status )
424+ . field ( "stdout" , stdout_debug)
425+ . field ( "stderr" , stderr_debug)
426+ . finish ( )
427+ }
428+ }
429+
403430/// Describes what to do with a standard I/O stream for a child process.
404431#[ stable( feature = "process" , since = "1.0.0" ) ]
405432pub struct Stdio ( StdioImp ) ;
0 commit comments