Skip to content

Commit 66a812f

Browse files
committed
Merge pull request #16 from oli-obk/logs
use the `log` crate + `env_logger`
2 parents c815170 + fee3a2c commit 66a812f

File tree

6 files changed

+180
-34
lines changed

6 files changed

+180
-34
lines changed

Cargo.lock

+106-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ test = false
1616

1717
[dependencies]
1818
byteorder = "0.4.2"
19+
env_logger = "0.3.3"
20+
log = "0.3.6"
21+
log_settings = "0.1.1"
1922

2023
[dev-dependencies]
2124
compiletest_rs = "0.1.1"

src/bin/miri.rs

+29
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,32 @@ 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+
const NSPACES: usize = 40;
45+
let format = |record: &log::LogRecord| {
46+
// prepend spaces to indent the final string
47+
let indentation = log_settings::settings().indentation;
48+
format!("{lvl}:{module}{depth:2}{indent:<indentation$} {text}",
49+
lvl = record.level(),
50+
module = record.location().module_path(),
51+
depth = indentation / NSPACES,
52+
indentation = indentation % NSPACES,
53+
indent = "",
54+
text = record.args())
55+
};
56+
57+
let mut builder = env_logger::LogBuilder::new();
58+
builder.format(format).filter(None, log::LogLevelFilter::Info);
59+
60+
if std::env::var("MIRI_LOG").is_ok() {
61+
builder.parse(&std::env::var("MIRI_LOG").unwrap());
62+
}
63+
64+
builder.init().unwrap();
65+
}

src/interpreter.rs

+15-25
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use error::{EvalError, EvalResult};
2020
use memory::{Memory, Pointer};
2121
use primval::{self, PrimVal};
2222

23-
const TRACE_EXECUTION: bool = true;
24-
2523
struct GlobalEvalContext<'a, 'tcx: 'a> {
2624
/// The results of the type checker, from rustc.
2725
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -168,32 +166,24 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
168166
r
169167
}
170168

