Skip to content

Commit

Permalink
fixup! fixup! refactor(log): reimplement log using tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Jun 2, 2024
1 parent 0b3e73b commit 92b7523
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
10 changes: 2 additions & 8 deletions src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use rustup::cli::rustup_mode;
#[cfg(windows)]
use rustup::cli::self_update;
use rustup::cli::setup_mode;
use rustup::currentprocess::{
filesource::StderrSource, process, varsource::VarSource, with_runtime, OSProcess,
};
use rustup::currentprocess::{process, varsource::VarSource, with_runtime, OSProcess};
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
use rustup::is_proxyable_tools;
use rustup::utils::utils::{self, ExitCode};
Expand Down Expand Up @@ -59,11 +57,7 @@ async fn maybe_trace_rustup() -> Result<utils::ExitCode> {

#[cfg(feature = "otel")]
let telemetry = rustup::cli::log::telemetry()?;
let console_logger = {
let curr_process = process();
let has_ansi = curr_process.stderr().is_a_tty();
rustup::cli::log::console_logger(curr_process, has_ansi)
};
let console_logger = rustup::cli::log::console_logger(process());
let subscriber = {
#[cfg(feature = "otel")]
{
Expand Down
7 changes: 4 additions & 3 deletions src/cli/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tracing_subscriber::{
};

use crate::{
currentprocess::{filesource::StderrSource as _, varsource::VarSource as _, Process},
currentprocess::{filesource::StderrSource, varsource::VarSource as _, Process},
utils::notify::NotificationLevel,
};

Expand Down Expand Up @@ -42,14 +42,15 @@ macro_rules! err {
/// When the `RUST_LOG` environment variable is present, a standard [`tracing_subscriber`]
/// formatter will be used according to the filtering directives set in its value.
/// Otherwise, this logger will use [`EventFormatter`] to mimic "classic" Rustup `stderr` output.
pub fn console_logger<S>(process: Process, with_ansi: bool) -> impl Layer<S>
pub fn console_logger<S>(process: Process) -> impl Layer<S>
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
let maybe_rust_log_directives = process.var_os("RUST_LOG").clone();
let has_ansi = process.stderr().is_a_tty();
let logger = tracing_subscriber::fmt::layer()
.with_writer(move || process.stderr())
.with_ansi(with_ansi);
.with_ansi(has_ansi);
if let Some(directives) = maybe_rust_log_directives {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
Expand Down
6 changes: 3 additions & 3 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(process);
let _guard = console_logger().set_default();
let _guard = tracing_subscriber().set_default();
let result = f();
*p.borrow_mut() = None;
result
Expand All @@ -164,7 +164,7 @@ fn ensure_hook() {
});
}

fn console_logger() -> impl tracing::Subscriber {
fn tracing_subscriber() -> impl tracing::Subscriber {
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
layer::SubscriberExt,
Expand Down Expand Up @@ -251,7 +251,7 @@ pub fn with_runtime<'a, R>(
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(process);
let result = runtime.block_on(fut.with_subscriber(console_logger()));
let result = runtime.block_on(fut.with_subscriber(tracing_subscriber()));
*p.borrow_mut() = None;
result
})
Expand Down

0 comments on commit 92b7523

Please sign in to comment.