From a2bb24eef9c218856f9403961e8f9e780ec48916 Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Fri, 13 Oct 2023 16:01:34 -0400 Subject: [PATCH] More logging of command input/output This will not log the exact command we execute, and will always log stdout & stderr. --- src/instruments.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/instruments.rs b/src/instruments.rs index 16afa6c..2427309 100644 --- a/src/instruments.rs +++ b/src/instruments.rs @@ -462,19 +462,41 @@ pub(crate) fn profile_target( command.args(app_config.target_args.as_slice()); } - let output = command.output()?; + log_command_string(&command); + let output = command.output()?; + let stdout = if !output.stdout.is_empty() { + String::from_utf8_lossy(&output.stdout) + } else { + "{empty}".into() + }; + let stderr = if !output.stderr.is_empty() { + String::from_utf8_lossy(&output.stderr) + } else { + "{empty}".into() + }; + if output.status.success() { + log::debug!("{xctrace_tool} exited successfuly"); + log::debug!("captured stdout:\n{stdout}"); + log::debug!("captured stderr:\n{stderr}"); + } if !output.status.success() { - let stderr = - String::from_utf8(output.stderr).unwrap_or_else(|_| "failed to capture stderr".into()); - let stdout = - String::from_utf8(output.stdout).unwrap_or_else(|_| "failed to capture stdout".into()); - return Err(anyhow!("instruments errored: {} {}", stderr, stdout)); + return Err(anyhow!( + "{xctrace_tool} exited with error.\nstdout: {}\nstderr: {stderr}", + stdout.trim_end(), + )); } Ok(trace_filepath) } +fn log_command_string(command: &Command) { + let mut elements = vec![command.get_program().to_string_lossy()]; + elements.extend(command.get_args().map(|arg| arg.to_string_lossy())); + let as_string = elements.join(" "); + log::debug!("executing command {as_string}") +} + /// get the tty of th current terminal session fn get_tty() -> Result> { let mut command = Command::new("ps");