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

lib: Add --verbose flag to the CLI for providing a debug output option #1154

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mabulgu
Copy link

@mabulgu mabulgu commented Feb 27, 2025

Description

This PR adds a --verbose flag with the short forms -v, -vv, -vvv.. to increase the logging verbosity as requested in #475.

With this PR, the current command structure looks as follows:

Screenshot 2025-02-28 at 01 05 34

The verbose flag is matched to different levels of tracing levels from WARN to TRACE:

// lib/src/cli.rs:1058

// Set log level based on `-v` occurrences
let log_level = match cli.global_args.verbose {
    0 => tracing::Level::WARN,  // Default (no -v)
    1 => tracing::Level::INFO,  // -v
    2 => tracing::Level::DEBUG, // -vv
    _ => tracing::Level::TRACE, // -vvv or more
};

So any user can change the tracing level at runtime by adding the flag and changing its count. In the following bootc switch example, -vvv is used for TRACING and sub level logs:

Screenshot 2025-02-23 at 02 51 10

Users still can use RUST_LOG as they used to. Usage of RUST_LOG overrides the --verbose flag if used. So even if you run your command with a -vv level, if you set your RUST_LOG as info, you will only see the INFO and WARN logs, not the DEBUG ones. This case is provided with a unit test here.

Test Results

Unit tests (tracing only is shown here):

running 3 tests
test cli::tracing_tests::test_default_tracing ... ok
test cli::tracing_tests::test_update_tracing_respects_rust_log ... ok
test cli::tracing_tests::test_update_tracing ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 64 filtered out; finished in 0.00s

Container tests:

➜  bootc git:(issue/475) podman run --rm -ti localhost/bootc bootc-integration-tests container

running 3 tests
test status         ... ok
test install config ... ok
test bootc upgrade  ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.09s

Closes: #475

@mabulgu mabulgu changed the title lib: Add --verbose flag to the CLI for providing a debug option lib: Add --verbose flag to the CLI for providing a debug output option Feb 27, 2025
@mabulgu
Copy link
Author

mabulgu commented Feb 28, 2025

I am checking the failure messages

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Feb 28, 2025
Copy link
Collaborator

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for working on this!!

eprintln!("Failed to update log level: {}", e);
}
} else {
eprintln!("Logging system not initialized yet.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably be fatal?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch again! How about now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also add a unit test for it.

#![allow(unsafe_code)]

use bootc_utils::{initialize_tracing, update_tracing_log_level};
use nix::unistd::{close, dup, dup2, pipe};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we already have nix in our dependency graph, but I personally prefer rustix and it's much more widely used in this codebase.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a personal preference TBH. Let me check Rustix and implement it instead.


// Read from the pipe
let mut buffer = String::new();
// File::from_raw_fd() is considered unsafe in Rust, as it takes ownership of a raw file descriptor.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And specifically note that rustix's pipe API is already returning OwnedFd https://docs.rs/rustix/latest/rustix/pipe/fn.pipe.html which handles this.

lib/src/cli.rs Outdated
/// Helper function to initialize tracing for tests
fn init_tracing_for_tests() {
INIT.call_once(|| {
std::env::remove_var("RUST_LOG");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 2024 edition this is going to be fully unsafe, see https://doc.rust-lang.org/edition-guide/rust-2024/newly-unsafe-functions.html#stdenvset_var-remove_var

And yeah...even though we're running this under a mutex, cargo is still going to run other tests in threads and so it is just unsafe.


Actually this comment relates to this whole topic - first, thank you for going to the effort of writing tests for the tracing code! (I honestly might have taken a patch without it with just some manual testing)

But I think to test this correctly we're going to need to fork off a helper process.

That said...there is also https://nexte.st/ and see specifically https://nexte.st/docs/design/why-process-per-test/ - we could investigate setting that up here and using it in CI, but skipping these tests when run directly via cargo test perhaps?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got your point. How about this new proposal:

1- Remove the test_update_tracing_respects_rust_log as it is related to environment setting and cleanup. So that we wont hit the future unsafe issues.
2- Keep the other test functions here as keeping extra tests at unit level wont hurt.
3- Implement similar scenarios and more with the ENV ones at the place where you suggested. BUt I need to look at this first. I don't know much about https://nexte.st yet:) Let me figure it out how to run etc. I am familiar with GH actions.

.gitignore Outdated
@@ -5,3 +5,6 @@ bootc.tar.zst

# Added by cargo
/target

# IDEs / Editors
.idea/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: let's add a trailing newline here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Done.

pub(crate) struct GlobalArgs {
/// Increase logging verbosity
#[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count, global = true)]
pub(crate) verbose: u8, // Custom verbosity, counts occurrences of -v
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably OK as is, but...some bits of code here log quite a bit at debug level (especially when fetching containers). I wonder if we should also have a --log-level argument that is just the same as setting the RUST_LOG env var? Because then it's easier to do e.g. --log-level=bootc=debug which avoids debug logging from tokio or containers-image-proxy-rs say.

Copy link
Author

@mabulgu mabulgu Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes a lot sense. We actually have a log level here but I think you want it to be configurable per component. -v is common amongst system-wise CLIs but I need to check how a flag like --log-level is used around. Depending on my research I might propose doing this in another PR, for the sake of separation of concerns:)

@mabulgu mabulgu force-pushed the issue/475 branch 2 times, most recently from f693a14 to a3cfdf7 Compare March 7, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add debug output option
2 participants