···1+/// Sets the panic-hook to be customized color-eyre `panic_hook`.
2+///
3+/// Additionally the panic handler prints different information
4+/// based on debug / release builds.
5+pub fn init() -> color_eyre::Result<()> {
6+ let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default()
7+ .panic_section(format!(
8+ "This is a bug. Please report it at {}",
9+ env!("CARGO_PKG_REPOSITORY")
10+ ))
11+ .capture_span_trace_by_default(false)
12+ .display_location_section(false)
13+ .display_env_section(false)
14+ .into_hooks();
15+16+ eyre_hook.install()?;
17+ std::panic::set_hook(Box::new(move |panic_info| {
18+ //TODO: exit from terminal if the app is in a terminal
19+20+ // in release mode, do human_panic printing
21+ #[cfg(not(debug_assertions))]
22+ {
23+ use human_panic::{handle_dump, metadata, print_msg};
24+ let metadata = metadata!();
25+ let file_path = handle_dump(&metadata, panic_info);
26+ print_msg(file_path, &metadata)
27+ .expect("human-panic: printing error message to console failed");
28+ eprintln!("{}", panic_hook.panic_report(panic_info));
29+ }
30+31+ // in debug mode do better panic printing
32+ #[cfg(debug_assertions)]
33+ {
34+ better_panic::Settings::auto()
35+ .most_recent_first(false)
36+ .lineno_suffix(true)
37+ .verbosity(better_panic::Verbosity::Full)
38+ .create_panic_handler()(panic_info);
39+ }
40+41+ // 1 = failure
42+ std::process::exit(1)
43+ }));
44+45+ Ok(())
46+}
47+48+/// Akin to `dbg!` macro, except that it generates `tracing::Event`s instead
49+/// of printing to standard error.
50+///
51+/// Can customize level by providing a `tracing::Level`, but its debug by default.
52+#[macro_export]
53+macro_rules! trace_dbg {
54+ (target: $target:expr, level: $level:expr, $ex:expr) => {
55+ {
56+ match $ex {
57+ value => {
58+ tracing::event!(target: $target, $level, ?value, stringify!($ex));
59+ value
60+ }
61+ }
62+ }
63+ };
64+ (level: $level:expr, $ex:expr) => {
65+ trace_dbg!(target: module_path!(), level: $level, $ex)
66+ };
67+68+ (target: $target:expr, $ex:expr) => {
69+ trace_dbg!(target: $target, level: tracing::Level::DEBUG, $ex)
70+ };
71+ ($ex:expr) => {
72+ trace_dbg!(level: tracing::Level::DEBUG, $ex)
73+ };
74+}
+6-2
src/main.rs
···2//! My (suri.codes) personal-knowledge-system, with deeply integrated task tracking and long term goal planning capabilities.
3//!
45-fn main() {
6- println!("Hello, world!");
00007}
···2//! My (suri.codes) personal-knowledge-system, with deeply integrated task tracking and long term goal planning capabilities.
3//!
45+mod errors;
6+7+fn main() -> color_eyre::Result<()> {
8+ errors::init()?;
9+10+ Ok(())
11}