#[cfg(feature = "message-box")]
use crate::alert_service::AlertService;
#[cfg(feature = "message-box")]
use crate::settings::ARGS;
#[cfg(feature = "message-box")]
use crate::settings::RUNS_ON_CONSOLE;
use lazy_static::lazy_static;
use log::LevelFilter;
use log::{Level, Metadata, Record};
use std::sync::atomic::{AtomicBool, Ordering};
pub struct AppLogger {
popup_always_enabled: AtomicBool,
}
lazy_static! {
static ref APP_LOGGER: AppLogger = AppLogger {
popup_always_enabled: AtomicBool::new(false)
};
}
impl AppLogger {
#[inline]
pub fn init() {
#[cfg(feature = "message-box")]
if !*RUNS_ON_CONSOLE && !ARGS.batch {
AlertService::init();
};
log::set_logger(&*APP_LOGGER).unwrap();
log::set_max_level(LevelFilter::Error);
}
#[allow(dead_code)]
pub fn set_max_level(level: LevelFilter) {
log::set_max_level(level);
}
#[allow(dead_code)]
pub fn set_popup_always_enabled(popup: bool) {
APP_LOGGER
.popup_always_enabled
.store(popup, Ordering::SeqCst);
}
pub fn flush() {
#[cfg(feature = "message-box")]
if !*RUNS_ON_CONSOLE && !ARGS.batch {
AlertService::flush();
}
}
}
impl log::Log for AppLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
eprintln!("*** {}: {}", record.level(), record.args());
#[cfg(feature = "message-box")]
if !*RUNS_ON_CONSOLE
&& !ARGS.batch
&& ((record.metadata().level() == LevelFilter::Error)
|| APP_LOGGER.popup_always_enabled.load(Ordering::SeqCst))
{
let msg = format!("{}:\n{}", record.level(), &record.args().to_string());
let _res = if record.metadata().level() == Level::Error {
AlertService::push_debug_str(msg)
} else {
AlertService::push_str(msg)
};
};
}
}
fn flush(&self) {}
}