Skip to content

Commit

Permalink
utils: Add a log_debug() helper, use it in a few places
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 19, 2024
1 parent eea3996 commit 96fe0a9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
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
3 changes: 2 additions & 1 deletion lib/src/lsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::Path;
use std::process::Command;

use anyhow::{Context, Result};
use bootc_utils::CommandRunExt;
use camino::{Utf8Path, Utf8PathBuf};
use cap_std::fs::Dir;
#[cfg(feature = "install")]
Expand Down Expand Up @@ -95,7 +96,7 @@ pub(crate) fn selinux_ensure_install() -> Result<bool> {
let mut cmd = Command::new(&tmpf);
cmd.env(guardenv, tmpf);
cmd.args(std::env::args_os().skip(1));
tracing::debug!("Re-executing {cmd:?}");
cmd.log_debug();
Err(anyhow::Error::msg(cmd.exec()).context("execve"))
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn run_findmnt(args: &[&str], path: &str) -> Result<Filesystem> {
])
.args(args)
.arg(path)
.log_debug()
.run_and_parse_json()?;
o.filesystems
.into_iter()
Expand Down
15 changes: 15 additions & 0 deletions utils/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ use anyhow::{Context, Result};

/// Helpers intended for [`std::process::Command`].
pub trait CommandRunExt {
/// Log (at debug level) the full child commandline.
fn log_debug(&mut self) -> &mut Self;

/// Execute the child process.
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 @@ -71,9 +75,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 96fe0a9

Please sign in to comment.