rustc_log/
lib.rs

1//! This crate allows tools to enable rust logging without having to magically
2//! match rustc's tracing crate version.
3//!
4//! For example if someone is working on rustc_ast and wants to write some
5//! minimal code against it to run in a debugger, with access to the `debug!`
6//! logs emitted by rustc_ast, that can be done by writing:
7//!
8//! ```toml
9//! [dependencies]
10//! rustc_ast = { path = "../rust/compiler/rustc_ast" }
11//! rustc_log = { path = "../rust/compiler/rustc_log" }
12//! ```
13//!
14//! ```
15//! fn main() {
16//!     rustc_log::init_logger(rustc_log::LoggerConfig::from_env("LOG")).unwrap();
17//!     /* ... */
18//! }
19//! ```
20//!
21//! Now `LOG=debug cargo +nightly run` will run your minimal main.rs and show
22//! rustc's debug logging. In a workflow like this, one might also add
23//! `std::env::set_var("LOG", "debug")` to the top of main so that `cargo
24//! +nightly run` by itself is sufficient to get logs.
25//!
26//! The reason rustc_log is a tiny separate crate, as opposed to exposing the
27//! same things in rustc_driver only, is to enable the above workflow. If you
28//! had to depend on rustc_driver in order to turn on rustc's debug logs, that's
29//! an enormously bigger dependency tree; every change you make to rustc_ast (or
30//! whichever piece of the compiler you are interested in) would involve
31//! rebuilding all the rest of rustc up to rustc_driver in order to run your
32//! main.rs. Whereas by depending only on rustc_log and the few crates you are
33//! debugging, you can make changes inside those crates and quickly run main.rs
34//! to read the debug logs.
35
36use std::env::{self, VarError};
37use std::fmt::{self, Display};
38use std::io::{self, IsTerminal};
39
40use tracing::dispatcher::SetGlobalDefaultError;
41use tracing_core::{Event, Subscriber};
42use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
43use tracing_subscriber::fmt::FmtContext;
44use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
45use tracing_subscriber::layer::SubscriberExt;
46use tracing_subscriber::{Layer, Registry};
47
48/// The values of all the environment variables that matter for configuring a logger.
49/// Errors are explicitly preserved so that we can share error handling.
50pub struct LoggerConfig {
51    pub filter: Result<String, VarError>,
52    pub color_logs: Result<String, VarError>,
53    pub verbose_entry_exit: Result<String, VarError>,
54    pub verbose_thread_ids: Result<String, VarError>,
55    pub backtrace: Result<String, VarError>,
56    pub wraptree: Result<String, VarError>,
57    pub lines: Result<String, VarError>,
58}
59
60impl LoggerConfig {
61    pub fn from_env(env: &str) -> Self {
62        LoggerConfig {
63            filter: env::var(env),
64            color_logs: env::var(format!("{env}_COLOR")),
65            verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
66            verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
67            backtrace: env::var(format!("{env}_BACKTRACE")),
68            wraptree: env::var(format!("{env}_WRAPTREE")),
69            lines: env::var(format!("{env}_LINES")),
70        }
71    }
72}
73
74/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
75pub 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`.
84pub trait BuildSubscriberRet:
85    tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
86{
87}
88
89impl<
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)`.
98pub fn init_logger_with_additional_layer<F, T>(
99    cfg: LoggerConfig,
100    build_subscriber: F,
101) -> Result<(), Error>
102where
103    F: FnOnce() -> T,
104    T: BuildSubscriberRet,
105{
106    let filter = match cfg.filter {
107        Ok(env) => EnvFilter::new(env),
108        _ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
109    };
110
111    let color_logs = match cfg.color_logs {
112        Ok(value) => match value.as_ref() {
113            "always" => true,
114            "never" => false,
115            "auto" => stderr_isatty(),
116            _ => return Err(Error::InvalidColorValue(value)),
117        },
118        Err(VarError::NotPresent) => stderr_isatty(),
119        Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
120    };
121
122    let verbose_entry_exit = match cfg.verbose_entry_exit {
123        Ok(v) => &v != "0",
124        Err(_) => false,
125    };
126
127    let verbose_thread_ids = match cfg.verbose_thread_ids {
128        Ok(v) => &v == "1",
129        Err(_) => false,
130    };
131
132    let lines = match cfg.lines {
133        Ok(v) => &v == "1",
134        Err(_) => false,
135    };
136
137    let mut layer = tracing_tree::HierarchicalLayer::default()
138        .with_writer(io::stderr)
139        .with_ansi(color_logs)
140        .with_targets(true)
141        .with_verbose_exit(verbose_entry_exit)
142        .with_verbose_entry(verbose_entry_exit)
143        .with_indent_amount(2)
144        .with_indent_lines(lines)
145        .with_thread_ids(verbose_thread_ids)
146        .with_thread_names(verbose_thread_ids);
147
148    match cfg.wraptree {
149        Ok(v) => match v.parse::<usize>() {
150            Ok(v) => {
151                layer = layer.with_wraparound(v);
152            }
153            Err(_) => return Err(Error::InvalidWraptree(v)),
154        },
155        Err(_) => {} // no wraptree
156    }
157
158    let subscriber = build_subscriber().with(layer.with_filter(filter));
159    match cfg.backtrace {
160        Ok(backtrace_target) => {
161            let fmt_layer = tracing_subscriber::fmt::layer()
162                .with_writer(io::stderr)
163                .without_time()
164                .event_format(BacktraceFormatter { backtrace_target });
165            let subscriber = subscriber.with(fmt_layer);
166            tracing::subscriber::set_global_default(subscriber)?;
167        }
168        Err(_) => {
169            tracing::subscriber::set_global_default(subscriber)?;
170        }
171    };
172
173    Ok(())
174}
175
176struct BacktraceFormatter {
177    backtrace_target: String,
178}
179
180impl<S, N> FormatEvent<S, N> for BacktraceFormatter
181where
182    S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
183    N: for<'a> FormatFields<'a> + 'static,
184{
185    fn format_event(
186        &self,
187        _ctx: &FmtContext<'_, S, N>,
188        mut writer: format::Writer<'_>,
189        event: &Event<'_>,
190    ) -> fmt::Result {
191        let target = event.metadata().target();
192        if !target.contains(&self.backtrace_target) {
193            return Ok(());
194        }
195        // Use Backtrace::force_capture because we don't want to depend on the
196        // RUST_BACKTRACE environment variable being set.
197        let backtrace = std::backtrace::Backtrace::force_capture();
198        writeln!(writer, "stack backtrace: \n{backtrace:?}")
199    }
200}
201
202pub fn stdout_isatty() -> bool {
203    io::stdout().is_terminal()
204}
205
206pub fn stderr_isatty() -> bool {
207    io::stderr().is_terminal()
208}
209
210#[derive(Debug)]
211pub enum Error {
212    InvalidColorValue(String),
213    NonUnicodeColorValue,
214    InvalidWraptree(String),
215    AlreadyInit(SetGlobalDefaultError),
216}
217
218impl std::error::Error for Error {}
219
220impl Display for Error {
221    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
222        match self {
223            Error::InvalidColorValue(value) => write!(
224                formatter,
225                "invalid log color value '{value}': expected one of always, never, or auto",
226            ),
227            Error::NonUnicodeColorValue => write!(
228                formatter,
229                "non-Unicode log color value: expected one of always, never, or auto",
230            ),
231            Error::InvalidWraptree(value) => write!(
232                formatter,
233                "invalid log WRAPTREE value '{value}': expected a non-negative integer",
234            ),
235            Error::AlreadyInit(tracing_error) => Display::fmt(tracing_error, formatter),
236        }
237    }
238}
239
240impl From<SetGlobalDefaultError> for Error {
241    fn from(tracing_error: SetGlobalDefaultError) -> Self {
242        Error::AlreadyInit(tracing_error)
243    }
244}