From b15e0a073758700045560e8d2804555aba3d2cab Mon Sep 17 00:00:00 2001 From: rami3l Date: Sun, 11 Aug 2024 11:21:31 +0800 Subject: [PATCH] feat(cli/rustup-mode)!: set log level to `INFO`/`DEBUG` on `--quiet`/`--verbose` if `RUSTUP_LOG` is unset --- src/bin/rustup-init.rs | 2 +- src/cli/rustup_mode.rs | 17 ++++++++++++----- src/process.rs | 8 ++++++-- src/test/mock/clitools.rs | 7 ++++++- .../cli-ui/rustup/rustup_help_cmd_stdout.toml | 4 ++-- .../rustup/rustup_help_flag_stdout.toml | 4 ++-- .../rustup/rustup_only_options_stdout.toml | 4 ++-- tests/suite/cli_rustup.rs | 19 +------------------ 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/bin/rustup-init.rs b/src/bin/rustup-init.rs index 591e81297c..010cb321d1 100644 --- a/src/bin/rustup-init.rs +++ b/src/bin/rustup-init.rs @@ -93,7 +93,7 @@ async fn run_rustup_inner( utils::current_exe()?; match process.name().as_deref() { - Some("rustup") => rustup_mode::main(current_dir, process).await, + Some("rustup") => rustup_mode::main(current_dir, process, console_filter).await, Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => { // NB: The above check is only for the prefix of the file // name. Browsers rename duplicates to diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index f2e178e158..bc8446ae3f 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -10,10 +10,11 @@ use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, Val use clap_complete::Shell; use itertools::Itertools; use tracing::{info, trace, warn}; +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; use crate::{ cli::{ - common::{self, PackageUpdate}, + common::{self, update_console_filter, PackageUpdate}, errors::CLIError, help::*, self_update::{self, check_rustup_update, SelfUpdateMode}, @@ -68,11 +69,11 @@ fn handle_epipe(res: Result) -> Result { after_help = RUSTUP_HELP, )] struct Rustup { - /// Enable verbose output - #[arg(short, long)] + /// Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset + #[arg(short, long, conflicts_with = "quiet")] verbose: bool, - /// Disable progress output + /// Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset #[arg(short, long, conflicts_with = "verbose")] quiet: bool, @@ -532,7 +533,11 @@ enum SetSubcmd { } #[tracing::instrument(level = "trace", fields(args = format!("{:?}", process.args_os().collect::>())))] -pub async fn main(current_dir: PathBuf, process: &Process) -> Result { +pub async fn main( + current_dir: PathBuf, + process: &Process, + console_filter: Handle, +) -> Result { self_update::cleanup_self_updater(process)?; use clap::error::ErrorKind::*; @@ -570,6 +575,8 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result, _guard: DefaultGuard, // guard is dropped at the end of the test } @@ -230,10 +233,11 @@ impl TestProcess { impl From for TestProcess { fn from(inner: TestContext) -> Self { let inner = Process::TestProcess(inner); - let guard = crate::cli::log::tracing_subscriber(&inner).0.set_default(); + let (tracing_subscriber, console_filter) = crate::cli::log::tracing_subscriber(&inner); Self { process: inner, - _guard: guard, + console_filter, + _guard: tracing_subscriber.set_default(), } } } diff --git a/src/test/mock/clitools.rs b/src/test/mock/clitools.rs index d81eb0374b..de9981abd5 100644 --- a/src/test/mock/clitools.rs +++ b/src/test/mock/clitools.rs @@ -793,7 +793,12 @@ impl Config { } let tp = process::TestProcess::new(&*self.workdir.borrow(), &arg_strings, vars, ""); - let process_res = rustup_mode::main(tp.process.current_dir().unwrap(), &tp.process).await; + let process_res = rustup_mode::main( + tp.process.current_dir().unwrap(), + &tp.process, + tp.console_filter.clone(), + ) + .await; // convert Err's into an ec let ec = match process_res { Ok(process_res) => process_res, diff --git a/tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml b/tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml index b6771fb5c3..755cf27fd5 100644 --- a/tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml @@ -30,8 +30,8 @@ Arguments: [+toolchain] Release channel (e.g. +stable) or custom toolchain to set override Options: - -v, --verbose Enable verbose output - -q, --quiet Disable progress output + -v, --verbose Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset + -q, --quiet Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -h, --help Print help -V, --version Print version diff --git a/tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml index 4fa43f881b..05a76d4e80 100644 --- a/tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml @@ -30,8 +30,8 @@ Arguments: [+toolchain] Release channel (e.g. +stable) or custom toolchain to set override Options: - -v, --verbose Enable verbose output - -q, --quiet Disable progress output + -v, --verbose Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset + -q, --quiet Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -h, --help Print help -V, --version Print version diff --git a/tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml b/tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml index e0d9a66f54..dce8bfe049 100644 --- a/tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml @@ -32,10 +32,10 @@ Arguments: Options: -v, --verbose - Enable verbose output + Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset -q, --quiet - Disable progress output + Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -h, --help Print help diff --git a/tests/suite/cli_rustup.rs b/tests/suite/cli_rustup.rs index b9b6003aae..41d8c496be 100644 --- a/tests/suite/cli_rustup.rs +++ b/tests/suite/cli_rustup.rs @@ -84,24 +84,7 @@ async fn rustup_stable_quiet() { " ), - for_host!( - r"info: syncing channel updates for 'stable-{0}' -info: latest update on 2015-01-02, rust version 1.1.0 (hash-stable-1.1.0) -info: downloading component 'cargo' -info: downloading component 'rust-docs' -info: downloading component 'rust-std' -info: downloading component 'rustc' -info: removing previous version of component 'cargo' -info: removing previous version of component 'rust-docs' -info: removing previous version of component 'rust-std' -info: removing previous version of component 'rustc' -info: installing component 'cargo' -info: installing component 'rust-docs' -info: installing component 'rust-std' -info: installing component 'rustc' -info: cleaning up downloads & tmp directories -" - ), + "", ) .await; }