Skip to content

Commit 94dfe49

Browse files
Rollup merge of #140969 - Stypox:logger-layer, r=RalfJung,oli-obk
Allow initializing logger with additional tracing Layer This PR adds functions to the `rustc_log` and `rustc_driver_impl` crates to allow initializing the logger with an additional `tracing_subscriber::Layer`. This will be used in Miri to save trace events to file using e.g. [`tracing-chrome`](https://github.com/thoren-d/tracing-chrome)'s or [`tracing-tracy`](https://github.com/nagisa/rust_tracy_client)'s `Layer`s. Additional context on the choice of signature can be found in [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Adding.20a.20dependency.20to.20Miri.20that.20depends.20on.20a.20rustc.20dep/near/515707776). I re-exported `tracing_subscriber::{Layer, Registry};` from `rustc_log` so that `rustc_driver_impl` can use them in the function signature without depending on `tracing_subscriber` directly. I did this to avoid copy-pasting the dependency line with all of the enabled features from the `rustc_log` to the `rustc_driver_impl`'s Cargo.toml, which would have possibly led to redundancies and inconsistencies. r? `@RalfJung`
2 parents d087f11 + 781baaf commit 94dfe49

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,13 +1500,31 @@ pub fn init_rustc_env_logger(early_dcx: &EarlyDiagCtxt) {
15001500

15011501
/// This allows tools to enable rust logging without having to magically match rustc's
15021502
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose
1503-
/// the values directly rather than having to set an environment variable.
1503+
/// the logger config directly rather than having to set an environment variable.
15041504
pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15051505
if let Err(error) = rustc_log::init_logger(cfg) {
15061506
early_dcx.early_fatal(error.to_string());
15071507
}
15081508
}
15091509

1510+
/// This allows tools to enable rust logging without having to magically match rustc's
1511+
/// tracing crate version. In contrast to `init_rustc_env_logger`, it allows you to
1512+
/// choose the logger config directly rather than having to set an environment variable.
1513+
/// Moreover, in contrast to `init_logger`, it allows you to add a custom tracing layer
1514+
/// via `build_subscriber`, for example `|| Registry::default().with(custom_layer)`.
1515+
pub fn init_logger_with_additional_layer<F, T>(
1516+
early_dcx: &EarlyDiagCtxt,
1517+
cfg: rustc_log::LoggerConfig,
1518+
build_subscriber: F,
1519+
) where
1520+
F: FnOnce() -> T,
1521+
T: rustc_log::BuildSubscriberRet,
1522+
{
1523+
if let Err(error) = rustc_log::init_logger_with_additional_layer(cfg, build_subscriber) {
1524+
early_dcx.early_fatal(error.to_string());
1525+
}
1526+
}
1527+
15101528
/// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`].
15111529
/// Making this handler optional lets tools can install a different handler, if they wish.
15121530
pub fn install_ctrlc_handler() {

compiler/rustc_log/src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4343
use tracing_subscriber::fmt::FmtContext;
4444
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
4545
use tracing_subscriber::layer::SubscriberExt;
46+
use tracing_subscriber::{Layer, Registry};
4647

4748
/// The values of all the environment variables that matter for configuring a logger.
4849
/// Errors are explicitly preserved so that we can share error handling.
@@ -72,6 +73,36 @@ impl LoggerConfig {
7273

7374
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
7475
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
76+
init_logger_with_additional_layer(cfg, || Registry::default())
77+
}
78+
79+
/// Trait alias for the complex return type of `build_subscriber` in
80+
/// [init_logger_with_additional_layer]. A [Registry] with any composition of [tracing::Subscriber]s
81+
/// (e.g. `Registry::default().with(custom_layer)`) should be compatible with this type.
82+
/// Having an alias is also useful so rustc_driver_impl does not need to explicitly depend on
83+
/// `tracing_subscriber`.
84+
pub trait BuildSubscriberRet:
85+
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
86+
{
87+
}
88+
89+
impl<
90+
T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync,
91+
> BuildSubscriberRet for T
92+
{
93+
}
94+
95+
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
96+
/// Additionally add a custom layer to collect logging and tracing events via `build_subscriber`,
97+
/// for example: `|| Registry::default().with(custom_layer)`.
98+
pub fn init_logger_with_additional_layer<F, T>(
99+
cfg: LoggerConfig,
100+
build_subscriber: F,
101+
) -> Result<(), Error>
102+
where
103+
F: FnOnce() -> T,
104+
T: BuildSubscriberRet,
105+
{
75106
let filter = match cfg.filter {
76107
Ok(env) => EnvFilter::new(env),
77108
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
@@ -124,7 +155,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
124155
Err(_) => {} // no wraptree
125156
}
126157

127-
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
158+
let subscriber = build_subscriber().with(layer.with_filter(filter));
128159
match cfg.backtrace {
129160
Ok(backtrace_target) => {
130161
let fmt_layer = tracing_subscriber::fmt::layer()

0 commit comments

Comments
 (0)