Skip to content

Commit

Permalink
fixup! 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 92b7523 commit 2142f7e
Showing 1 changed file with 1 addition and 37 deletions.
38 changes: 1 addition & 37 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ use cwdsource::*;
use filesource::*;
use varsource::*;

use crate::currentprocess;

/// An abstraction for the current process.
///
/// This acts as a clonable proxy to the global state provided by some key OS
Expand Down Expand Up @@ -138,16 +136,13 @@ pub fn with<F, R>(process: Process, f: F) -> R
where
F: FnOnce() -> R,
{
use tracing_subscriber::util::SubscriberInitExt;

ensure_hook();

PROCESS.with(|p| {
if let Some(old_p) = &*p.borrow() {
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(process);
let _guard = tracing_subscriber().set_default();
let result = f();
*p.borrow_mut() = None;
result
Expand All @@ -164,35 +159,6 @@ fn ensure_hook() {
});
}

fn tracing_subscriber() -> impl tracing::Subscriber {
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
layer::SubscriberExt,
Layer, Registry,
};

let curr_process = currentprocess::process();
let maybe_directives = curr_process.var_os("RUST_LOG").clone();
let logger = tracing_subscriber::fmt::layer()
.with_writer(move || curr_process.stderr())
.with_ansi(false);
let console_logger = if let Some(directives) = maybe_directives {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.parse_lossy(directives.to_string_lossy());
logger.compact().with_filter(env_filter).boxed()
} else {
// Receive log lines from Rustup only.
let env_filter = EnvFilter::new("rustup=DEBUG");
logger
.event_format(crate::cli::log::EventFormatter)
.with_filter(env_filter)
.boxed()
};
// TODO: What about the original otel logger?
Registry::default().with(console_logger)
}

/// Run a function in the context of a process definition and a tokio runtime.
///
/// The process state is injected into a thread-local in every work thread of
Expand All @@ -203,8 +169,6 @@ pub fn with_runtime<'a, R>(
mut runtime_builder: tokio::runtime::Builder,
fut: impl Future<Output = R> + 'a,
) -> R {
use tracing::instrument::WithSubscriber;

ensure_hook();

let start_process = process.clone();
Expand Down Expand Up @@ -251,7 +215,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(tracing_subscriber()));
let result = runtime.block_on(fut);
*p.borrow_mut() = None;
result
})
Expand Down

0 comments on commit 2142f7e

Please sign in to comment.