1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::sync::RwLock;

lazy_static! {
    static ref LOGGER: RwLock<Option<Box<dyn Logger>>> = RwLock::new(None);
}

#[derive(Debug, Copy, Clone)]
pub enum Log {
    Info,
    Warning,
    Error,
    Debug,
    #[cfg(feature = "profiler")]
    ProfileStart,
    #[cfg(feature = "profiler")]
    ProfileEnd,
}

pub trait Logger: Send + Sync {
    fn log(&mut self, mode: Log, message: String);
}

pub struct DefaultLogger;

impl Logger for DefaultLogger {
    fn log(&mut self, mode: Log, message: String) {
        match mode {
            Log::Info => println!("[INFO] {}", message),
            Log::Warning => eprintln!("[WARNING] {}", message),
            Log::Error => eprintln!("[ERROR] {}", message),
            Log::Debug => eprintln!("[DEBUG] {}", message),
            #[cfg(feature = "profiler")]
            Log::ProfileStart => {}
            #[cfg(feature = "profiler")]
            Log::ProfileEnd => {}
        }
    }
}

pub fn logger_setup<L>(instance: L)
where
    L: Logger + 'static,
{
    if let Ok(mut logger) = LOGGER.write() {
        *logger = Some(Box::new(instance));
    }
}

pub fn logger_log(mode: Log, message: String) {
    if let Ok(mut logger) = LOGGER.write() {
        if let Some(ref mut logger) = *logger {
            logger.log(mode, message);
        }
    }
}

#[macro_export]
macro_rules! log {
    ($lvl:expr, $($arg:tt)*) => ({
        $crate::log::logger_log($lvl, format!(
            "[{}: {} | {}]:\n{}",
            file!(),
            line!(),
            module_path!(),
            format_args!($($arg)*)
        ));
    })
}

#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => ({
        $crate::log::logger_log($crate::log::Log::Info, format!(
            "[{}: {} | {}]:\n{}",
            file!(),
            line!(),
            module_path!(),
            format_args!($($arg)*)
        ));
    })
}

#[macro_export]
macro_rules! warn {
    ($($arg:tt)*) => ({
        $crate::log::logger_log($crate::log::Log::Warning, format!(
            "[{}: {} | {}]:\n{}",
            file!(),
            line!(),
            module_path!(),
            format_args!($($arg)*)
        ));
    })
}

#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => ({
        $crate::log::logger_log($crate::log::Log::Error, format!(
            "[{}: {} | {}]:\n{}",
            file!(),
            line!(),
            module_path!(),
            format_args!($($arg)*)
        ));
    })
}

#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => ({
        $crate::log::logger_log($crate::log::Log::Debug, format!(
            "[{}: {} | {}]:\n{}",
            file!(),
            line!(),
            module_path!(),
            format_args!($($arg)*)
        ));
    })
}

#[cfg(feature = "profiler")]
#[macro_export]
macro_rules! profile_scope {
    ($id:literal, $code:block) => {{
        $crate::log::logger_log($crate::log::Log::ProfileStart, $id.to_string());
        let result = { $code };
        $crate::log::logger_log($crate::log::Log::ProfileEnd, $id.to_string());
        result
    }};
}

#[cfg(not(feature = "profiler"))]
#[macro_export]
macro_rules! profile_scope {
    ($id:literal, $code:block) => {{
        $code
    }};
}