]> git.lizzy.rs Git - rust.git/blob - src/libstd/panicking.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.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
15 use any::Any;
16 use cell::RefCell;
17 use old_io::IoResult;
18 use rt::{backtrace, unwind};
19 use rt::util::{Stderr, Stdio};
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<Writer + Send>>> = {
25         RefCell::new(None)
26     }
27 }
28
29 impl Writer for Stdio {
30     fn write_all(&mut self, bytes: &[u8]) -> IoResult<()> {
31         let _ = self.write_bytes(bytes);
32         Ok(())
33     }
34 }
35
36 pub fn on_panic(obj: &(Any+Send), file: &'static str, line: uint) {
37     let msg = match obj.downcast_ref::<&'static str>() {
38         Some(s) => *s,
39         None => match obj.downcast_ref::<String>() {
40             Some(s) => &s[..],
41             None => "Box<Any>",
42         }
43     };
44     let mut err = Stderr;
45     let thread = thread::current();
46     let name = thread.name().unwrap_or("<unnamed>");
47     let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
48     match prev {
49         Some(mut stderr) => {
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);
56             }
57             let mut s = Some(stderr);
58             LOCAL_STDERR.with(|slot| {
59                 *slot.borrow_mut() = s.take();
60             });
61         }
62         None => {
63             let _ = writeln!(&mut err, "thread '{}' panicked at '{}', {}:{}",
64                              name, msg, file, line);
65             if backtrace::log_enabled() {
66                 let _ = backtrace::write(&mut err);
67             }
68         }
69     }
70
71     // If this is a double panic, make sure that we printed a backtrace
72     // for this panic.
73     if unwind::panicking() && !backtrace::log_enabled() {
74         let _ = backtrace::write(&mut err);
75     }
76 }