logid_core/
log_id.rs

1//! Contains the [`LogId`] struct.
2
3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct LogId {
5    pub(crate) module_path: &'static str,
6
7    pub(crate) identifier: &'static str,
8
9    pub(crate) log_level: LogLevel,
10}
11
12impl evident::event::Id for LogId {}
13
14impl evident::publisher::CaptureControl for LogId {
15    fn start(id: &Self) -> bool {
16        id == &START_LOGGING
17    }
18
19    fn start_id() -> Self {
20        START_LOGGING
21    }
22
23    fn stop(id: &Self) -> bool {
24        id == &STOP_LOGGING
25    }
26
27    fn stop_id() -> Self {
28        STOP_LOGGING
29    }
30}
31
32/// Notify LOGGER and listeners to start logging.
33///
34/// **Note:** Filter does not affect capturing of this LogId. It is up to the handler to decide wether to filter it or not.
35pub const START_LOGGING: LogId = crate::new_log_id!("START_LOGGING", LogLevel::Info);
36/// Notify LOGGER and listeners to stop logging.
37///
38/// **Note:** Filter does not affect capturing of this LogId. It is up to the handler to decide wether to filter it or not.
39pub const STOP_LOGGING: LogId = crate::new_log_id!("STOP_LOGGING", LogLevel::Info);
40
41impl LogId {
42    pub const fn new(
43        module_path: &'static str,
44        identifier: &'static str,
45        log_level: LogLevel,
46    ) -> Self {
47        LogId {
48            module_path,
49            identifier,
50            log_level,
51        }
52    }
53
54    pub fn get_module_path(&self) -> &'static str {
55        self.module_path
56    }
57
58    pub fn get_identifier(&self) -> &'static str {
59        self.identifier
60    }
61
62    pub fn get_log_level(&self) -> LogLevel {
63        self.log_level
64    }
65}
66
67impl std::fmt::Display for LogId {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(f, "id='{}::{}'", self.module_path, self.identifier)
70    }
71}
72
73/// Log level a [`LogId`] may represent.
74#[derive(Debug, Default, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, std::hash::Hash)]
75pub enum LogLevel {
76    Trace = 0,
77    #[default]
78    Debug = 1,
79    Info = 2,
80    Warn = 3,
81    Error = 4,
82}
83
84impl std::fmt::Display for LogLevel {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        let s = match self {
87            LogLevel::Error => "ERR",
88            LogLevel::Warn => "WARN",
89            LogLevel::Info => "INFO",
90            LogLevel::Debug => "DEBUG",
91            LogLevel::Trace => "TRACE",
92        };
93
94        write!(f, "{s}")
95    }
96}
97
98/// Macro to create a [`LogId`] with a custom identifier and [`LogLevel`].
99///
100/// **Note:** The identifier must be a string literal.
101///
102/// ## Usage
103///
104/// ```
105/// use logid_core::{new_log_id, log_id::LogLevel};
106///
107/// let id = new_log_id!("custom_ident", LogLevel::Debug);
108/// ```
109#[macro_export]
110macro_rules! new_log_id {
111    ($identifier:expr, $log_level:expr) => {
112        $crate::log_id::LogId::new(module_path!(), $identifier, $log_level)
113    };
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn create_log_id_with_macro() {
122        let log_id = new_log_id!("custom_ident", LogLevel::Debug);
123
124        assert_eq!(
125            log_id.module_path,
126            module_path!(),
127            "Module path was not set correctly using `log_id!()` macro."
128        );
129        assert_eq!(
130            log_id.identifier, "custom_ident",
131            "Identifier was not set correctly using `log_id!()` macro."
132        );
133        assert_eq!(
134            log_id.log_level,
135            LogLevel::Debug,
136            "Log level was not set correctly using `log_id!()` macro."
137        );
138    }
139}