Skip to content

Commit c949f2a

Browse files
committed
Allow initializing logger with additional tracing Layer
1 parent f6b5da7 commit c949f2a

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,17 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15091509
}
15101510
}
15111511

1512+
pub fn init_logger_with_additional_layer(
1513+
early_dcx: &EarlyDiagCtxt,
1514+
cfg: rustc_log::LoggerConfig,
1515+
additional_tracing_layer: impl rustc_log::Layer<rustc_log::Registry> + Send + Sync,
1516+
) {
1517+
if let Err(error) = rustc_log::init_logger_with_additional_layer(cfg, additional_tracing_layer)
1518+
{
1519+
early_dcx.early_fatal(error.to_string());
1520+
}
1521+
}
1522+
15121523
/// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`].
15131524
/// Making this handler optional lets tools can install a different handler, if they wish.
15141525
pub fn install_ctrlc_handler() {

compiler/rustc_log/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ 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::SubscriberExt;
45+
use tracing_subscriber::layer::{Identity, SubscriberExt};
46+
// Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
47+
pub use tracing_subscriber::{Layer, Registry};
4648

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

7375
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
7476
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
77+
init_logger_with_additional_layer(cfg, Identity::new())
78+
}
79+
80+
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
81+
/// Additionally add a custom layer to collect logging and tracing events.
82+
pub fn init_logger_with_additional_layer(
83+
cfg: LoggerConfig,
84+
additional_tracing_layer: impl Layer<Registry> + Send + Sync,
85+
) -> Result<(), Error> {
7586
let filter = match cfg.filter {
7687
Ok(env) => EnvFilter::new(env),
7788
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
@@ -104,7 +115,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
104115
};
105116

106117
let mut layer = tracing_tree::HierarchicalLayer::default()
107-
.with_writer(io::stderr)
118+
.with_writer(io::stderr as fn() -> io::Stderr)
108119
.with_ansi(color_logs)
109120
.with_targets(true)
110121
.with_verbose_exit(verbose_entry_exit)
@@ -124,7 +135,8 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
124135
Err(_) => {} // no wraptree
125136
}
126137

127-
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
138+
let subscriber =
139+
Registry::default().with(additional_tracing_layer).with(layer.with_filter(filter));
128140
match cfg.backtrace {
129141
Ok(backtrace_target) => {
130142
let fmt_layer = tracing_subscriber::fmt::layer()

0 commit comments

Comments
 (0)