/
mod.rs
90 lines (78 loc) · 2.21 KB
/
mod.rs
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
use std::{
cell::RefCell, borrow::Cow, sync::atomic::{AtomicUsize, Ordering},
};
use crate::channel::{self, Receiver, Sender};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(usize)]
pub enum Level {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}
#[derive(Clone)]
pub struct Logger {
pub channel : Sender<Message>
}
impl Default for Logger {
fn default() -> Self {
let (sender,_reveiver) = channel::channel();
Self { channel: sender }
}
}
impl Logger {
pub fn new(sender : Sender<Message>) -> Self {
Self {
channel : sender
}
}
pub fn log(&self, level : Level, module : &'static str, file : &'static str, line : u32, data : Cow<'static, str>) {
let _ = self.channel.send(Message { level, module, file, line, data });
}
}
#[derive(Debug, Clone)]
pub struct Message {
pub level : Level,
pub module : &'static str,
pub line : u32,
pub file : &'static str,
pub data : Cow<'static, str>,
}
#[macro_use]
pub mod macros;
static MAX_NOTIFY_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
//static NOTIFY_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
#[inline]
pub fn set_max_level(level: Level) {
MAX_NOTIFY_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
}
#[inline]
pub fn enabled_level(level: &Level) -> bool {
MAX_NOTIFY_LEVEL_FILTER.load(Ordering::Relaxed) >= (*level as usize)
}
#[inline]
pub fn max_level() -> Level {
unsafe { std::mem::transmute(MAX_NOTIFY_LEVEL_FILTER.load(Ordering::Relaxed)) }
}
thread_local! {
pub static COMPONENT_LOGGER : RefCell<Logger> = RefCell::new(Logger::default());
}
/// Initializes the logger for the current thread/component.
pub fn initialize_logger(msngr: Logger) {
let _ = COMPONENT_LOGGER.with(|v| {
let mut brw = v.borrow_mut();
*brw = msngr;
Ok::<(), ()>(())
});
// Wait for local_key_cell_methods
//COMPONENT_LOGGER.replace(msngr);
}
/// Use for fast initialization during testing
pub fn testing_logger_dummy() -> Receiver<Message> {
let (sender, receiver) = channel::channel();
let msngr = Logger::new(sender);
initialize_logger(msngr);
receiver
}