Skip to content

Commit

Permalink
fix(bin): consider env when no verbosity flag is set (#1848)
Browse files Browse the repository at this point in the history
* fix(bin): consider env when no verbosity flag is set

There are two ways to specify the maximum log level of `neqo-server` and
`neqo-client`. Via the `RUST_LOG` environment variable and since
#1692 via the verbosity flags (e.g. `-v`).

Previously, if no verbosity flag was provided, `INFO` was set as the maximum log
level, the `RUST_LOG` environment variable was simply ignored.

With this commit, if no flag is set, the `RUST_LOG` environment variable is
considered, and only then the `env_logger` crate default value (`ERROR`) is used.

In other words, the precedence order now is:

1. command line flags (e.g. `-v`)
2. `RUST_LOG` environment variable
3. default `ERROR` level

* clippy

* Move `verbose` into `SharedArgs`
  • Loading branch information
mxinden authored Apr 25, 2024
1 parent 0a84268 commit 17c5895
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
11 changes: 6 additions & 5 deletions neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ type Res<T> = Result<T, Error>;
#[command(author, version, about, long_about = None)]
#[allow(clippy::struct_excessive_bools)] // Not a good use of that lint.
pub struct Args {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>,

#[command(flatten)]
shared: SharedArgs,

Expand Down Expand Up @@ -180,7 +177,6 @@ impl Args {
pub fn new(requests: &[u64]) -> Self {
use std::str::FromStr;
Self {
verbose: clap_verbosity_flag::Verbosity::<clap_verbosity_flag::InfoLevel>::default(),
shared: crate::SharedArgs::default(),
urls: requests
.iter()
Expand Down Expand Up @@ -479,7 +475,12 @@ fn qlog_new(args: &Args, hostname: &str, cid: &ConnectionId) -> Res<NeqoQlog> {
}

pub async fn client(mut args: Args) -> Res<()> {
neqo_common::log::init(Some(args.verbose.log_level_filter()));
neqo_common::log::init(
args.shared
.verbose
.as_ref()
.map(clap_verbosity_flag::Verbosity::log_level_filter),
);
init()?;

args.update_for_tests();
Expand Down
4 changes: 4 additions & 0 deletions neqo-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ mod udp;

#[derive(Debug, Parser)]
pub struct SharedArgs {
#[command(flatten)]
verbose: Option<clap_verbosity_flag::Verbosity>,

#[arg(short = 'a', long, default_value = "h3")]
/// ALPN labels to negotiate.
///
Expand Down Expand Up @@ -66,6 +69,7 @@ pub struct SharedArgs {
impl Default for SharedArgs {
fn default() -> Self {
Self {
verbose: None,
alpn: "h3".into(),
qlog_dir: None,
max_table_size_encoder: 16384,
Expand Down
11 changes: 6 additions & 5 deletions neqo-bin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ type Res<T> = Result<T, Error>;
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity<clap_verbosity_flag::InfoLevel>,

#[command(flatten)]
shared: SharedArgs,

Expand Down Expand Up @@ -132,7 +129,6 @@ impl Default for Args {
fn default() -> Self {
use std::str::FromStr;
Self {
verbose: clap_verbosity_flag::Verbosity::<clap_verbosity_flag::InfoLevel>::default(),
shared: crate::SharedArgs::default(),
hosts: vec!["[::]:12345".to_string()],
db: PathBuf::from_str("../test-fixture/db").unwrap(),
Expand Down Expand Up @@ -587,7 +583,12 @@ enum Ready {
pub async fn server(mut args: Args) -> Res<()> {
const HQ_INTEROP: &str = "hq-interop";

neqo_common::log::init(Some(args.verbose.log_level_filter()));
neqo_common::log::init(
args.shared
.verbose
.as_ref()
.map(clap_verbosity_flag::Verbosity::log_level_filter),
);
assert!(!args.key.is_empty(), "Need at least one key");

init_db(args.db.clone())?;
Expand Down

0 comments on commit 17c5895

Please sign in to comment.