Skip to main content

datex_core/utils/
logger.rs

1use crate::{prelude::*, values::core_values::sender::Sender};
2use core::cell::RefCell;
3use datex_macros_internal::Datex;
4
5#[derive(Datex, Clone, Copy)]
6#[datex(structural)]
7pub enum LogLevel {
8    Trace,
9    Debug,
10    Info,
11    Warn,
12    Error,
13}
14
15impl From<LogLevel> for log::Level {
16    fn from(level: LogLevel) -> Self {
17        match level {
18            LogLevel::Trace => log::Level::Trace,
19            LogLevel::Debug => log::Level::Debug,
20            LogLevel::Info => log::Level::Info,
21            LogLevel::Warn => log::Level::Warn,
22            LogLevel::Error => log::Level::Error,
23        }
24    }
25}
26
27#[derive(Datex, Clone)]
28#[datex(structural)]
29pub struct LogMessage {
30    level: LogLevel,
31    module: String,
32    tag: Option<String>,
33    message: String,
34}
35
36impl LogMessage {
37    pub fn new(
38        level: LogLevel,
39        module: String,
40        tag: Option<String>,
41        message: String,
42    ) -> Self {
43        Self {
44            level,
45            module,
46            tag,
47            message,
48        }
49    }
50}
51
52// FIXME: better solution, should be thread_local here, but not supported on esp32 target
53struct SingleContext<T>(T);
54/// SAFETY: only works for single threaded
55unsafe impl<T> Sync for SingleContext<T> {}
56
57static SENDERS: SingleContext<RefCell<Vec<Sender<LogMessage>>>> =
58    SingleContext(RefCell::new(Vec::new()));
59
60/// Registers a sender which is called for any global log
61pub fn register_log_sender(sender: Sender<LogMessage>) {
62    SENDERS.0.borrow_mut().push(sender);
63}
64
65pub fn log_message(message: LogMessage) {
66    let senders = &mut SENDERS.0.borrow_mut();
67
68    // send message on all senders, remove all closed senders
69    senders.retain_mut(|sender| sender.send(message.clone()).is_ok());
70}
71
72pub fn string_with_maybe_tag(string: String, tag: &Option<String>) -> String {
73    if let Some(tag) = tag {
74        format!("[{}] {}", tag, string)
75    } else {
76        string
77    }
78}
79
80macro _log($level:expr, $tag:expr, $($arg:tt)+) {
81    let string = format!($($arg)+);
82    let string_with_tag = string_with_maybe_tag(string.clone(), &$tag);
83
84    log::log!(log::Level::from($level), "{}", string_with_tag);
85    log_message(LogMessage::new($level, module_path!().to_string(), $tag, string));
86}
87
88pub macro log_info($($arg:tt)+) {
89    _log!(LogLevel::Info, None, $($arg)+);
90}
91pub macro log_info_with_tag($tag:tt, $($arg:tt)+) {
92    _log!(LogLevel::Info, Some($tag.to_string()), $($arg)+);
93}
94
95pub macro log_error($($arg:tt)+) {
96    _log!(LogLevel::Error, Some($tag.to_string()), $($arg)+);
97}
98pub macro log_error_with_tag($tag:tt, $($arg:tt)+) {
99    _log!(LogLevel::Error, Some($tag.to_string()), $($arg)+);
100}
101
102pub macro log_warn($($arg:tt)+) {
103    _log!(LogLevel::Warn, Some($tag.to_string()), $($arg)+);
104}
105pub macro log_warn_with_tag($tag:tt, $($arg:tt)+) {
106    _log!(LogLevel::Warn, Some($tag.to_string()), $($arg)+);
107}
108
109pub macro log_trace($($arg:tt)+) {
110    _log!(LogLevel::Trace, Some($tag.to_string()), $($arg)+);
111}
112pub macro log_trace_with_tag($tag:tt, $($arg:tt)+) {
113    _log!(LogLevel::Trace, Some($tag.to_string()), $($arg)+);
114}
115
116pub macro log_debug($($arg:tt)+) {
117    _log!(LogLevel::Debug, Some($tag.to_string()), $($arg)+);
118}
119pub macro log_debug_with_tag($tag:tt, $($arg:tt)+) {
120    _log!(LogLevel::Debug, Some($tag.to_string()), $($arg)+);
121}