kapiti 0.0.3

The Kapiti DNS Server
Documentation
use std::panic;
use std::process;

use backtrace::Backtrace;
use tracing::error;

/// Enables a global panic hook which will log the panic/backtrace before exiting the process.
/// This replaces the default behavior where only the panicing thread is exited,
/// which would leave things running in a degraded state in the context of multiple worker threads.
pub fn init_hook() {
    panic::set_hook(Box::new(&handle_panic));
}

/// Prints information about the panic and then exits the process.
/// Panic output looks like this:
///  - empty line -
///  panic message+file/line
///  - empty line -
///  stacktrace
///  - empty line -
///  timestamped error with panicing thread's tracing context, and panic message+file/line again
fn handle_panic(panic_info: &panic::PanicInfo) {
    // Prints the panic message and the file/line where the panic!() statement was located.
    println!("\n{}", panic_info);
    // Prints a dump of the "short" backtrace, surrounded by whitespace lines.
    // - The "long" backtrace has the same items, but with additional hex codes sprinkled in.
    // - Missing items in release builds are not affected by "short" vs "long" backtraces.
    // - Enabling 'debug = true' in release builds increases executable size from ~17MB to ~123MB.
    println!("\n{:?}", Backtrace::new());

    // Include another copy of the panic on the last line before exiting
    // Also includes the thread label/tracing context
    error!("Exiting process following thread panic: {}", panic_info);
    process::exit(1);
}