Auto merge of #97176 - kraktus:cmd_debug, r=the8472

More verbose `Debug` implementation of `std::process:Command`

Mainly based on commit: ccc019aabf from https://github.com/zackmdavis

close https://github.com/rust-lang/rust/issues/42200
This commit is contained in:
bors 2022-12-27 18:13:23 +00:00
commit 92c1937a90
5 changed files with 173 additions and 29 deletions

View file

@ -144,6 +144,7 @@ pub enum ChildStdio {
Null,
}
#[derive(Debug)]
pub enum Stdio {
Inherit,
Null,
@ -510,16 +511,68 @@ impl ChildStdio {
}
impl fmt::Debug for Command {
// show all attributes but `self.closures` which does not implement `Debug`
// and `self.argv` which is not useful for debugging
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.program != self.args[0] {
write!(f, "[{:?}] ", self.program)?;
}
write!(f, "{:?}", self.args[0])?;
if f.alternate() {
let mut debug_command = f.debug_struct("Command");
debug_command.field("program", &self.program).field("args", &self.args);
if !self.env.is_unchanged() {
debug_command.field("env", &self.env);
}
for arg in &self.args[1..] {
write!(f, " {:?}", arg)?;
if self.cwd.is_some() {
debug_command.field("cwd", &self.cwd);
}
if self.uid.is_some() {
debug_command.field("uid", &self.uid);
}
if self.gid.is_some() {
debug_command.field("gid", &self.gid);
}
if self.groups.is_some() {
debug_command.field("groups", &self.groups);
}
if self.stdin.is_some() {
debug_command.field("stdin", &self.stdin);
}
if self.stdout.is_some() {
debug_command.field("stdout", &self.stdout);
}
if self.stderr.is_some() {
debug_command.field("stderr", &self.stderr);
}
if self.pgroup.is_some() {
debug_command.field("pgroup", &self.pgroup);
}
#[cfg(target_os = "linux")]
{
debug_command.field("create_pidfd", &self.create_pidfd);
}
debug_command.finish()
} else {
if let Some(ref cwd) = self.cwd {
write!(f, "cd {cwd:?} && ")?;
}
for (key, value_opt) in self.get_envs() {
if let Some(value) = value_opt {
write!(f, "{}={value:?} ", key.to_string_lossy())?;
}
}
if self.program != self.args[0] {
write!(f, "[{:?}] ", self.program)?;
}
write!(f, "{:?}", self.args[0])?;
for arg in &self.args[1..] {
write!(f, " {:?}", arg)?;
}
Ok(())
}
Ok(())
}
}