Skip to content

Commit 581a317

Browse files
committed
rustc_log: provide a way to init logging based on the values, not names, of the env vars
1 parent 1db4b12 commit 581a317

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

compiler/rustc_driver_impl/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1524,14 +1524,14 @@ fn report_ice(
15241524
/// This allows tools to enable rust logging without having to magically match rustc's
15251525
/// tracing crate version.
15261526
pub fn init_rustc_env_logger(handler: &EarlyErrorHandler) {
1527-
init_env_logger(handler, "RUSTC_LOG");
1527+
init_logger(handler, rustc_log::LoggerConfig::from_env("RUSTC_LOG"));
15281528
}
15291529

15301530
/// This allows tools to enable rust logging without having to magically match rustc's
1531-
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose an env var
1532-
/// other than `RUSTC_LOG`.
1533-
pub fn init_env_logger(handler: &EarlyErrorHandler, env: &str) {
1534-
if let Err(error) = rustc_log::init_env_logger(env) {
1531+
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose
1532+
/// the values directly rather than having to set an environment variable.
1533+
pub fn init_logger(handler: &EarlyErrorHandler, cfg: rustc_log::LoggerConfig) {
1534+
if let Err(error) = rustc_log::init_logger(cfg) {
15351535
handler.early_error(error.to_string());
15361536
}
15371537
}

compiler/rustc_log/src/lib.rs

+34-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//!
1515
//! ```
1616
//! fn main() {
17-
//! rustc_log::init_env_logger("LOG").unwrap();
17+
//! rustc_log::init_logger(rustc_log::LoggerConfig::from_env("LOG")).unwrap();
1818
//!
1919
//! let edition = rustc_span::edition::Edition::Edition2021;
2020
//! rustc_span::create_session_globals_then(edition, || {
@@ -52,13 +52,36 @@ use tracing_subscriber::fmt::{
5252
};
5353
use tracing_subscriber::layer::SubscriberExt;
5454

55-
pub fn init_env_logger(env: &str) -> Result<(), Error> {
56-
let filter = match env::var(env) {
55+
/// The values of all the environment variables that matter for configuring a logger.
56+
/// Errors are explicitly preserved so that we can share error handling.
57+
pub struct LoggerConfig {
58+
pub filter: Result<String, VarError>,
59+
pub color_logs: Result<String, VarError>,
60+
pub verbose_entry_exit: Result<String, VarError>,
61+
pub verbose_thread_ids: Result<String, VarError>,
62+
pub backtrace: Result<String, VarError>,
63+
}
64+
65+
impl LoggerConfig {
66+
pub fn from_env(env: &str) -> Self {
67+
LoggerConfig {
68+
filter: env::var(env),
69+
color_logs: env::var(format!("{env}_COLOR")),
70+
verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
71+
verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
72+
backtrace: env::var(format!("{env}_BACKTRACE")),
73+
}
74+
}
75+
}
76+
77+
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
78+
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
79+
let filter = match cfg.filter {
5780
Ok(env) => EnvFilter::new(env),
5881
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
5982
};
6083

61-
let color_logs = match env::var(String::from(env) + "_COLOR") {
84+
let color_logs = match cfg.color_logs {
6285
Ok(value) => match value.as_ref() {
6386
"always" => true,
6487
"never" => false,
@@ -69,14 +92,14 @@ pub fn init_env_logger(env: &str) -> Result<(), Error> {
6992
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
7093
};
7194

72-
let verbose_entry_exit = match env::var_os(String::from(env) + "_ENTRY_EXIT") {
73-
None => false,
74-
Some(v) => &v != "0",
95+
let verbose_entry_exit = match cfg.verbose_entry_exit {
96+
Ok(v) => &v != "0",
97+
Err(_) => false,
7598
};
7699

77-
let verbose_thread_ids = match env::var_os(String::from(env) + "_THREAD_IDS") {
78-
None => false,
79-
Some(v) => &v == "1",
100+
let verbose_thread_ids = match cfg.verbose_thread_ids {
101+
Ok(v) => &v == "1",
102+
Err(_) => false,
80103
};
81104

82105
let layer = tracing_tree::HierarchicalLayer::default()
@@ -91,7 +114,7 @@ pub fn init_env_logger(env: &str) -> Result<(), Error> {
91114
.with_thread_names(verbose_thread_ids);
92115

93116
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
94-
match env::var(format!("{env}_BACKTRACE")) {
117+
match cfg.backtrace {
95118
Ok(str) => {
96119
let fmt_layer = tracing_subscriber::fmt::layer()
97120
.with_writer(io::stderr)

src/librustdoc/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern crate rustc_interface;
5353
extern crate rustc_lexer;
5454
extern crate rustc_lint;
5555
extern crate rustc_lint_defs;
56+
extern crate rustc_log;
5657
extern crate rustc_macros;
5758
extern crate rustc_metadata;
5859
extern crate rustc_middle;
@@ -175,7 +176,7 @@ pub fn main() {
175176
// in the sysroot), and all of rustdoc's logging goes to its version (the one in Cargo.toml).
176177

177178
init_logging(&handler);
178-
rustc_driver::init_env_logger(&handler, "RUSTDOC_LOG");
179+
rustc_driver::init_logger(&handler, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
179180

180181
let exit_code = rustc_driver::catch_with_exit_code(|| match get_args(&handler) {
181182
Some(args) => main_args(&mut handler, &args, using_internal_features),

src/tools/error_index_generator/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(rustc_private)]
22

33
extern crate rustc_driver;
4+
extern crate rustc_log;
45
extern crate rustc_session;
56

67
use std::env;
@@ -173,7 +174,7 @@ fn parse_args() -> (OutputFormat, PathBuf) {
173174
fn main() {
174175
let handler =
175176
rustc_session::EarlyErrorHandler::new(rustc_session::config::ErrorOutputType::default());
176-
rustc_driver::init_env_logger(&handler, "RUST_LOG");
177+
rustc_driver::init_logger(&handler, rustc_log::LoggerConfig::from_env("RUST_LOG"));
177178
let (format, dst) = parse_args();
178179
let result = main_with_result(format, &dst);
179180
if let Err(e) = result {

0 commit comments

Comments
 (0)