Skip to content

Commit

Permalink
Miri: use new init_logger function, avoid calling env::set_var
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 11, 2023
1 parent 8eb8e5f commit d61eda3
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/tools/miri/src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_log;
extern crate rustc_metadata;
extern crate rustc_middle;
extern crate rustc_session;

use std::env;
use std::env::{self, VarError};
use std::num::NonZeroU64;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -183,44 +184,52 @@ macro_rules! show_error {
($($tt:tt)*) => { show_error(&format_args!($($tt)*)) };
}

fn rustc_logger_config() -> rustc_log::LoggerConfig {
// Start with the usual env vars.
let mut cfg = rustc_log::LoggerConfig::from_env("RUSTC_LOG");
// Enable verbose entry/exit logging by default if MIRI_LOG is set.
if env::var_os("MIRI_LOG").is_some()
&& matches!(cfg.verbose_entry_exit, Err(VarError::NotPresent))
{
cfg.verbose_entry_exit = Ok(format!("1"));
}

cfg
}

fn init_early_loggers(handler: &EarlyErrorHandler) {
// Note that our `extern crate log` is *not* the same as rustc's; as a result, we have to
// initialize them both, and we always initialize `miri`'s first.
let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
env_logger::init_from_env(env);
// Enable verbose entry/exit logging by default if MIRI_LOG is set.
if env::var_os("MIRI_LOG").is_some() && env::var_os("RUSTC_LOG_ENTRY_EXIT").is_none() {
env::set_var("RUSTC_LOG_ENTRY_EXIT", "1");
}
// We only initialize `rustc` if the env var is set (so the user asked for it).
// If it is not set, we avoid initializing now so that we can initialize
// later with our custom settings, and *not* log anything for what happens before
// `miri` gets started.
// Now for rustc. We only initialize `rustc` if the env var is set (so the user asked for it).
// If it is not set, we avoid initializing now so that we can initialize later with our custom
// settings, and *not* log anything for what happens before `miri` gets started.
if env::var_os("RUSTC_LOG").is_some() {
rustc_driver::init_rustc_env_logger(handler);
rustc_driver::init_logger(handler, rustc_logger_config());
}
}

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

Expand Down

0 comments on commit d61eda3

Please sign in to comment.