use std::fmt;
use std::sync::Arc;
use crate::modules::module_abstract::ModuleAbstract;
use crate::components::trace::OneTrace;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Level
{
DEBUG,
DEBUGERR,
NORMAL,
NOTICE,
NOTICEDERR,
WARNING,
ERROR,
FATAL,
}
impl Level
{
pub fn to_string(&self) -> String
{
match *self
{
Level::DEBUG => "DEBUG".to_string(),
Level::DEBUGERR => "DEBUGERR".to_string(),
Level::ERROR => "ERROR".to_string(),
Level::FATAL => "FATAL".to_string(),
Level::NOTICE => "NOTICE".to_string(),
Level::NOTICEDERR => "NOTICEDERR".to_string(),
Level::WARNING => "WARNING".to_string(),
Level::NORMAL => "NORMAL".to_string()
}
}
pub fn convert4LengthString(&self) -> String
{
match *self
{
Level::DEBUG => "DBUG".to_string(),
Level::DEBUGERR => "ERRD".to_string(),
Level::ERROR => "ERR ".to_string(),
Level::FATAL => "FATA".to_string(),
Level::NOTICE => "NOTI".to_string(),
Level::NOTICEDERR => "NOER".to_string(),
Level::WARNING => "WARN".to_string(),
Level::NORMAL => " ".to_string()
}
}
pub fn tou8(&self) -> u8
{
match *self
{
Level::DEBUG => 0,
Level::DEBUGERR => 1,
Level::NORMAL => 2,
Level::NOTICE => 3,
Level::NOTICEDERR => 4,
Level::WARNING => 5,
Level::ERROR => 6,
Level::FATAL => 7,
}
}
pub fn launchModuleFunc(module: &Arc<dyn ModuleAbstract>, oneTrace: &OneTrace)
{
match oneTrace.level
{
Level::DEBUG => module.event_onDebug(oneTrace),
Level::DEBUGERR => module.event_onDebugErr(oneTrace),
Level::ERROR => module.event_onError(oneTrace),
Level::FATAL => module.event_onFatal(oneTrace),
Level::NOTICE => module.event_onNotice(oneTrace),
Level::NOTICEDERR => module.event_onNoticeErr(oneTrace),
Level::WARNING => module.event_onWarning(oneTrace),
Level::NORMAL => module.event_onNormal(oneTrace)
}
}
pub fn min() -> Level
{
Level::DEBUG
}
pub fn max() -> Level
{
Level::FATAL
}
}
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:<4}", self.convert4LengthString())
}
}
impl From<u8> for Level
{
fn from(value: u8) -> Self {
match value
{
0 => Level::DEBUG,
1 => Level::DEBUGERR,
2 => Level::NORMAL,
3 => Level::NOTICE,
4 => Level::NOTICEDERR,
5 => Level::WARNING,
6 => Level::ERROR,
_ => Level::FATAL
}
}
}