Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Add a log_debug() helper #794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:?}");
cgwalters marked this conversation as resolved.
Show resolved Hide resolved
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
omertuc marked this conversation as resolved.
Show resolved Hide resolved
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