Skip to content

Commit

Permalink
utils: Add a log_debug() helper
Browse files Browse the repository at this point in the history
ref containers#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 43606a7
Show file tree
Hide file tree
Showing 2 changed files with 10 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
9 changes: 9 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,17 @@ 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 {
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 43606a7

Please sign in to comment.