Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A crate which logs panics instead of writing to standard error.
2 //!
3 //! The format used is identical to the standard library's.
4 //!
5 //! Because logging with a backtrace requires additional dependencies,
6 //! the `with-backtrace` feature must be enabled. You can add the
7 //! following in your `Cargo.toml`:
8 //!
9 //! ```toml
10 //! log-panics = { version = "2", features = ["with-backtrace"]}
11 //! ```
12 //!
13 //! To use, call [`log_panics::init()`](init) somewhere early in execution,
14 //! such as immediately after initializing `log`, or use the [`Config`]
15 //! builder for more customization.
16
17 #![doc(html_root_url = "https://docs.rs/log-panics/2.0.0")]
18 #![warn(missing_docs)]
19
20 // Enable feature requirements on docs.rs.
21 #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
22
23 #[macro_use]
24 extern crate logrust-analyzer cargo log-panics 2.1.0 log/;
25
26 #[cfg(feature = "with-backtrace")]
27 extern crate backtracerust-analyzer cargo log-panics 2.1.0 backtrace/;
28
29 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/, threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/};
30
31 use backtracerust-analyzer cargo backtrace 0.3.66 crate/::Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#;
32
33 #[cfg(not(feature = "with-backtrace"))]
34 mod backtrace {
35 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Default)]
36 pub struct Backtrace;
37 }
38
39 struct Shimrust-analyzer cargo log-panics 2.1.0 Shim#(Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#);
40
41 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Shimrust-analyzer cargo log-panics 2.1.0 Shim# {
42 #[cfg(feature = "with-backtrace")]
43 fn fmtrust-analyzer cargo log-panics 2.1.0 impl#[Shim][Debug]fmt().(&self, fmt: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
44 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.0rust-analyzer cargo log-panics 2.1.0 Shim#0..framesrust-analyzer cargo backtrace 0.3.66 capture/impl#[Backtrace]frames().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
45 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(fmt, "\n{:?}", self.0rust-analyzer cargo log-panics 2.1.0 Shim#0.)
46 } else {
47 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
48 }
49 }
50
51 #[inline]
52 #[cfg(not(feature = "with-backtrace"))]
53 fn fmt(&self, _: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
55 }
56 }
57
58 /// Determines how backtraces will be displayed.
59 #[cfg(feature = "with-backtrace")]
60 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
61 pub enum BacktraceModerust-analyzer cargo log-panics 2.1.0 BacktraceMode# {
62 /// Backtraces will be omitted from the log.
63 Offrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Off#,
64 /// Backtraces will include addresses, but no symbol names or locations.
65 Unresolvedrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Unresolved#,
66 /// Backtraces will include addresses as well as symbol names and locations when possible.
67 Resolvedrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Resolved#
68 }
69
70 /// Configures the panic hook, ending with initialization.
71 ///
72 /// ## Example
73 ///
74 /// ```
75 /// # #[cfg(feature = "with-backtrace")]
76 /// log_panics::Config::new()
77 /// .backtrace_mode(log_panics::BacktraceMode::Unresolved)
78 /// .install_panic_hook()
79 /// ```
80 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
81 pub struct Configrust-analyzer cargo log-panics 2.1.0 Config# {
82 // We store a constructor function instead of a BacktraceMode enum
83 // so that inlining can eliminate references to `Backtrace::default`
84 // if symbolication is not desired.
85 make_backtracerust-analyzer cargo log-panics 2.1.0 Config#make_backtrace.: fn() -> Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#,
86 }
87
88 impl Configrust-analyzer cargo log-panics 2.1.0 Config# {
89 /// Initializes the builder with the default set of features.
90 pub fn newrust-analyzer cargo log-panics 2.1.0 impl#[Config]new().() -> Selfrust-analyzer cargo log-panics 2.1.0 impl#[Config] {
91 Selfrust-analyzer cargo log-panics 2.1.0 impl#[Config] {
92 make_backtracerust-analyzer cargo log-panics 2.1.0 Config#make_backtrace.: Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#::defaultrust-analyzer cargo backtrace 0.3.66 capture/impl#[Backtrace][Default]default().,
93 }
94 }
95
96 /// Controls how backtraces are displayed.
97 ///
98 /// The default when backtraces are enabled is [`BacktraceMode::Resolved`].
99 #[cfg(feature = "with-backtrace")]
100 pub fn backtrace_moderust-analyzer cargo log-panics 2.1.0 impl#[Config]backtrace_mode().(mut self, mode: BacktraceModerust-analyzer cargo log-panics 2.1.0 BacktraceMode#) -> Selfrust-analyzer cargo log-panics 2.1.0 impl#[Config] {
101 self.make_backtracerust-analyzer cargo log-panics 2.1.0 Config#make_backtrace. = match mode {
102 BacktraceModerust-analyzer cargo log-panics 2.1.0 BacktraceMode#::Offrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Off# => || Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#::fromrust-analyzer cargo backtrace 0.3.66 capture/impl#[Backtrace][`From<Vec<BacktraceFrame>>`]from().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]),
103 BacktraceModerust-analyzer cargo log-panics 2.1.0 BacktraceMode#::Unresolvedrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Unresolved# => Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#::new_unresolvedrust-analyzer cargo backtrace 0.3.66 capture/impl#[Backtrace]new_unresolved().,
104 BacktraceModerust-analyzer cargo log-panics 2.1.0 BacktraceMode#::Resolvedrust-analyzer cargo log-panics 2.1.0 BacktraceMode#Resolved# => Backtracerust-analyzer cargo backtrace 0.3.66 capture/Backtrace#::defaultrust-analyzer cargo backtrace 0.3.66 capture/impl#[Backtrace][Default]default().,
105 };
106 self
107 }
108
109 /// Initializes the panic hook.
110 ///
111 /// After this method is called, all panics will be logged rather than printed
112 /// to standard error.
113 pub fn install_panic_hookrust-analyzer cargo log-panics 2.1.0 impl#[Config]install_panic_hook().(self) {
114 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::set_hookrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panicking/set_hook().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(move |info| {
115 let backtrace = (self.make_backtracerust-analyzer cargo log-panics 2.1.0 Config#make_backtrace.)();
116
117 let thread = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().();
118 let thread = thread.namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]name().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("<unnamed>");
119
120 let msg = match info.payloadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]payload().().downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<&'static str>() {
121 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s,
122 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => match info.payloadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]payload().().downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() {
123 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) => &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s,
124 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => "Box<Any>",
125 },
126 };
127
128 match info.locationrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/impl#[`PanicHookInfo<'a>`]location().() {
129 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(location) => {
130 errorrust-analyzer cargo log 0.4.34 macros/error!!(
131 target: "panic", "thread '{}' panicked at '{}': {}:{}{:?}",
132 thread,
133 msg,
134 location.filerust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/location/impl#[`Location<'a>`]file().(),
135 location.linerust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/location/impl#[`Location<'a>`]line().(),
136 Shimrust-analyzer cargo log-panics 2.1.0 Shim#(backtrace)
137 );
138 }
139 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => errorrust-analyzer cargo log 0.4.34 macros/error!!(
140 target: "panic",
141 "thread '{}' panicked at '{}'{:?}",
142 thread,
143 msg,
144 Shimrust-analyzer cargo log-panics 2.1.0 Shim#(backtrace)
145 ),
146 }
147 }));
148 }
149 }
150
151 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Configrust-analyzer cargo log-panics 2.1.0 Config# {
152 fn defaultrust-analyzer cargo log-panics 2.1.0 impl#[Config][Default]default().() -> Selfrust-analyzer cargo log-panics 2.1.0 impl#[Config][Default] {
153 Selfrust-analyzer cargo log-panics 2.1.0 impl#[Config][Default]::newrust-analyzer cargo log-panics 2.1.0 impl#[Config]new().()
154 }
155 }
156
157 /// Initializes the panic hook with the default settings.
158 ///
159 /// After this method is called, all panics will be logged rather than printed
160 /// to standard error.
161 ///
162 /// See [`Config`] for more information.
163 pub fn initrust-analyzer cargo log-panics 2.1.0 init().() {
164 Configrust-analyzer cargo log-panics 2.1.0 Config#::newrust-analyzer cargo log-panics 2.1.0 impl#[Config]new().().install_panic_hookrust-analyzer cargo log-panics 2.1.0 impl#[Config]install_panic_hook().()
165 }