Skip to content

Commit

Permalink
utils: Add a log_debug() helper
Browse files Browse the repository at this point in the history
ref #793
And add a log at trace level unconditionally.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Sep 18, 2024
1 parent ecabb89 commit e1d242e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub(crate) fn list_dev(dev: &Utf8Path) -> Result<Device> {
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()?;
Expand Down
12 changes: 12 additions & 0 deletions utils/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;
Expand Down Expand Up @@ -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<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
let mut stdout = tempfile::tempfile()?;
self.stdout(stdout.try_clone()?);
Expand Down

0 comments on commit e1d242e

Please sign in to comment.