Skip to content

Commit d61eda3

Browse files
committed
Miri: use new init_logger function, avoid calling env::set_var
1 parent 8eb8e5f commit d61eda3

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/tools/miri/src/bin/miri.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ extern crate rustc_data_structures;
99
extern crate rustc_driver;
1010
extern crate rustc_hir;
1111
extern crate rustc_interface;
12+
extern crate rustc_log;
1213
extern crate rustc_metadata;
1314
extern crate rustc_middle;
1415
extern crate rustc_session;
1516

16-
use std::env;
17+
use std::env::{self, VarError};
1718
use std::num::NonZeroU64;
1819
use std::path::PathBuf;
1920
use std::str::FromStr;
@@ -183,44 +184,52 @@ macro_rules! show_error {
183184
($($tt:tt)*) => { show_error(&format_args!($($tt)*)) };
184185
}
185186

187+
fn rustc_logger_config() -> rustc_log::LoggerConfig {
188+
// Start with the usual env vars.
189+
let mut cfg = rustc_log::LoggerConfig::from_env("RUSTC_LOG");
190+
// Enable verbose entry/exit logging by default if MIRI_LOG is set.
191+
if env::var_os("MIRI_LOG").is_some()
192+
&& matches!(cfg.verbose_entry_exit, Err(VarError::NotPresent))
193+
{
194+
cfg.verbose_entry_exit = Ok(format!("1"));
195+
}
196+
197+
cfg
198+
}
199+
186200
fn init_early_loggers(handler: &EarlyErrorHandler) {
187201
// Note that our `extern crate log` is *not* the same as rustc's; as a result, we have to
188202
// initialize them both, and we always initialize `miri`'s first.
189203
let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
190204
env_logger::init_from_env(env);
191-
// Enable verbose entry/exit logging by default if MIRI_LOG is set.
192-
if env::var_os("MIRI_LOG").is_some() && env::var_os("RUSTC_LOG_ENTRY_EXIT").is_none() {
193-
env::set_var("RUSTC_LOG_ENTRY_EXIT", "1");
194-
}
195-
// We only initialize `rustc` if the env var is set (so the user asked for it).
196-
// If it is not set, we avoid initializing now so that we can initialize
197-
// later with our custom settings, and *not* log anything for what happens before
198-
// `miri` gets started.
205+
// Now for rustc. We only initialize `rustc` if the env var is set (so the user asked for it).
206+
// If it is not set, we avoid initializing now so that we can initialize later with our custom
207+
// settings, and *not* log anything for what happens before `miri` gets started.
199208
if env::var_os("RUSTC_LOG").is_some() {
200-
rustc_driver::init_rustc_env_logger(handler);
209+
rustc_driver::init_logger(handler, rustc_logger_config());
201210
}
202211
}
203212

204213
fn init_late_loggers(handler: &EarlyErrorHandler, tcx: TyCtxt<'_>) {
205-
// We initialize loggers right before we start evaluation. We overwrite the `RUSTC_LOG`
206-
// env var if it is not set, control it based on `MIRI_LOG`.
207-
// (FIXME: use `var_os`, but then we need to manually concatenate instead of `format!`.)
208-
if let Ok(var) = env::var("MIRI_LOG") {
209-
if env::var_os("RUSTC_LOG").is_none() {
214+
// If `RUSTC_LOG` is not set, then `init_early_loggers` did not call
215+
// `rustc_driver::init_logger`, so we have to do this now.
216+
if env::var_os("RUSTC_LOG").is_none() {
217+
let mut cfg = rustc_logger_config();
218+
if let Ok(var) = env::var("MIRI_LOG") {
219+
// Setup rustc logging of the Miri-relevant components.
210220
// We try to be a bit clever here: if `MIRI_LOG` is just a single level
211221
// used for everything, we only apply it to the parts of rustc that are
212222
// CTFE-related. Otherwise, we use it verbatim for `RUSTC_LOG`.
213223
// This way, if you set `MIRI_LOG=trace`, you get only the right parts of
214224
// rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_const_eval::interpret=debug`.
215225
if log::Level::from_str(&var).is_ok() {
216-
env::set_var(
217-
"RUSTC_LOG",
218-
format!("rustc_middle::mir::interpret={var},rustc_const_eval::interpret={var}"),
219-
);
226+
cfg.filter = Ok(format!(
227+
"rustc_middle::mir::interpret={var},rustc_const_eval::interpret={var}"
228+
));
220229
} else {
221-
env::set_var("RUSTC_LOG", &var);
230+
cfg.filter = Ok(var);
222231
}
223-
rustc_driver::init_rustc_env_logger(handler);
232+
rustc_driver::init_logger(handler, cfg);
224233
}
225234
}
226235

0 commit comments

Comments
 (0)