1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! The `unwind-context` crate makes debugging panics easier
//! by adding a colored panic context with a simple macro.
//! # Introduction
//!
//! In Rust, panics are typically used when an
//! [unrecoverable](https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html)
//! error occurs or when writing examples, prototype code, or tests.
//!
//! However, it can be difficult to pinpoint the exact cause of a panic,
//! especially if it happens deep in the code or within a loop. While adding
//! logs can help, this may lead to a large number of log entries, making it
//! challenging to identify which ones are related to the panic.
//!
//! # About
//!
//! The goal of this crate is to make the panic context addition simple, and the
//! context itself detailed enough, and easy to read. Accordingly, it also makes
//! it easier to add context to assertions in your tests. This crate provides
//! [`unwind_context`] and [`debug_unwind_context`] macros and some other
//! auxiliary types, traits, functions, and macros that help you define function
//! or scope context and write it to [`std::io::stderr`] or another
//! writeable target if panic occurs. If panic occurs, the context will be
//! written in "reverse" chronological order during the unwinding process.
//!
//! This library adds very little overhead to compiled functions unless they are
//! panicked:
//! - First, it constructs a structure containing the context data, code
//! location, writer, and color scheme on the stack. It also stores a
//! reference to the custom panic detector, if specified.
//! - And when this "context scope guard" structure is dropped, its destructor
//! checks for [`std::thread::panicking`] and calls the cold print function if
//! panic has been detected.
//!
//! This crate is intended for diagnostic use. The exact contents and format of
//! the messages printed on panic are not specified, other than being a clear
//! and compact description.
//!
//! Note that the context will only be printed if the
//! [`panic`](https://doc.rust-lang.org/cargo/reference/profiles.html#panic)
//! setting is set to `unwind`, which is the default for both
//! [`dev`](https://doc.rust-lang.org/cargo/reference/profiles.html#dev)
//! and
//! [`release`](https://doc.rust-lang.org/cargo/reference/profiles.html#release)
//! profiles.
//!
//! # Usage
//!
//! First, add the following to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! unwind-context = "0.2.2"
//! ```
//!
//! Then, add the macro call with the given function arguments or scope
//! arguments to the beginning of the functions to be tracked and bind the
//! result to some scope variable (otherwise the unwind context scope guard will
//! be immediately dropped):
//! use unwind_context::unwind_context;
//!
//! fn func1(a: u32, b: &str, c: bool) {
//! let _ctx = unwind_context!(fn(a, b, c));
//! // ...
//! for i in 0..10 {
//! let _ctx = unwind_context!(i);
//! // ...
//! }
//! // ...
//! }
//! With `unwind_context!(a, b, c)` syntax, it will print code location,
//! given argument names (stringified expressions), and values on unwind,
//! whereas with `unwind_context!(fn(a, b, c))` it will also print function
//! names as well. Note that it uses the [`core::fmt::Debug`] representation. If
//! you want to use the [`core::fmt::Display`] representation, you can use the
//! [`WithDisplay`] wrapper.
//!
//! You can use the [`set_colors_enabled`] function to unconditionally enable
//! the 16-ANSI-color colorization. If you want to enable colorization only if
//! supported by the terminal, you can use the [`enable_colors_if_supported`]
//! function, which will require enabling the
//! [`detect-color-support`](#feature-flags) feature flag:
//! ```toml
//! [dependencies.unwind-context]
//! version = "0.2.2"
//! features = [ "detect-color-support" ]
//! ```
//! # /*
//! fn main() {
//! # */
//! unwind_context::enable_colors_if_supported();
//! # test();
//! // ...
//! # /*
//! }
//!
//! # */
//! # /*
//! #[test]
//! # */
//! fn test() {
//! unwind_context::enable_colors_if_supported()
//! // ...
//! }
//! If you want to specify a custom color scheme, you can use the
//! [`set_default_color_scheme`] function.
//! Also, colorization can be customized separately for each context scope guard
//! with the [`unwind_context_with_io`] and [`unwind_context_with_fmt`] macros.
//!
//! This crate depends on the standard library by default that is needed to
//! write to [`std::io::stderr`] and to detect panicking using
//! [`std::thread::panicking`]. To use this crate in a `#![no_std]` context with
//! your custom [`core::fmt::Write`] writer and custom [`PanicDetector`], use
//! `default-features = false` in your `Cargo.toml` as shown below:
//! ```toml
//! [dependencies.unwind-context]
//! version = "0.2.2"
//! default-features = false
//! ```
//!
//! # Examples
//!
//! The following crate example:
//! will output:
//! # Macro expansion
//!
//! The following function:
//! use unwind_context::unwind_context;
//!
//! fn foo(a: &str, b: Vec<u8>, c: bool, d: String) {
//! let _ctx = unwind_context!(fn(a, &b, ..., d.clone()));
//! // ...
//! for i in 0..10 {
//! let _ctx = unwind_context!(i);
//! // ...
//! }
//! }
//! will partially expand into:
//! fn foo(a: u32, b: Vec<u8>, c: bool, d: String) {
//! let _ctx = unwind_context::UnwindContextWithIo::new(
//! unwind_context::UnwindContextFunc::new(
//! {
//! struct Item;
//! let module_path = ::core::module_path!();
//! let item_type_name = ::core::any::type_name::<Item>();
//! unwind_context::func_name_from_item_type_name(
//! module_path, item_type_name
//! )
//! },
//! (
//! unwind_context::UnwindContextArg::new(Some("a"), a),
//! (
//! unwind_context::UnwindContextArg::new(Some("&b"), &b),
//! (
//! unwind_context::UnwindContextArg::new(
//! None,
//! unwind_context::NonExhaustiveMarker,
//! ),
//! (
//! unwind_context::UnwindContextArg::new(
//! Some("d.clone()"), d.clone()
//! ),
//! (),
//! ),
//! ),
//! ),
//! ),
//! ),
//! ::std::io::stderr(),
//! unwind_context::StdPanicDetector,
//! unwind_context::get_default_color_scheme_if_enabled(),
//! );
//! // ...
//! for i in 0..10 {
//! let _ctx = unwind_context::UnwindContextWithIo::new(
//! unwind_context::UnwindContextArgs::new((
//! unwind_context::UnwindContextArg::new(Some("i"), i),
//! (),
//! )),
//! ::std::io::stderr(),
//! unwind_context::StdPanicDetector,
//! unwind_context::get_default_color_scheme_if_enabled(),
//! );
//! // ...
//! }
//! }
//! # Feature Flags
//!
//! - `std` (enabled by default): Enables [`UnwindContextWithIo`] structure,
//! [`unwind_context`], [`debug_unwind_context`], [`unwind_context_with_io`],
//! and [`debug_unwind_context_with_io`] macros.
//! - `detect-color-support`: Enables [`enable_colors_if_supported`] function
//! and [`supports-color`] optional dependency.
//! - `custom-default-colors`: Enables [`set_default_color_scheme`] function and
//! [`atomic_ref`] optional dependency.
//!
//! # Similar crates
//!
//! - [`scopeguard`] allows you to run any code at the end of a scope. It has
//! both success and unwind guard variants, and it doesn't require panic hook
//! modification.
//! - [`panic-context`] allows you to specify and modify panic context using a
//! custom panic hook. It provides more fine-grained control over the output.
//! However, it implicitly modifies the panic hook using a mutex for a
//! one-time thread local initialization and doesn’t add any automatic context
//! or colorization.
//! - [`econtext`] allows you to specify panic context and automatically adds
//! some context including function name and location. However, it requires
//! panic hook modification via the init function and uses dynamic dispatch
//! and some unsafe code.
//!
//! # License
//!
//! Licensed under either of
//!
//! - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
//! - MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)
//!
//! at your option.
//!
//! # Contribution
//!
//! Unless you explicitly state otherwise, any contribution intentionally
//! submitted for inclusion in the work by you, as defined in the Apache-2.0
//! license, shall be dual licensed as above, without any
//! additional terms or conditions.
//!
//! [`supports-color`]: https://crates.io/crates/supports-color
//! [`atomic_ref`]: https://crates.io/crates/atomic_ref
//! [`scopeguard`]: https://crates.io/crates/scopeguard
//! [`panic-context`]: https://crates.io/crates/panic-context
//! [`econtext`]: https://crates.io/crates/econtext
extern crate std;
use version_sync as _; // Used in integration tests.
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;