]> git.proxmox.com Git - rustc.git/blob - src/libstd/panicking.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / libstd / panicking.rs
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.
4 //
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.
10
11 #![unstable(feature = "std_misc")]
12
13 use prelude::v1::*;
14 use io::prelude::*;
15
16 use any::Any;
17 use cell::RefCell;
18 use rt::{backtrace, unwind};
19 use sys::stdio::Stderr;
20 use thread;
21
22 // Defined in this module instead of old_io::stdio so that the unwinding
23 thread_local! {
24 pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {
25 RefCell::new(None)
26 }
27 }
28
29 pub fn on_panic(obj: &(Any+Send), file: &'static str, line: usize) {
30 let msg = match obj.downcast_ref::<&'static str>() {
31 Some(s) => *s,
32 None => match obj.downcast_ref::<String>() {
33 Some(s) => &s[..],
34 None => "Box<Any>",
35 }
36 };
37 let mut err = Stderr::new();
38 let thread = thread::current();
39 let name = thread.name().unwrap_or("<unnamed>");
40 let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
41 match prev {
42 Some(mut stderr) => {
43 // FIXME: what to do when the thread printing panics?
44 let _ = writeln!(stderr,
45 "thread '{}' panicked at '{}', {}:{}\n",
46 name, msg, file, line);
47 if backtrace::log_enabled() {
48 let _ = backtrace::write(&mut *stderr);
49 }
50 let mut s = Some(stderr);
51 LOCAL_STDERR.with(|slot| {
52 *slot.borrow_mut() = s.take();
53 });
54 }
55 None => {
56 let _ = writeln!(&mut err, "thread '{}' panicked at '{}', {}:{}",
57 name, msg, file, line);
58 if backtrace::log_enabled() {
59 let _ = backtrace::write(&mut err);
60 }
61 }
62 }
63
64 // If this is a double panic, make sure that we printed a backtrace
65 // for this panic.
66 if unwind::panicking() && !backtrace::log_enabled() {
67 let _ = backtrace::write(&mut err);
68 }
69 }