Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,19 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
}
}

pub fn init_logger_with_additional_layer<F, T>(
early_dcx: &EarlyDiagCtxt,
cfg: rustc_log::LoggerConfig,
build_subscriber: F,
) where
F: FnOnce() -> T,
T: rustc_log::BuildSubscriberRet,
{
if let Err(error) = rustc_log::init_logger_with_additional_layer(cfg, build_subscriber) {
early_dcx.early_fatal(error.to_string());
}
}

/// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`].
/// Making this handler optional lets tools can install a different handler, if they wish.
pub fn install_ctrlc_handler() {
Expand Down
28 changes: 27 additions & 1 deletion compiler/rustc_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
use tracing_subscriber::fmt::FmtContext;
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
use tracing_subscriber::layer::SubscriberExt;
// Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
pub use tracing_subscriber::{Layer, Registry};

/// The values of all the environment variables that matter for configuring a logger.
/// Errors are explicitly preserved so that we can share error handling.
Expand Down Expand Up @@ -72,6 +74,30 @@ impl LoggerConfig {

/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
init_logger_with_additional_layer(cfg, || Registry::default())
}

pub trait BuildSubscriberRet:
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
{
}

impl<
T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync,
> BuildSubscriberRet for T
{
}

/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
/// Additionally add a custom layer to collect logging and tracing events.
pub fn init_logger_with_additional_layer<F, T>(
cfg: LoggerConfig,
build_subscriber: F,
) -> Result<(), Error>
where
F: FnOnce() -> T,
T: BuildSubscriberRet,
{
let filter = match cfg.filter {
Ok(env) => EnvFilter::new(env),
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
Expand Down Expand Up @@ -124,7 +150,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
Err(_) => {} // no wraptree
}

let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
let subscriber = build_subscriber().with(layer.with_filter(filter));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So making this .with(filter).with(layer) as before does not work?

This is the one part of the PR I am not sure about, since I don't know much about tracing.
Cc @oli-obk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it will apply the filter to every layer, including any custom layer we might add

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So despite the fact that the filter is added after the custom layers, it takes effect before them, preventing events from reaching them...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's not what I expected.^^

match cfg.backtrace {
Ok(backtrace_target) => {
let fmt_layer = tracing_subscriber::fmt::layer()
Expand Down
Loading