1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![experimental]
use prelude::*;
use any::{Any, AnyRefExt};
use cell::RefCell;
use fmt;
use io::IoResult;
use rt::{backtrace, unwind};
use rt::util::{Stderr, Stdio};
use thread::Thread;
thread_local! {
pub static LOCAL_STDERR: RefCell<Option<Box<Writer + Send>>> = {
RefCell::new(None)
}
}
impl Writer for Stdio {
fn write(&mut self, bytes: &[u8]) -> IoResult<()> {
fn fmt_write<F: fmt::FormatWriter>(f: &mut F, bytes: &[u8]) {
let _ = f.write(bytes);
}
fmt_write(self, bytes);
Ok(())
}
}
pub fn on_fail(obj: &(Any+Send), file: &'static str, line: uint) {
let msg = match obj.downcast_ref::<&'static str>() {
Some(s) => *s,
None => match obj.downcast_ref::<String>() {
Some(s) => s[],
None => "Box<Any>",
}
};
let mut err = Stderr;
let thread = Thread::current();
let name = thread.name().unwrap_or("<unnamed>");
let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
match prev {
Some(mut stderr) => {
let _ = writeln!(stderr,
"thread '{}' panicked at '{}', {}:{}\n",
name, msg, file, line);
if backtrace::log_enabled() {
let _ = backtrace::write(&mut *stderr);
}
let mut s = Some(stderr);
LOCAL_STDERR.with(|slot| {
*slot.borrow_mut() = s.take();
});
}
None => {
let _ = writeln!(&mut err, "thread '{}' panicked at '{}', {}:{}",
name, msg, file, line);
if backtrace::log_enabled() {
let _ = backtrace::write(&mut err);
}
}
}
if unwind::panicking() && !backtrace::log_enabled() {
let _ = backtrace::write(&mut err);
}
}