Skip to content

Commit 5397f2e

Browse files
committed
when using MIRI_LOG, avoid logging for what rustc does before miri gets started
1 parent 7a6a687 commit 5397f2e

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/bin/miri.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ fn after_analysis<'a, 'tcx>(
107107
state: &mut CompileState<'a, 'tcx>,
108108
validate: bool,
109109
) {
110+
init_late_loggers();
110111
state.session.abort_if_errors();
111112

112113
let tcx = state.tcx.unwrap();
@@ -150,16 +151,25 @@ fn after_analysis<'a, 'tcx>(
150151
}
151152
}
152153

153-
fn init_loggers() {
154+
fn init_early_loggers() {
154155
// Notice that our `extern crate log` is NOT the same as rustc's! So we have to initialize
155-
// them both.
156-
// First, miri.
156+
// them both. We always initialize miri early.
157157
let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
158158
env_logger::init_from_env(env);
159-
// Now, change the RUST_LOG env var to control rustc's logger.
160-
// If MIRI_LOG is set and RUST_LOG is not, set RUST_LOG.
159+
// We only initialize rustc if the env var is set (so the user asked for it).
160+
// If it is not set, we avoid initializing now so that we can initialize
161+
// later with our custom settings, and NOT log anything for what happens before
162+
// miri gets started.
163+
if env::var("RUST_LOG").is_ok() {
164+
rustc_driver::init_rustc_env_logger();
165+
}
166+
}
167+
168+
fn init_late_loggers() {
169+
// Initializing loggers right before we start evaluation. We overwrite the RUST_LOG
170+
// env var if it is not set, control it based on MIRI_LOG.
161171
if let Ok(var) = env::var("MIRI_LOG") {
162-
if env::var("RUST_LOG") == Err(env::VarError::NotPresent) {
172+
if env::var("RUST_LOG").is_err() {
163173
// We try to be a bit clever here: If MIRI_LOG is just a single level
164174
// used for everything, we only apply it to the parts of rustc that are
165175
// CTFE-related. Only if MIRI_LOG contains `module=level`, we just
@@ -172,9 +182,9 @@ fn init_loggers() {
172182
env::set_var("RUST_LOG",
173183
&format!("rustc::mir::interpret={0},rustc_mir::interpret={0}", var));
174184
}
185+
rustc_driver::init_rustc_env_logger();
175186
}
176187
}
177-
rustc_driver::init_rustc_env_logger();
178188
}
179189

180190
fn find_sysroot() -> String {
@@ -199,7 +209,7 @@ fn find_sysroot() -> String {
199209
}
200210

201211
fn main() {
202-
init_loggers();
212+
init_early_loggers();
203213
let mut args: Vec<String> = std::env::args().collect();
204214

205215
let sysroot_flag = String::from("--sysroot");

0 commit comments

Comments
 (0)