diff --git a/rustup-init.sh b/rustup-init.sh index b7caccd98e..51d25d8b79 100755 --- a/rustup-init.sh +++ b/rustup-init.sh @@ -41,9 +41,9 @@ Usage: rustup-init[EXE] [OPTIONS] Options: -v, --verbose - Enable verbose output + Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset -q, --quiet - Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset + Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host 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/common.rs b/src/cli/common.rs index 5d3a99a812..291ac86b78 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -13,6 +13,7 @@ use std::{cmp, env}; use anyhow::{anyhow, Context, Result}; use git_testament::{git_testament, render_testament}; use tracing::{debug, error, info, trace, warn}; +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; use super::self_update; use crate::{ @@ -127,15 +128,13 @@ pub(crate) fn read_line(process: &Process) -> Result { pub(super) struct Notifier { tracker: Mutex, ram_notice_shown: RefCell, - verbose: bool, } impl Notifier { - pub(super) fn new(verbose: bool, quiet: bool, process: &Process) -> Self { + pub(super) fn new(quiet: bool, process: &Process) -> Self { Self { tracker: Mutex::new(DownloadTracker::new_with_display_progress(!quiet, process)), ram_notice_shown: RefCell::new(false), - verbose, } } @@ -158,9 +157,7 @@ impl Notifier { for n in format!("{n}").lines() { match level { NotificationLevel::Debug => { - if self.verbose { - debug!("{}", n); - } + debug!("{}", n); } NotificationLevel::Info => { info!("{}", n); @@ -180,13 +177,8 @@ impl Notifier { } #[tracing::instrument(level = "trace")] -pub(crate) fn set_globals( - current_dir: PathBuf, - verbose: bool, - quiet: bool, - process: &Process, -) -> Result> { - let notifier = Notifier::new(verbose, quiet, process); +pub(crate) fn set_globals(current_dir: PathBuf, quiet: bool, process: &Process) -> Result> { + let notifier = Notifier::new(quiet, process); Cfg::from_env(current_dir, Arc::new(move |n| notifier.handle(n)), process) } @@ -661,3 +653,31 @@ pub(crate) fn warn_if_host_is_emulated(process: &Process) { warn!("For best compatibility and performance you should reinstall rustup for your native CPU."); } } + +/// Updates the console logger level according to whether `quiet` or `verbose` is set to `true`. +/// +/// Does nothing if at least one of the following conditions is met: +/// - The `RUSTUP_LOG` environment variable is set. +/// - Both `quiet` and `verbose` are set to `true`. +pub(super) fn update_console_filter( + process: &Process, + filter: &Handle, + quiet: bool, + verbose: bool, +) { + if process.var("RUSTUP_LOG").is_ok() { + return; + } + + let maybe_directives = match (quiet, verbose) { + (true, _) => Some("rustup=WARN"), + (_, true) => Some("rustup=DEBUG"), + (_, _) => None, + }; + + if let Some(directives) = maybe_directives { + filter + .modify(|it| *it = EnvFilter::new(directives)) + .expect("error reloading `EnvFilter` for console_logger"); + } +} diff --git a/src/cli/log.rs b/src/cli/log.rs index a0d169ce76..5db2bb0b53 100644 --- a/src/cli/log.rs +++ b/src/cli/log.rs @@ -69,7 +69,7 @@ where (logger.compact().with_filter(env_filter).boxed(), handle) } else { // Receive log lines from Rustup only. - let (env_filter, handle) = reload::Layer::new(EnvFilter::new("rustup=DEBUG")); + let (env_filter, handle) = reload::Layer::new(EnvFilter::new("rustup=INFO")); ( logger .event_format(EventFormatter) diff --git a/src/cli/proxy_mode.rs b/src/cli/proxy_mode.rs index de55f9a99c..650767fb44 100644 --- a/src/cli/proxy_mode.rs +++ b/src/cli/proxy_mode.rs @@ -31,7 +31,7 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result .skip(1 + toolchain.is_some() as usize) .collect(); - let cfg = set_globals(current_dir, false, true, process)?; + let cfg = set_globals(current_dir, true, process)?; let cmd = cfg.resolve_local_toolchain(toolchain)?.command(arg0)?; run_command_for_dir(cmd, arg0, &cmd_args) } diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index ed77adcc48..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::*; @@ -545,7 +550,7 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result { write!(process.stdout().lock(), "{err}")?; info!("This is the version for the rustup toolchain manager, not the rustc compiler."); - let mut cfg = common::set_globals(current_dir, false, true, process)?; + let mut cfg = common::set_globals(current_dir, true, process)?; match cfg.active_rustc_version() { Ok(Some(version)) => info!("The currently active `rustc` version is `{version}`"), Ok(None) => info!("No `rustc` is currently active"), @@ -570,7 +575,9 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result Result> { pub(crate) async fn install( current_dir: PathBuf, no_prompt: bool, - verbose: bool, quiet: bool, mut opts: InstallOpts<'_>, process: &Process, @@ -549,7 +548,7 @@ pub(crate) async fn install( } let no_modify_path = opts.no_modify_path; - if let Err(e) = maybe_install_rust(current_dir, verbose, quiet, opts, process).await { + if let Err(e) = maybe_install_rust(current_dir, quiet, opts, process).await { report_error(&e, process); // On windows, where installation happens in a console @@ -804,7 +803,6 @@ pub(crate) fn install_proxies(process: &Process) -> Result<()> { async fn maybe_install_rust( current_dir: PathBuf, - verbose: bool, quiet: bool, opts: InstallOpts<'_>, process: &Process, @@ -828,7 +826,7 @@ async fn maybe_install_rust( fs::create_dir_all(home).context("unable to create ~/.rustup")?; } - let mut cfg = common::set_globals(current_dir, verbose, quiet, process)?; + let mut cfg = common::set_globals(current_dir, quiet, process)?; let (components, targets) = (opts.components, opts.targets); let toolchain = opts.install(&mut cfg)?; @@ -1230,8 +1228,7 @@ mod tests { home.apply(&mut vars); let tp = TestProcess::with_vars(vars); let mut cfg = - common::set_globals(tp.process.current_dir().unwrap(), false, false, &tp.process) - .unwrap(); + common::set_globals(tp.process.current_dir().unwrap(), false, &tp.process).unwrap(); let opts = InstallOpts { default_host_triple: None, diff --git a/src/cli/setup_mode.rs b/src/cli/setup_mode.rs index ed2fe95c9d..ccee7c134a 100644 --- a/src/cli/setup_mode.rs +++ b/src/cli/setup_mode.rs @@ -7,7 +7,7 @@ use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; use crate::{ cli::{ - common, + common::{self, update_console_filter}, self_update::{self, InstallOpts}, }, dist::Profile, @@ -25,12 +25,12 @@ use crate::{ before_help = format!("rustup-init {}", common::version()), )] struct RustupInit { - /// 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, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset - #[arg(short, long)] + /// Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset + #[arg(short, long, conflicts_with = "verbose")] quiet: bool, /// Disable confirmation prompt @@ -115,11 +115,7 @@ pub async fn main( warn!("{}", common::WARN_COMPLETE_PROFILE); } - if quiet && process.var("RUSTUP_LOG").is_err() { - console_filter - .modify(|it| *it = EnvFilter::new("rustup=WARN")) - .expect("error reloading `EnvFilter` for console_logger"); - } + update_console_filter(process, &console_filter, quiet, verbose); let opts = InstallOpts { default_host_triple: default_host, @@ -131,5 +127,5 @@ pub async fn main( targets: &target.iter().map(|s| &**s).collect::>(), }; - self_update::install(current_dir, no_prompt, verbose, quiet, opts, process).await + self_update::install(current_dir, no_prompt, quiet, opts, process).await } diff --git a/src/process.rs b/src/process.rs index 1491e6bbc0..32b6d3efb6 100644 --- a/src/process.rs +++ b/src/process.rs @@ -17,6 +17,8 @@ use anyhow::{Context, Result}; use tracing::subscriber::DefaultGuard; #[cfg(feature = "test")] use tracing_subscriber::util::SubscriberInitExt; +#[cfg(feature = "test")] +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; pub mod filesource; pub mod terminalsource; @@ -177,6 +179,7 @@ impl Default for OsProcess { #[cfg(feature = "test")] pub struct TestProcess { pub process: Process, + pub console_filter: Handle, _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-init/rustup-init_help_flag_stdout.toml b/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml index c51af98b68..c300ba12d7 100644 --- a/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup-init/rustup-init_help_flag_stdout.toml @@ -10,9 +10,9 @@ Usage: rustup-init[EXE] [OPTIONS] Options: -v, --verbose - Enable verbose output + Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset -q, --quiet - Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset + Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host diff --git a/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml b/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml index 1866e86f4e..51087f3ba3 100644 --- a/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup-init/rustup-init_sh_help_flag_stdout.toml @@ -10,9 +10,9 @@ Usage: rustup-init[EXE] [OPTIONS] Options: -v, --verbose - Enable verbose output + Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset -q, --quiet - Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset + Disable progress output, set log level to 'WARN' if 'RUSTUP_LOG' is unset -y Disable confirmation prompt --default-host 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; }