Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose machinery to customize setup_panic! #88

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixes

- Use normal panics when `RUST_BACKTRACE` is enabled
- Skip unnecesary frames in backtrace
- Skip unnecessary frames in backtrace

## 2018-04-18, Version 0.3.0

Expand Down
68 changes: 42 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ use std::panic::PanicInfo;
use std::path::{Path, PathBuf};

/// A convenient metadata struct that describes a crate
///
/// See [`metadata!`]
pub struct Metadata {
/// The crate version
pub version: Cow<'static, str>,
Expand All @@ -63,6 +65,19 @@ pub struct Metadata {
pub homepage: Cow<'static, str>,
}

/// Initialize [`Metadata`]
#[macro_export]
macro_rules! metadata {
() => {
Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
}
};
}

/// `human-panic` initialisation macro
///
/// You can either call this macro with no arguments `setup_panic!()` or
Expand Down Expand Up @@ -90,44 +105,45 @@ macro_rules! setup_panic {
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};

#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
match $crate::PanicStyle::default() {
$crate::PanicStyle::Debug => {}
$crate::PanicStyle::Human => {
let meta = $meta;

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);
print_msg(file_path, &$meta)
let file_path = handle_dump(&meta, info);
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
}
Ok(_) => {}
}
};

() => {
#[allow(unused_imports)]
use std::panic::{self, PanicInfo};
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};
$crate::setup_panic!($crate::metadata!());
};
}

#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
let meta = Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};
/// Style of panic to be used
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum PanicStyle {
/// Normal panic
Debug,
/// Human-formatted panic
Human,
}

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
impl Default for PanicStyle {
fn default() -> Self {
if cfg!(debug_assertions) {
PanicStyle::Debug
} else {
match ::std::env::var("RUST_BACKTRACE") {
Ok(_) => PanicStyle::Debug,
Err(_) => PanicStyle::Human,
}
Ok(_) => {}
}
};
}
}

/// Utility function that prints a message to our human users
Expand Down