error_log/
presets.rs

1use crate::{format_unix_timestamp, ErrorLog};
2#[allow(unused_imports)]
3use core::fmt::{Debug, Display};
4use log::{debug, error, info, trace, warn, LevelFilter};
5#[cfg(feature = "native-dialog")]
6use native_dialog::MessageType;
7#[cfg(feature = "errors")]
8use {crate::DebugDisplay, alloc::boxed::Box};
9
10#[cfg(feature = "anyhow")]
11/**
12Pre-defined [`ErrorLog`] Using [`anyhow::Error`] as `E`
13
14Its suggested to use [`new_anyhow()`][crate::ErrorLog::new_anyhow] to load optimized settings.
15*/
16pub type ErrorLogAnyhow<T> = ErrorLog<T, anyhow::Error>;
17
18#[cfg(feature = "anyhow")]
19/// Special methods for [`ErrorLogAnyhow`][crate::ErrorLogAnyhow]
20impl<T: Debug> ErrorLog<T, anyhow::Error> {
21    /// Creates a new [`ErrorLog`][crate::ErrorLog] and sets the [`FormatMode`][crate::FormatMode] to Debug. Indented for best [`anyhow`] compatibility
22    pub fn new_anyhow() -> Self {
23        let mut out = Self::new();
24        out.display_mode(crate::FormatMode::Debug);
25        out
26    }
27}
28
29#[cfg(feature = "errors")]
30/**
31Pre-defined [`ErrorLog`] using `Box<dyn DebugDisplay>` as `E`
32
33Unlocks additional functions:
34- [`merge_result_box()`][Self::merge_result_box]
35- [`push_result_box()`][Self::push_result_box]
36- [`push_err_box()`][Self::push_err_box]
37*/
38pub type ErrorLogBox<T> = ErrorLog<T, Box<dyn DebugDisplay>>;
39
40impl<T, E> ErrorLog<T, E> {
41    /// Display entries using [`log`] macros
42    pub fn display_fn_log(&mut self) -> &mut Self {
43        self.display_fn = |level, unix, e| {
44            let ts = format_unix_timestamp(unix);
45            match level {
46                LevelFilter::Off => (),
47                LevelFilter::Error => error!("{ts} {e}"),
48                LevelFilter::Warn => warn!("{ts} {e}"),
49                LevelFilter::Info => info!("{ts} {e}"),
50                LevelFilter::Debug => debug!("{ts} {e}"),
51                LevelFilter::Trace => trace!("{ts} {e}"),
52            }
53        };
54        self
55    }
56    /// Display errors using [`println`]
57    pub fn display_fn_println(&mut self) -> &mut Self {
58        self.display_fn = Self::default().display_fn;
59        self
60    }
61}
62
63#[cfg(feature = "native-dialog")]
64impl<T, E: Debug + Display> ErrorLog<T, E> {
65    /// Display [`crate::Entries`] using [`native_dialog::MessageDialog`]
66    pub fn display_fn_native_dialog(&mut self) -> &mut Self {
67        self.display_fn = |lvl, unix_ts, e| {
68            if let Err(dialog_err) = native_dialog::MessageDialog::new()
69                .set_type(match lvl {
70                    LevelFilter::Off => return,
71                    LevelFilter::Error => MessageType::Error,
72                    LevelFilter::Warn => MessageType::Warning,
73                    _ => MessageType::Info,
74                })
75                .set_title(lvl.as_str())
76                .set_text(&format!("{}: {e}", format_unix_timestamp(unix_ts)))
77                .show_alert()
78            {
79                println!("Failed to show MessageDialog: {}", dialog_err)
80            }
81        };
82        self
83    }
84}