Skip to content
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ jobs:
run: cargo -Zminimal-versions generate-lockfile
- uses: dtolnay/rust-toolchain@1.74.0
- name: Cargo check
run: cargo check --workspace --all-targets
# skip the tests, which require the lazy_cell feature from Rust 1.80
run: cargo check --workspace
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Log panics to the `log` macro as error"
include = ["src", "LICENSE"]
categories = ["development-tools", "development-tools::debugging"] # https://crates.io/category_slugs
keywords = ["log", "panic"]
rust-version = "1.74"

[dependencies]
log = "0.4"
log = "0.4.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ Call this somewhere at the start of your program (after initializing your logger

```rust
use panic_log::Configuration;
[...]
// ...
panic_log::initialize_hook(Configuration::default());
```
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ pub fn initialize_hook(config: Configuration) {
} else {
"<unknown location>".to_owned()
};
let message = info.payload().downcast_ref::<&str>().unwrap_or(&"");
let message = info
.payload()
.downcast_ref::<&str>()
.copied()
.or_else(|| info.payload().downcast_ref::<String>().map(|s| s.as_str()))
.unwrap_or("<message is not a string>");

let backtrace = if config.force_capture {
backtrace::Backtrace::force_capture()
Expand Down
24 changes: 22 additions & 2 deletions tests/test_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,41 @@ use std::{

use panic_log::{initialize_hook, Configuration};

// The test binary runs all tests in parallel by default; this lets multiple tests overwrite the
// panic hook concurrently and cause spurious failure.
static SERIAL_TEST: Mutex<()> = Mutex::new(());

#[test]
#[should_panic]
fn test() {
let _serial = SERIAL_TEST.lock().unwrap();

initialize_hook(Configuration::default());

// Drop the lock to not poison it
drop(_serial);
panic!("Test");
}

#[test]
#[should_panic]
fn test_forced_trace() {
let _serial = SERIAL_TEST.lock().unwrap();

initialize_hook(Configuration {
force_capture: true,
..Default::default()
});

// Drop the lock to not poison it
drop(_serial);
panic!("Test");
}

#[test]
fn test_original_hook() {
let _serial = SERIAL_TEST.lock().unwrap();

let original_hook = panic::take_hook();
let ran_hook = Arc::new(Mutex::new(false));
let ran_hook_copy = Arc::clone(&ran_hook);
Expand All @@ -40,11 +56,13 @@ fn test_original_hook() {
});
let _ = panic::catch_unwind(|| panic!("Test"));

assert_eq!(*ran_hook.lock().unwrap(), true);
assert!(*ran_hook.lock().unwrap());
}

#[test]
fn test_no_original_hook() {
let _serial = SERIAL_TEST.lock().unwrap();

let original_hook = panic::take_hook();
let ran_hook = Arc::new(Mutex::new(false));
let ran_hook_copy = Arc::clone(&ran_hook);
Expand All @@ -60,11 +78,13 @@ fn test_no_original_hook() {
});
let _ = panic::catch_unwind(|| panic!("Test"));

assert_eq!(*ran_hook.lock().unwrap(), false);
assert!(!*ran_hook.lock().unwrap());
}

#[test]
fn test_flush_logger() {
let _serial = SERIAL_TEST.lock().unwrap();

struct Logger {
pub flushed: Arc<Mutex<bool>>,
}
Expand Down