171-
fn log<F>(&self, extra_indent: usize, f: F) where F: FnOnce() {
172-
let indent = self.stack.len() + extra_indent;
173-
if !TRACE_EXECUTION { return; }
174-
for _ in 0..indent { print!(" "); }
175-
f();
176-
println!("");
177-
}
178-
179169
fn run(&mut self) -> EvalResult<()> {
180170
'outer: while !self.stack.is_empty() {
181171
let mut current_block = self.frame().next_block;
182172

183173
loop {
184-
self.log(0, || print!("// {:?}", current_block));
174+
trace!("// {:?}", current_block);
185175
let current_mir = self.mir().clone(); // Cloning a reference.
186176
let block_data = current_mir.basic_block_data(current_block);
187177

188178
for stmt in &block_data.statements {
189-
self.log(0, || print!("{:?}", stmt));
179+
trace!("{:?}", stmt);
190180
let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
191181
let result = self.eval_assignment(lvalue, rvalue);
192182
self.maybe_report(stmt.span, result)?;
193183
}
194184

195185
let terminator = block_data.terminator();
196-
self.log(0, || print!("{:?}", terminator.kind));
186+
trace!("{:?}", terminator.kind);
197187

198188
let result = self.eval_terminator(terminator);
199189
match self.maybe_report(terminator.span, result)? {
@@ -245,6 +235,8 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
245235
let num_args = mir.arg_decls.len();
246236
let num_vars = mir.var_decls.len();
247237

238+
::log_settings::settings().indentation += 1;
239+
248240
self.stack.push(Frame {
249241
mir: mir.clone(),
250242
next_block: mir::START_BLOCK,
@@ -256,6 +248,7 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
256248
}
257249

258250
fn pop_stack_frame(&mut self) {
251+
::log_settings::settings().indentation -= 1;
259252
let _frame = self.stack.pop().expect("tried to pop a stack frame, but there were none");
260253
// TODO(solson): Deallocate local variables.
261254
self.substs_stack.pop();
@@ -419,10 +412,10 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
419412

420413
fn drop(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<()> {
421414
if !self.type_needs_drop(ty) {
422-
self.log(1, || print!("no need to drop {:?}", ty));
415+
debug!("no need to drop {:?}", ty);
423416
return Ok(());
424417
}
425-
self.log(1, || print!("need to drop {:?}", ty));
418+
trace!("-need to drop {:?}", ty);
426419

427420
// TODO(solson): Call user-defined Drop::drop impls.
428421

@@ -431,7 +424,7 @@ impl<'a, 'b, 'mir, 'tcx> FnEvalContext<'a, 'b, 'mir, 'tcx> {
431424
match self.memory.read_ptr(ptr) {
432425
Ok(contents_ptr) => {
433426
self.drop(contents_ptr, contents_ty)?;
434-
self.log(1, || print!("deallocating box"));
427+
trace!("-deallocating box");
435428
self.memory.deallocate(contents_ptr)?;
436429
}
437430
Err(EvalError::ReadBytesAsPointer) => {
@@ -1421,32 +1414,29 @@ pub fn interpret_start_points<'a, 'tcx>(
14211414
tcx: TyCtxt<'a, 'tcx, 'tcx>,
14221415
mir_map: &MirMap<'tcx>,
14231416
) {
1417+
let initial_indentation = ::log_settings::settings().indentation;
14241418
for (&id, mir) in &mir_map.map {
14251419
for attr in tcx.map.attrs(id) {
14261420
use syntax::attr::AttrMetaMethods;
14271421
if attr.check_name("miri_run") {
14281422
let item = tcx.map.expect_item(id);
14291423

1430-
if TRACE_EXECUTION {
1431-
println!("Interpreting: {}", item.name);
1432-
}
1424+
::log_settings::settings().indentation = initial_indentation;
1425+
1426+
debug!("Interpreting: {}", item.name);
14331427

14341428
let mut gecx = GlobalEvalContext::new(tcx, mir_map);
14351429
let mut fecx = FnEvalContext::new(&mut gecx);
14361430
match fecx.call_nested(mir) {
1437-
Ok(Some(return_ptr)) => if TRACE_EXECUTION {
1431+
Ok(Some(return_ptr)) => if log_enabled!(::log::LogLevel::Debug) {
14381432
fecx.memory.dump(return_ptr.alloc_id);
14391433
},
1440-
Ok(None) => println!("(diverging function returned)"),
1434+
Ok(None) => warn!("diverging function returned"),
14411435
Err(_e) => {
14421436
// TODO(solson): Detect whether the error was already reported or not.
14431437
// tcx.sess.err(&e.to_string());
14441438
}
14451439
}
1446-
1447-
if TRACE_EXECUTION {
1448-
println!("");
1449-
}
14501440
}
14511441
}
14521442
}

src/lib.rs

+2
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;

tests/run-fail/inception.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,32 @@ fn run_miri(file: &str, sysroot: &str) -> Output {
99
let libpath = libpath.to_str().unwrap();
1010
let libpath2 = path.join("target").join("debug").join("deps");
1111
let libpath2 = libpath2.to_str().unwrap();
12+
let mut args = vec![
13+
"run".to_string(), "--".to_string(),
14+
"--sysroot".to_string(), sysroot.to_string(),
15+
"-L".to_string(), libpath.to_string(),
16+
"-L".to_string(), libpath2.to_string(),
17+
file.to_string()
18+
];
19+
for file in std::fs::read_dir("target/debug/deps").unwrap() {
20+
let file = file.unwrap();
21+
if file.file_type().unwrap().is_file() {
22+
let path = file.path();
23+
if let Some(ext) = path.extension() {
24+
if ext == "rlib" {
25+
let name = path.file_stem().unwrap().to_str().unwrap();
26+
if let Some(dash) = name.rfind('-') {
27+
if name.starts_with("lib") {
28+
args.push("--extern".to_string());
29+
args.push(format!("{}={}", &name[3..dash], path.to_str().unwrap()));
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
1236
Command::new("cargo")
13-
.args(&[
14-
"run", "--",
15-
"--sysroot", sysroot,
16-
"-L", libpath,
17-
"-L", libpath2,
18-
file
19-
])
37+
.args(&args)
2038
.output()
2139
.unwrap_or_else(|e| panic!("failed to execute process: {}", e))
2240
}

0 commit comments

Comments
 (0)