Skip to content

Commit fc96ca8

Browse files
committed
Use closure to allow passing custom tracing layers
The previous method, where a layer would be passed directly, required to pass a "no-op" layer when no custom layer was needed. This should have in theory worked, however having a no-op layer seems to change the way the tracing lib applies filters internally, leading to some debug!() being printed despite them being out of the minimum level for the filters. Note however that this behavior was very inconsistent, and e.g. some debug!() would get printed and some others wouldn't, for no apparent reason.
1 parent 0d74252 commit fc96ca8

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,13 +1507,15 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15071507
}
15081508
}
15091509

1510-
pub fn init_logger_with_additional_layer(
1510+
pub fn init_logger_with_additional_layer<F, T>(
15111511
early_dcx: &EarlyDiagCtxt,
15121512
cfg: rustc_log::LoggerConfig,
1513-
additional_tracing_layer: impl rustc_log::Layer<rustc_log::Registry> + Send + Sync,
1514-
) {
1515-
if let Err(error) = rustc_log::init_logger_with_additional_layer(cfg, additional_tracing_layer)
1516-
{
1513+
build_subscriber: F,
1514+
) where
1515+
F: FnOnce() -> T,
1516+
T: rustc_log::BuildSubscriberRet,
1517+
{
1518+
if let Err(error) = rustc_log::init_logger_with_additional_layer(cfg, build_subscriber) {
15171519
early_dcx.early_fatal(error.to_string());
15181520
}
15191521
}

compiler/rustc_log/src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use tracing_core::{Event, Subscriber};
4242
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4343
use tracing_subscriber::fmt::FmtContext;
4444
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
45-
use tracing_subscriber::layer::{Identity, SubscriberExt};
45+
use tracing_subscriber::layer::SubscriberExt;
4646
// Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
4747
pub use tracing_subscriber::{Layer, Registry};
4848

@@ -74,15 +74,30 @@ impl LoggerConfig {
7474

7575
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
7676
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
77-
init_logger_with_additional_layer(cfg, Identity::new())
77+
init_logger_with_additional_layer(cfg, || Registry::default())
78+
}
79+
80+
pub trait BuildSubscriberRet:
81+
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
82+
{
83+
}
84+
85+
impl<
86+
T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync,
87+
> BuildSubscriberRet for T
88+
{
7889
}
7990

8091
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
8192
/// Additionally add a custom layer to collect logging and tracing events.
82-
pub fn init_logger_with_additional_layer(
93+
pub fn init_logger_with_additional_layer<F, T>(
8394
cfg: LoggerConfig,
84-
additional_tracing_layer: impl Layer<Registry> + Send + Sync,
85-
) -> Result<(), Error> {
95+
build_subscriber: F,
96+
) -> Result<(), Error>
97+
where
98+
F: FnOnce() -> T,
99+
T: BuildSubscriberRet,
100+
{
86101
let filter = match cfg.filter {
87102
Ok(env) => EnvFilter::new(env),
88103
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
@@ -115,7 +130,7 @@ pub fn init_logger_with_additional_layer(
115130
};
116131

117132
let mut layer = tracing_tree::HierarchicalLayer::default()
118-
.with_writer(io::stderr as fn() -> io::Stderr)
133+
.with_writer(io::stderr)
119134
.with_ansi(color_logs)
120135
.with_targets(true)
121136
.with_verbose_exit(verbose_entry_exit)
@@ -135,8 +150,7 @@ pub fn init_logger_with_additional_layer(
135150
Err(_) => {} // no wraptree
136151
}
137152

138-
let subscriber =
139-
Registry::default().with(additional_tracing_layer).with(layer.with_filter(filter));
153+
let subscriber = build_subscriber().with(layer.with_filter(filter));
140154
match cfg.backtrace {
141155
Ok(backtrace_target) => {
142156
let fmt_layer = tracing_subscriber::fmt::layer()

0 commit comments

Comments
 (0)