use crate::{format_unix_timestamp, ErrorLog};
#[allow(unused_imports)]
use core::fmt::{Debug, Display};
use log::{debug, error, info, trace, warn, LevelFilter};
#[cfg(feature = "native-dialog")]
use native_dialog::MessageType;
#[cfg(feature = "errors")]
use {crate::DebugDisplay, alloc::boxed::Box};
#[cfg(feature = "anyhow")]
pub type ErrorLogAnyhow<T> = ErrorLog<T, anyhow::Error>;
#[cfg(feature = "anyhow")]
impl<T: Debug> ErrorLog<T, anyhow::Error> {
pub fn new_anyhow() -> Self {
let mut out = Self::new();
out.display_mode(crate::FormatMode::Debug);
out
}
}
#[cfg(feature = "errors")]
pub type ErrorLogBox<T> = ErrorLog<T, Box<dyn DebugDisplay>>;
impl<T, E> ErrorLog<T, E> {
pub fn display_fn_log(&mut self) -> &mut Self {
self.display_fn = |level, unix, e| {
let ts = format_unix_timestamp(unix);
match level {
LevelFilter::Off => (),
LevelFilter::Error => error!("{ts} {e}"),
LevelFilter::Warn => warn!("{ts} {e}"),
LevelFilter::Info => info!("{ts} {e}"),
LevelFilter::Debug => debug!("{ts} {e}"),
LevelFilter::Trace => trace!("{ts} {e}"),
}
};
self
}
pub fn display_fn_println(&mut self) -> &mut Self {
self.display_fn = Self::default().display_fn;
self
}
}
#[cfg(feature = "native-dialog")]
impl<T, E: Debug + Display> ErrorLog<T, E> {
pub fn display_fn_native_dialog(&mut self) -> &mut Self {
self.display_fn = |lvl, unix_ts, e| {
if let Err(dialog_err) = native_dialog::MessageDialog::new()
.set_type(match lvl {
LevelFilter::Off => return,
LevelFilter::Error => MessageType::Error,
LevelFilter::Warn => MessageType::Warning,
_ => MessageType::Info,
})
.set_title(lvl.as_str())
.set_text(&format!("{}: {e}", format_unix_timestamp(unix_ts)))
.show_alert()
{
println!("Failed to show MessageDialog: {}", dialog_err)
}
};
self
}
}