lib: Add --verbose flag to the CLI for providing a debug output option#1154
lib: Add --verbose flag to the CLI for providing a debug output option#1154mabulgu wants to merge 7 commits intobootc-dev:mainfrom
Conversation
|
I am checking the failure messages |
cgwalters
left a comment
There was a problem hiding this comment.
Thank you so much for working on this!!
utils/src/tracing_util.rs
Outdated
| eprintln!("Failed to update log level: {}", e); | ||
| } | ||
| } else { | ||
| eprintln!("Logging system not initialized yet."); |
There was a problem hiding this comment.
I think this should probably be fatal?
There was a problem hiding this comment.
Good catch again! How about now?
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:)
f693a14 to
a3cfdf7
Compare
Signed-off-by: mabulgu <[email protected]>
…m in the current scope Signed-off-by: mabulgu <[email protected]>
…hanges the log level Signed-off-by: mabulgu <[email protected]>
Signed-off-by: mabulgu <[email protected]>
Signed-off-by: mabulgu <[email protected]>
Signed-off-by: mabulgu <[email protected]>
Also removed the test case for RUST_LOG as we will cover it via a highler level test Signed-off-by: mabulgu <[email protected]>
|
I will get back to this PR soon. |
Description
This PR adds a
--verboseflag 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:
The
verboseflag is matched to different levels of tracing levels fromWARNtoTRACE:So any user can change the tracing level at runtime by adding the flag and changing its count. In the following
bootc switchexample,-vvvis used forTRACINGand sub level logs:Users still can use
RUST_LOGas they used to. Usage ofRUST_LOGoverrides the--verboseflag if used. So even if you run your command with a-vvlevel, if you set your RUST_LOG asinfo, you will only see theINFOandWARNlogs, not theDEBUGones. This case is provided with a unit test here.Test Results
Unit tests (tracing only is shown here):
Container tests:
Closes: #475