eyre/examples/panic_compose.rs
Luke Frisken 60e426e43e
#66 work in progress make print_panic_info() public (#67)
* add example of desired usage

* reimplement all of kellpossibles pr

* reorganizing to minimize diff

* update changelog for release

* (cargo-release) version 0.5.8

* (cargo-release) start next development iteration 0.5.9-alpha.0

Co-authored-by: Jane Lusby <jlusby@yaah.dev>
2020-11-23 16:33:06 -08:00

51 lines
1.1 KiB
Rust

use color_eyre::eyre::Report;
use tracing::instrument;
#[instrument]
fn main() -> Result<(), Report> {
#[cfg(feature = "capture-spantrace")]
install_tracing();
let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default().into_hooks();
eyre_hook.install()?;
std::panic::set_hook(Box::new(move |pi| {
tracing::error!("{}", panic_hook.panic_report(pi));
}));
read_config();
Ok(())
}
#[cfg(feature = "capture-spantrace")]
fn install_tracing() {
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(ErrorLayer::default())
.init();
}
#[instrument]
fn read_file(path: &str) {
if let Err(e) = std::fs::read_to_string(path) {
panic!("{}", e);
}
}
#[instrument]
fn read_config() {
read_file("fake_file")
}