@@ -12,6 +12,10 @@ extern crate log_settings;
1212extern crate syntax;
1313extern crate log;
1414
15+ use std:: path:: PathBuf ;
16+ use std:: str:: FromStr ;
17+ use std:: env;
18+
1519use rustc:: session:: Session ;
1620use rustc_metadata:: cstore:: CStore ;
1721use rustc_driver:: { Compilation , CompilerCalls , RustcDefaultCalls } ;
@@ -21,7 +25,6 @@ use rustc::hir::{self, itemlikevisit};
2125use rustc:: ty:: TyCtxt ;
2226use rustc_codegen_utils:: codegen_backend:: CodegenBackend ;
2327use syntax:: ast;
24- use std:: path:: PathBuf ;
2528
2629struct MiriCompilerCalls {
2730 default : Box < RustcDefaultCalls > ,
@@ -105,6 +108,7 @@ fn after_analysis<'a, 'tcx>(
105108 state : & mut CompileState < ' a , ' tcx > ,
106109 validate : bool ,
107110) {
111+ init_late_loggers ( ) ;
108112 state. session . abort_if_errors ( ) ;
109113
110114 let tcx = state. tcx . unwrap ( ) ;
@@ -148,42 +152,39 @@ fn after_analysis<'a, 'tcx>(
148152 }
149153}
150154
151- fn init_logger ( ) {
152- let format = |formatter : & mut env_logger:: fmt:: Formatter , record : & log:: Record | {
153- use std:: io:: Write ;
154- if record. level ( ) == log:: Level :: Trace {
155- // prepend frame number
156- let indentation = log_settings:: settings ( ) . indentation ;
157- writeln ! (
158- formatter,
159- "{indentation}:{lvl}:{module}: {text}" ,
160- lvl = record. level( ) ,
161- module = record. module_path( ) . unwrap_or( "<unknown module>" ) ,
162- indentation = indentation,
163- text = record. args( ) ,
164- )
165- } else {
166- writeln ! (
167- formatter,
168- "{lvl}:{module}: {text}" ,
169- lvl = record. level( ) ,
170- module = record. module_path( ) . unwrap_or( "<unknown_module>" ) ,
171- text = record. args( ) ,
172- )
173- }
174- } ;
175-
176- let mut builder = env_logger:: Builder :: new ( ) ;
177- builder. format ( format) . filter (
178- None ,
179- log:: LevelFilter :: Info ,
180- ) ;
181-
182- if std:: env:: var ( "MIRI_LOG" ) . is_ok ( ) {
183- builder. parse ( & std:: env:: var ( "MIRI_LOG" ) . unwrap ( ) ) ;
155+ fn init_early_loggers ( ) {
156+ // Notice that our `extern crate log` is NOT the same as rustc's! So we have to initialize
157+ // them both. We always initialize miri early.
158+ let env = env_logger:: Env :: new ( ) . filter ( "MIRI_LOG" ) . write_style ( "MIRI_LOG_STYLE" ) ;
159+ env_logger:: init_from_env ( env) ;
160+ // We only initialize rustc if the env var is set (so the user asked for it).
161+ // If it is not set, we avoid initializing now so that we can initialize
162+ // later with our custom settings, and NOT log anything for what happens before
163+ // miri gets started.
164+ if env:: var ( "RUST_LOG" ) . is_ok ( ) {
165+ rustc_driver:: init_rustc_env_logger ( ) ;
184166 }
167+ }
185168
186- builder. init ( ) ;
169+ fn init_late_loggers ( ) {
170+ // Initializing loggers right before we start evaluation. We overwrite the RUST_LOG
171+ // env var if it is not set, control it based on MIRI_LOG.
172+ if let Ok ( var) = env:: var ( "MIRI_LOG" ) {
173+ if env:: var ( "RUST_LOG" ) . is_err ( ) {
174+ // We try to be a bit clever here: If MIRI_LOG is just a single level
175+ // used for everything, we only apply it to the parts of rustc that are
176+ // CTFE-related. Otherwise, we use it verbatim for RUST_LOG.
177+ // This way, if you set `MIRI_LOG=trace`, you get only the right parts of
178+ // rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_mir::interpret=debug`.
179+ if log:: Level :: from_str ( & var) . is_ok ( ) {
180+ env:: set_var ( "RUST_LOG" ,
181+ & format ! ( "rustc::mir::interpret={0},rustc_mir::interpret={0}" , var) ) ;
182+ } else {
183+ env:: set_var ( "RUST_LOG" , & var) ;
184+ }
185+ rustc_driver:: init_rustc_env_logger ( ) ;
186+ }
187+ }
187188}
188189
189190fn find_sysroot ( ) -> String {
@@ -208,8 +209,7 @@ fn find_sysroot() -> String {
208209}
209210
210211fn main ( ) {
211- rustc_driver:: init_rustc_env_logger ( ) ;
212- init_logger ( ) ;
212+ init_early_loggers ( ) ;
213213 let mut args: Vec < String > = std:: env:: args ( ) . collect ( ) ;
214214
215215 let sysroot_flag = String :: from ( "--sysroot" ) ;
0 commit comments