human_panic/lib.rs
1//! Panic messages for humans
2//!
3//! Reduce the effort for users to report panics back to you
4//!
5//! You can still get the raw output by either:
6//! - Running a debug build
7//! - Setting `RUST_BACKTRACE=1`
8//!
9//! ## Example
10//!
11//! Add to your `fn main()`:
12//! ```rust
13//! human_panic::setup_panic!();
14//! ```
15//!
16//! When run locally in a release build:
17//! ```txt
18//! my-program had a problem and crashed. To help us diagnose the problem you can send us a crash report.
19//!
20//! We have generated a report file at "/var/folders/zw/bpfvmq390lv2c6gn_6byyv0w0000gn/T/report-8351cad6-d2b5-4fe8-accd-1fcbf4538792.toml". Submit an issue or email with the subject of "human-panic Crash Report" and include the report as an attachment.
21//!
22//! - Homepage: https://github.com/rust-ci/human-panic
23//! - Authors: Yoshua Wuyts <yoshuawuyts@gmail.com>
24//!
25//! We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.
26//!
27//! Thank you kindly!
28//! ```
29//!
30//! Traditional output:
31//! ```txt
32//! thread 'main' panicked at 'oops', examples/main.rs:2:3
33//! note: Run with `RUST_BACKTRACE=1` for a backtrace.
34//! ```
35
36#![cfg_attr(docsrs, feature(doc_cfg))]
37#![warn(clippy::print_stderr)]
38#![warn(clippy::print_stdout)]
39
40#[doc = include_str!("../README.md")]
41#[cfg(doctest)]
42pub struct ReadmeDoctests;
43
44mod metadata;
45mod panic;
46
47pub mod report;
48pub use metadata::Metadata;
49pub use panic::PanicStyle;
50pub use panic::handle_dump;
51pub use panic::print_msg;
52pub use panic::setup_panic;
53
54/// Collect Cargo [`Metadata`]
55///
56/// ## Example
57///
58/// ```rust
59/// use human_panic::metadata;
60///
61/// let metadata = metadata!()
62/// .support("- Open a support request by email to support@mycompany.com");
63/// ```
64#[macro_export]
65macro_rules! metadata {
66 () => {{
67 $crate::Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
68 .authors(env!("CARGO_PKG_AUTHORS").replace(":", ", "))
69 .homepage(env!("CARGO_PKG_HOMEPAGE"))
70 .repository(env!("CARGO_PKG_REPOSITORY"))
71 }};
72}
73
74/// Register `human-panic`
75///
76/// The macro should be called from within a function, for example as the first line of the
77/// `main()` function of the program.
78///
79/// ## Example
80///
81/// Default [`metadata!`]
82/// ```rust
83/// use human_panic::setup_panic;
84///
85/// setup_panic!();
86/// ```
87///
88/// Extend or override default [`metadata!`]
89/// ```rust
90/// use human_panic::setup_panic;
91/// use human_panic::metadata;
92///
93/// setup_panic!(metadata!()
94/// .authors("My Company Support <support@mycompany.com>")
95/// .support("- Open a support request by email to support@mycompany.com")
96/// );
97/// ```
98#[macro_export]
99macro_rules! setup_panic {
100 ($meta:expr) => {{
101 $crate::setup_panic(|| $meta);
102 }};
103
104 () => {
105 $crate::setup_panic!($crate::metadata!());
106 };
107}