1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
18 use rt::{backtrace, unwind};
19 use rt::util::{Stderr, Stdio};
22 // Defined in this module instead of io::stdio so that the unwinding
24 pub static LOCAL_STDERR: RefCell<Option<Box<Writer + Send>>> = {
29 impl Writer for Stdio {
30 fn write(&mut self, bytes: &[u8]) -> IoResult<()> {
31 let _ = self.write_bytes(bytes);
36 pub fn on_fail(obj: &(Any+Send), file: &'static str, line: uint) {
37 let msg = match obj.downcast_ref::<&'static str>() {
39 None => match obj.downcast_ref::<String>() {
45 let thread = Thread::current();
46 let name = thread.name().unwrap_or("<unnamed>");
47 let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
50 // FIXME: what to do when the thread printing panics?
51 let _ = writeln!(stderr,
52 "thread '{}' panicked at '{}', {}:{}\n",
53 name, msg, file, line);
54 if backtrace::log_enabled() {
55 let _ = backtrace::write(&mut *stderr);
57 let mut s = Some(stderr);
58 LOCAL_STDERR.with(|slot| {
59 *slot.borrow_mut() = s.take();
63 let _ = writeln!(&mut err, "thread '{}' panicked at '{}', {}:{}",
64 name, msg, file, line);
65 if backtrace::log_enabled() {
66 let _ = backtrace::write(&mut err);
71 // If this is a double panic, make sure that we printed a backtrace
73 if unwind::panicking() && !backtrace::log_enabled() {
74 let _ = backtrace::write(&mut err);