From e2866404ef854641502974b400769518f8c203d2 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Tue, 15 Aug 2023 16:02:55 -0700 Subject: [PATCH] Format the existing command in logging --- crates/flycheck/src/lib.rs | 41 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs index 7c52eb0b5be..2de719af92c 100644 --- a/crates/flycheck/src/lib.rs +++ b/crates/flycheck/src/lib.rs @@ -5,7 +5,9 @@ #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)] use std::{ + ffi::OsString, fmt, io, + path::PathBuf, process::{ChildStderr, ChildStdout, Command, Stdio}, time::Duration, }; @@ -221,21 +223,19 @@ impl FlycheckActor { } let command = self.check_command(); + let formatted_command = format!("{:?}", command); + tracing::debug!(?command, "will restart flycheck"); match CommandHandle::spawn(command) { Ok(command_handle) => { - tracing::debug!( - command = ?self.check_command(), - "did restart flycheck" - ); + tracing::debug!(command = formatted_command, "did restart flycheck"); self.command_handle = Some(command_handle); self.report_progress(Progress::DidStart); } Err(error) => { self.report_progress(Progress::DidFailToRestart(format!( - "Failed to run the following command: {:?} error={}", - self.check_command(), - error + "Failed to run the following command: {} error={}", + formatted_command, error ))); } } @@ -245,11 +245,13 @@ impl FlycheckActor { // Watcher finished let command_handle = self.command_handle.take().unwrap(); + let formatted_handle = format!("{:?}", command_handle); + let res = command_handle.join(); if res.is_err() { tracing::error!( - "Flycheck failed to run the following command: {:?}", - self.check_command() + "Flycheck failed to run the following command: {}", + formatted_handle ); } self.report_progress(Progress::DidFinish(res)); @@ -286,7 +288,7 @@ impl FlycheckActor { fn cancel_check_process(&mut self) { if let Some(command_handle) = self.command_handle.take() { tracing::debug!( - command = ?self.check_command(), + command = ?command_handle, "did cancel flycheck" ); command_handle.cancel(); @@ -397,6 +399,19 @@ struct CommandHandle { child: JodGroupChild, thread: stdx::thread::JoinHandle>, receiver: Receiver, + program: OsString, + arguments: Vec, + current_dir: Option, +} + +impl fmt::Debug for CommandHandle { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CommandHandle") + .field("program", &self.program) + .field("arguments", &self.arguments) + .field("current_dir", &self.current_dir) + .finish() + } } impl CommandHandle { @@ -404,6 +419,10 @@ impl CommandHandle { command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null()); let mut child = command.group_spawn().map(JodGroupChild)?; + let program = command.get_program().into(); + let arguments = command.get_args().map(|arg| arg.into()).collect::>(); + let current_dir = command.get_current_dir().map(|arg| arg.to_path_buf()); + let stdout = child.0.inner().stdout.take().unwrap(); let stderr = child.0.inner().stderr.take().unwrap(); @@ -413,7 +432,7 @@ impl CommandHandle { .name("CargoHandle".to_owned()) .spawn(move || actor.run()) .expect("failed to spawn thread"); - Ok(CommandHandle { child, thread, receiver }) + Ok(CommandHandle { program, arguments, current_dir, child, thread, receiver }) } fn cancel(mut self) {