From 378b8ac11a915683bd0c3e9513981e773a35e324 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Mon, 11 Oct 2021 16:54:12 +0300 Subject: [PATCH] Improve debug implementation of `Command` In normal mode `Debug` now prints just the command-line (similarly to `std::process::Command`). This is useful for logging commands and formatting error messages that includes command-line. In "alternate" mode full internals of this structure are still printed. --- src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b2b0da5..ed10784 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -616,7 +616,6 @@ impl io::AsyncRead for ChildStderr { /// }; /// # std::io::Result::Ok(()) }); /// ``` -#[derive(Debug)] pub struct Command { inner: std::process::Command, stdin: Option, @@ -940,6 +939,26 @@ impl Command { } } +impl fmt::Debug for Command { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + f.debug_struct("Command") + .field("inner", &self.inner) + .field("stdin", &self.stdin) + .field("stdout", &self.stdout) + .field("stderr", &self.stderr) + .field("reap_on_drop", &self.reap_on_drop) + .field("kill_on_drop", &self.kill_on_drop) + .finish() + } else { + // Stdlib outputs command-line in Debug for Command. This does the + // same, if not in "alternate" (long pretty-printed) mode. + // This is useful for logs, for example. + fmt::Debug::fmt(&self.inner, f) + } + } +} + /// Moves `Fd` out of non-blocking mode. #[cfg(unix)] fn blocking_fd(fd: std::os::unix::io::RawFd) -> io::Result<()> {