]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
11 #![unstable(feature = "std_misc")]
18 use rt
::{backtrace, unwind}
;
19 use sys
::stdio
::Stderr
;
22 // Defined in this module instead of old_io::stdio so that the unwinding
24 pub static LOCAL_STDERR
: RefCell
<Option
<Box
<Write
+ Send
>>> = {
29 pub fn on_panic(obj
: &(Any
+Send
), file
: &'
static str, line
: usize) {
30 let msg
= match obj
.downcast_ref
::<&'
static str>() {
32 None
=> match obj
.downcast_ref
::<String
>() {
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());
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
);
50 let mut s
= Some(stderr
);
51 LOCAL_STDERR
.with(|slot
| {
52 *slot
.borrow_mut() = s
.take();
56 let _
= writeln
!(&mut err
, "thread '{}' panicked at '{}', {}:{}",
57 name
, msg
, file
, line
);
58 if backtrace
::log_enabled() {
59 let _
= backtrace
::write(&mut err
);
64 // If this is a double panic, make sure that we printed a backtrace
66 if unwind
::panicking() && !backtrace
::log_enabled() {
67 let _
= backtrace
::write(&mut err
);