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;
46
47/// The values of all the environment variables that matter for configuring a logger.
48/// Errors are explicitly preserved so that we can share error handling.
49pub struct LoggerConfig {
50    pub filter: Result<String, VarError>,
51    pub color_logs: Result<String, VarError>,
52    pub verbose_entry_exit: Result<String, VarError>,
53    pub verbose_thread_ids: Result<String, VarError>,
54    pub backtrace: Result<String, VarError>,
55    pub wraptree: Result<String, VarError>,
56    pub lines: Result<String, VarError>,
57}
58
59impl LoggerConfig {
60    pub fn from_env(env: &str) -> Self {
61        LoggerConfig {
62            filter: env::var(env),
63            color_logs: env::var(format!("{env}_COLOR")),
64            verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
65            verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
66            backtrace: env::var(format!("{env}_BACKTRACE")),
67            wraptree: env::var(format!("{env}_WRAPTREE")),
68            lines: env::var(format!("{env}_LINES")),
69        }
70    }
71}
72
73/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
74pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
75    let filter = match cfg.filter {
76        Ok(env) => EnvFilter::new(env),
77        _ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
78    };
79
80    let color_logs = match cfg.color_logs {
81        Ok(value) => match value.as_ref() {
82            "always" => true,
83            "never" => false,
84            "auto" => stderr_isatty(),
85            _ => return Err(Error::InvalidColorValue(value)),
86        },
87        Err(VarError::NotPresent) => stderr_isatty(),
88        Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
89    };
90
91    let verbose_entry_exit = match cfg.verbose_entry_exit {
92        Ok(v) => &v != "0",
93        Err(_) => false,
94    };
95
96    let verbose_thread_ids = match cfg.verbose_thread_ids {
97        Ok(v) => &v == "1",
98        Err(_) => false,
99    };
100
101    let lines = match cfg.lines {
102        Ok(v) => &v == "1",
103        Err(_) => false,
104    };
105
106    let mut layer = tracing_tree::HierarchicalLayer::default()
107        .with_writer(io::stderr)
108        .with_ansi(color_logs)
109        .with_targets(true)
110        .with_verbose_exit(verbose_entry_exit)
111        .with_verbose_entry(verbose_entry_exit)
112        .with_indent_amount(2)
113        .with_indent_lines(lines)
114        .with_thread_ids(verbose_thread_ids)
115        .with_thread_names(verbose_thread_ids);
116
117    match cfg.wraptree {
118        Ok(v) => match v.parse::<usize>() {
119            Ok(v) => {
120                layer = layer.with_wraparound(v);
121            }
122            Err(_) => return Err(Error::InvalidWraptree(v)),
123        },
124        Err(_) => {} // no wraptree
125    }
126
127    let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
128    match cfg.backtrace {
129        Ok(backtrace_target) => {
130            let fmt_layer = tracing_subscriber::fmt::layer()
131                .with_writer(io::stderr)
132                .without_time()
133                .event_format(BacktraceFormatter { backtrace_target });
134            let subscriber = subscriber.with(fmt_layer);
135            tracing::subscriber::set_global_default(subscriber)?;
136        }
137        Err(_) => {
138            tracing::subscriber::set_global_default(subscriber)?;
139        }
140    };
141
142    Ok(())
143}
144
145struct BacktraceFormatter {
146    backtrace_target: String,
147}
148
149impl<S, N> FormatEvent<S, N> for BacktraceFormatter
150where
151    S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
152    N: for<'a> FormatFields<'a> + 'static,
153{
154    fn format_event(
155        &self,
156        _ctx: &FmtContext<'_, S, N>,
157        mut writer: format::Writer<'_>,
158        event: &Event<'_>,
159    ) -> fmt::Result {
160        let target = event.metadata().target();
161        if !target.contains(&self.backtrace_target) {
162            return Ok(());
163        }
164        // Use Backtrace::force_capture because we don't want to depend on the
165        // RUST_BACKTRACE environment variable being set.
166        let backtrace = std::backtrace::Backtrace::force_capture();
167        writeln!(writer, "stack backtrace: \n{backtrace:?}")
168    }
169}
170
171pub fn stdout_isatty() -> bool {
172    io::stdout().is_terminal()
173}
174
175pub fn stderr_isatty() -> bool {
176    io::stderr().is_terminal()
177}
178
179#[derive(Debug)]
180pub enum Error {
181    InvalidColorValue(String),
182    NonUnicodeColorValue,
183    InvalidWraptree(String),
184    AlreadyInit(SetGlobalDefaultError),
185}
186
187impl std::error::Error for Error {}
188
189impl Display for Error {
190    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
191        match self {
192            Error::InvalidColorValue(value) => write!(
193                formatter,
194                "invalid log color value '{value}': expected one of always, never, or auto",
195            ),
196            Error::NonUnicodeColorValue => write!(
197                formatter,
198                "non-Unicode log color value: expected one of always, never, or auto",
199            ),
200            Error::InvalidWraptree(value) => write!(
201                formatter,
202                "invalid log WRAPTREE value '{value}': expected a non-negative integer",
203            ),
204            Error::AlreadyInit(tracing_error) => Display::fmt(tracing_error, formatter),
205        }
206    }
207}
208
209impl From<SetGlobalDefaultError> for Error {
210    fn from(tracing_error: SetGlobalDefaultError) -> Self {
211        Error::AlreadyInit(tracing_error)
212    }
213}