]> git.lizzy.rs Git - rust.git/blob - src/libstd/failure.rs
c23e043c174091876a60189b0c7f44a229148407
[rust.git] / src / libstd / failure.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 #![experimental]
12
13 use alloc::boxed::Box;
14 use any::{Any, AnyRefExt};
15 use fmt;
16 use io::{Writer, IoResult};
17 use kinds::Send;
18 use option::{Some, None};
19 use result::Ok;
20 use rt::backtrace;
21 use rustrt::{Stderr, Stdio};
22 use rustrt::local::Local;
23 use rustrt::task::Task;
24 use str::Str;
25 use string::String;
26
27 // Defined in this module instead of io::stdio so that the unwinding
28 local_data_key!(pub local_stderr: Box<Writer + Send>)
29
30 impl Writer for Stdio {
31     fn write(&mut self, bytes: &[u8]) -> IoResult<()> {
32         fn fmt_write<F: fmt::FormatWriter>(f: &mut F, bytes: &[u8]) {
33             let _ = f.write(bytes);
34         }
35         fmt_write(self, bytes);
36         Ok(())
37     }
38 }
39
40 pub fn on_fail(obj: &Any + Send, file: &'static str, line: uint) {
41     let msg = match obj.downcast_ref::<&'static str>() {
42         Some(s) => *s,
43         None => match obj.downcast_ref::<String>() {
44             Some(s) => s.as_slice(),
45             None => "Box<Any>",
46         }
47     };
48     let mut err = Stderr;
49
50     // It is assumed that all reasonable rust code will have a local task at
51     // all times. This means that this `exists` will return true almost all of
52     // the time. There are border cases, however, when the runtime has
53     // *almost* set up the local task, but hasn't quite gotten there yet. In
54     // order to get some better diagnostics, we print on panic and
55     // immediately abort the whole process if there is no local task
56     // available.
57     if !Local::exists(None::<Task>) {
58         let _ = writeln!(&mut err, "panicked at '{}', {}:{}", msg, file, line);
59         if backtrace::log_enabled() {
60             let _ = backtrace::write(&mut err);
61         } else {
62             let _ = writeln!(&mut err, "run with `RUST_BACKTRACE=1` to \
63                                         see a backtrace");
64         }
65         return
66     }
67
68     // Peel the name out of local task so we can print it. We've got to be sure
69     // that the local task is in TLS while we're printing as I/O may occur.
70     let (name, unwinding) = {
71         let mut t = Local::borrow(None::<Task>);
72         (t.name.take(), t.unwinder.unwinding())
73     };
74     {
75         let n = name.as_ref().map(|n| n.as_slice()).unwrap_or("<unnamed>");
76
77         match local_stderr.replace(None) {
78             Some(mut stderr) => {
79                 // FIXME: what to do when the task printing panics?
80                 let _ = writeln!(stderr,
81                                  "task '{}' panicked at '{}', {}:{}\n",
82                                  n, msg, file, line);
83                 if backtrace::log_enabled() {
84                     let _ = backtrace::write(&mut *stderr);
85                 }
86                 local_stderr.replace(Some(stderr));
87             }
88             None => {
89                 let _ = writeln!(&mut err, "task '{}' panicked at '{}', {}:{}",
90                                  n, msg, file, line);
91                 if backtrace::log_enabled() {
92                     let _ = backtrace::write(&mut err);
93                 }
94             }
95         }
96
97         // If this is a double panic, make sure that we printed a backtrace
98         // for this panic.
99         if unwinding && !backtrace::log_enabled() {
100             let _ = backtrace::write(&mut err);
101         }
102     }
103     Local::borrow(None::<Task>).name = name;
104 }