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")]
11pub type ErrorLogAnyhow<T> = ErrorLog<T, anyhow::Error>;
17
18#[cfg(feature = "anyhow")]
19impl<T: Debug> ErrorLog<T, anyhow::Error> {
21 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")]
30pub type ErrorLogBox<T> = ErrorLog<T, Box<dyn DebugDisplay>>;
39
40impl<T, E> ErrorLog<T, E> {
41 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 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 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}