From e1d242ebc50e4df0bf8221b2a152b9d48bf54a34 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 18 Sep 2024 11:04:44 -0400 Subject: [PATCH] utils: Add a `log_debug()` helper ref https://github.com/containers/bootc/discussions/793 And add a log at trace level unconditionally. Signed-off-by: Colin Walters --- lib/src/blockdev.rs | 1 + utils/src/command.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/src/blockdev.rs b/lib/src/blockdev.rs index 8631902f..cd9c58b1 100644 --- a/lib/src/blockdev.rs +++ b/lib/src/blockdev.rs @@ -98,6 +98,7 @@ pub(crate) fn list_dev(dev: &Utf8Path) -> Result { let mut devs: DevicesOutput = Command::new("lsblk") .args(["-J", "-b", "-O"]) .arg(dev) + .log_debug() .run_and_parse_json()?; for dev in devs.blockdevices.iter_mut() { dev.backfill_missing()?; diff --git a/utils/src/command.rs b/utils/src/command.rs index 3a360caa..e43041b0 100644 --- a/utils/src/command.rs +++ b/utils/src/command.rs @@ -7,6 +7,7 @@ use anyhow::{Context, Result}; /// Helpers intended for [`std::process::Command`]. pub trait CommandRunExt { + fn log_debug(&mut self) -> &mut Self; fn run(&mut self) -> Result<()>; /// Execute the child process, parsing its stdout as JSON. fn run_and_parse_json(&mut self) -> Result; @@ -68,9 +69,20 @@ impl CommandRunExt for Command { fn run(&mut self) -> Result<()> { let stderr = tempfile::tempfile()?; self.stderr(stderr.try_clone()?); + tracing::trace!("exec: {self:?}"); self.status()?.check_status(stderr) } + /// Output a debug-level log message with this command. + fn log_debug(&mut self) -> &mut Self { + // We unconditionally log at trace level, so avoid double logging + if !tracing::enabled!(tracing::Level::TRACE) { + tracing::debug!("exec: {self:?}"); + } + self + } + + /// Synchronously execute the child, and parse its stdout as JSON. fn run_and_parse_json(&mut self) -> Result { let mut stdout = tempfile::tempfile()?; self.stdout(stdout.try_clone()?);