Skip to content

Commit a39252a

Browse files
committed
add the log crate + env_logger to be able to choose the log granularity at runtime
1 parent 39faf94 commit a39252a

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/bin/miri.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ extern crate getopts;
55
extern crate miri;
66
extern crate rustc;
77
extern crate rustc_driver;
8+
extern crate env_logger;
9+
extern crate log_settings;
10+
extern crate log;
811

912
use miri::interpreter;
1013
use rustc::session::Session;
@@ -31,6 +34,27 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
3134

3235
#[miri_run]
3336
fn main() {
37+
init_logger();
3438
let args: Vec<String> = std::env::args().collect();
3539
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
3640
}
41+
42+
#[miri_run]
43+
fn init_logger() {
44+
let format = |record: &log::LogRecord| {
45+
// prepend spaces to indent the final string
46+
let indentation = log_settings::settings().indentation;
47+
let spaces = " ";
48+
let indentation = &spaces[..std::cmp::min(indentation, spaces.len())];
49+
format!("{} -{} {}", record.level(), indentation, record.args())
50+
};
51+
52+
let mut builder = env_logger::LogBuilder::new();
53+
builder.format(format).filter(None, log::LogLevelFilter::Info);
54+
55+
if std::env::var("RUST_LOG").is_ok() {
56+
builder.parse(&std::env::var("RUST_LOG").unwrap());
57+
}
58+
59+
builder.init().unwrap();
60+
}

src/interpreter.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ use error::{EvalError, EvalResult};
2020
use memory::{Memory, Pointer};
2121
use primval::{self, PrimVal};
2222

23-
const TRACE_EXECUTION: bool = true;
24-
static mut INDENTATION: usize = 0;
25-
26-
fn log<F>(extra_indent: usize, f: F) where F: FnOnce() {
27-
let indent = unsafe { INDENTATION } + extra_indent;
28-
if !TRACE_EXECUTION { return; }
29-
for _ in 0..indent { print!(" "); }
30-
f();
31-
println!("");
32-
}
33-
3423
struct GlobalEvalContext<'a, 'tcx: 'a> {
3524
/// The results of the type checker, from rustc.
3625
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -182,19 +171,19 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
182171
let mut current_block = self.frame().next_block;
183172

184173
loop {
185-
log(0, || print!("// {:?}", current_block));
174+
trace!("// {:?}", current_block);
186175
let current_mir = self.mir().clone(); // Cloning a reference.
187176
let block_data = current_mir.basic_block_data(current_block);
188177

189178
for stmt in &block_data.statements {
190-
log(0, || print!("{:?}", stmt));
179+
trace!("{:?}", stmt);
191180
let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
192181
let result = self.eval_assignment(lvalue, rvalue);
193182
self.maybe_report(stmt.span, result)?;
194183
}
195184

196185
let terminator = block_data.terminator();
197-
log(0, || print!("{:?}", terminator.kind));
186+
trace!("{:?}", terminator.kind);
198187

199188
let result = self.eval_terminator(terminator);
200189
match self.maybe_report(terminator.span, result)? {
@@ -246,9 +235,7 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
246235
let num_args = mir.arg_decls.len();
247236
let num_vars = mir.var_decls.len();
248237

249-
unsafe {
250-
INDENTATION += 1;
251-
}
238+
::log_settings::settings().indentation += 1;
252239

253240
self.stack.push(Frame {
254241
mir: mir.clone(),
@@ -261,9 +248,7 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
261248
}
262249

263250
fn pop_stack_frame(&mut self) {
264-
unsafe {
265-
INDENTATION -= 1;
266-
}
251+
::log_settings::settings().indentation -= 1;
267252
let _frame = self.stack.pop().expect("tried to pop a stack frame, but there were none");
268253
// TODO(solson): Deallocate local variables.
269254
self.substs_stack.pop();
@@ -427,10 +412,10 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
427412

428413
fn drop(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<()> {
429414
if !self.type_needs_drop(ty) {
430-
log(1, || print!("no need to drop {:?}", ty));
415+
debug!("no need to drop {:?}", ty);
431416
return Ok(());
432417
}
433-
log(1, || print!("need to drop {:?}", ty));
418+
trace!("-need to drop {:?}", ty);
434419

435420
// TODO(solson): Call user-defined Drop::drop impls.
436421

@@ -439,7 +424,7 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
439424
match self.memory.read_ptr(ptr) {
440425
Ok(contents_ptr) => {
441426
self.drop(contents_ptr, contents_ty)?;
442-
log(1, || print!("deallocating box"));
427+
trace!("-deallocating box");
443428
self.memory.deallocate(contents_ptr)?;
444429
}
445430
Err(EvalError::ReadBytesAsPointer) => {
@@ -1429,36 +1414,29 @@ pub fn interpret_start_points<'a, 'tcx>(
14291414
tcx: TyCtxt<'a, 'tcx, 'tcx>,
14301415
mir_map: &MirMap<'tcx>,
14311416
) {
1417+
let initial_indentation = ::log_settings::settings().indentation;
14321418
for (&id, mir) in &mir_map.map {
14331419
for attr in tcx.map.attrs(id) {
14341420
use syntax::attr::AttrMetaMethods;
14351421
if attr.check_name("miri_run") {
14361422
let item = tcx.map.expect_item(id);
14371423

1438-
unsafe {
1439-
INDENTATION = 0;
1440-
}
1424+
::log_settings::settings().indentation = initial_indentation;
14411425

1442-
if TRACE_EXECUTION {
1443-
println!("Interpreting: {}", item.name);
1444-
}
1426+
debug!("Interpreting: {}", item.name);
14451427

14461428
let mut gecx = GlobalEvalContext::new(tcx, mir_map);
14471429
let mut fecx = FnEvalContext::new(&mut gecx);
14481430
match fecx.call_nested(mir) {
1449-
Ok(Some(return_ptr)) => if TRACE_EXECUTION {
1431+
Ok(Some(return_ptr)) => if log_enabled!(::log::LogLevel::Debug) {
14501432
fecx.memory.dump(return_ptr.alloc_id);
14511433
},
1452-
Ok(None) => println!("(diverging function returned)"),
1434+
Ok(None) => warn!("diverging function returned"),
14531435
Err(_e) => {
14541436
// TODO(solson): Detect whether the error was already reported or not.
14551437
// tcx.sess.err(&e.to_string());
14561438
}
14571439
}
1458-
1459-
if TRACE_EXECUTION {
1460-
println!("");
1461-
}
14621440
}
14631441
}
14641442
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#[macro_use] extern crate rustc;
1313
extern crate rustc_mir;
1414
extern crate syntax;
15+
#[macro_use] extern crate log;
16+
extern crate log_settings;
1517

1618
// From crates.io.
1719
extern crate byteorder;

0 commit comments

Comments
 (0)