Skip to content

Commit 927ad1a

Browse files
authored
Merge pull request #794 from cgwalters/log-command
utils: Add a `log_debug()` helper
2 parents 254876e + 96fe0a9 commit 927ad1a

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

lib/src/blockdev.rs

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub(crate) fn list_dev(dev: &Utf8Path) -> Result<Device> {
9898
let mut devs: DevicesOutput = Command::new("lsblk")
9999
.args(["-J", "-b", "-O"])
100100
.arg(dev)
101+
.log_debug()
101102
.run_and_parse_json()?;
102103
for dev in devs.blockdevices.iter_mut() {
103104
dev.backfill_missing()?;

lib/src/lsm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::Path;
66
use std::process::Command;
77

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

lib/src/mount.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn run_findmnt(args: &[&str], path: &str) -> Result<Filesystem> {
3838
])
3939
.args(args)
4040
.arg(path)
41+
.log_debug()
4142
.run_and_parse_json()?;
4243
o.filesystems
4344
.into_iter()

utils/src/command.rs

+15
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ use anyhow::{Context, Result};
99

1010
/// Helpers intended for [`std::process::Command`].
1111
pub trait CommandRunExt {
12+
/// Log (at debug level) the full child commandline.
13+
fn log_debug(&mut self) -> &mut Self;
14+
1215
/// Execute the child process.
1316
fn run(&mut self) -> Result<()>;
17+
1418
/// Execute the child process, parsing its stdout as JSON.
1519
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;
1620
}
@@ -71,9 +75,20 @@ impl CommandRunExt for Command {
7175
fn run(&mut self) -> Result<()> {
7276
let stderr = tempfile::tempfile()?;
7377
self.stderr(stderr.try_clone()?);
78+
tracing::trace!("exec: {self:?}");
7479
self.status()?.check_status(stderr)
7580
}
7681

82+
/// Output a debug-level log message with this command.
83+
fn log_debug(&mut self) -> &mut Self {
84+
// We unconditionally log at trace level, so avoid double logging
85+
if !tracing::enabled!(tracing::Level::TRACE) {
86+
tracing::debug!("exec: {self:?}");
87+
}
88+
self
89+
}
90+
91+
/// Synchronously execute the child, and parse its stdout as JSON.
7792
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
7893
let mut stdout = tempfile::tempfile()?;
7994
self.stdout(stdout.try_clone()?);

0 commit comments

Comments
 (0)