Skip to content

Commit

Permalink
feat(tracing): use hook to log errors on panics
Browse files Browse the repository at this point in the history
The two bugs raised in issues #1 and #2 were due to panics that were not
visible in the logs, which left the process hanging and unresponsive,
ultimately needing to be force killed with a command like 'Stop-Process
-Name komorebi'.

The only way to even verify that a panic had taken place and what the
panic related to, was to run '$env:RUST_BACKTRACE ='full';
komorebi.exe', wait for the panic, then restore the now-hidden window
with 'komorebic restore-windows' to finally see the panic message.

This commit integrates an example from the 'tracing' repo,  which
through the addition of a panic hook, logs out panics as errors.
Hopefully this will debugging much easier in the future.

re #1, re #2
  • Loading branch information
LGUG2Z committed Aug 15, 2021
1 parent a53b2cc commit a59bbac
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions komorebi/src/main.rs
Expand Up @@ -82,6 +82,31 @@ fn setup() -> Result<WorkerGuard> {
),
)?;

// https://github.com/tokio-rs/tracing/blob/master/examples/examples/panic_hook.rs
// Set a panic hook that records the panic as a `tracing` event at the
// `ERROR` verbosity level.
//
// If we are currently in a span when the panic occurred, the logged event
// will include the current span, allowing the context in which the panic
// occurred to be recorded.
std::panic::set_hook(Box::new(|panic| {
// If the panic has a source location, record it as structured fields.
if let Some(location) = panic.location() {
// On nightly Rust, where the `PanicInfo` type also exposes a
// `message()` method returning just the message, we could record
// just the message instead of the entire `fmt::Display`
// implementation, avoiding the duplciated location
tracing::error!(
message = %panic,
panic.file = location.file(),
panic.line = location.line(),
panic.column = location.column(),
);
} else {
tracing::error!(message = %panic);
}
}));

Ok(guard)
}

Expand Down

0 comments on commit a59bbac

Please sign in to comment.