usiem/logging/
mod.rs

1use std::{
2    cell::RefCell,
3    sync::atomic::{AtomicUsize, Ordering},
4};
5
6use crate::prelude::{kernel_message::KernelMessager, NotificationLevel};
7
8#[macro_use]
9pub mod macros;
10
11static MAX_NOTIFY_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
12//static NOTIFY_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
13
14#[inline]
15pub fn set_max_level(level: NotificationLevel) {
16    MAX_NOTIFY_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
17}
18
19#[inline]
20pub fn enabled_level(level: &NotificationLevel) -> bool {
21    MAX_NOTIFY_LEVEL_FILTER.load(Ordering::Relaxed) >= (*level as usize)
22}
23#[inline]
24pub fn max_level() -> NotificationLevel {
25    unsafe { std::mem::transmute(MAX_NOTIFY_LEVEL_FILTER.load(Ordering::Relaxed)) }
26}
27
28thread_local! {
29    pub static COMPONENT_LOGGER : RefCell<KernelMessager> = RefCell::new(KernelMessager::default());
30}
31
32/// Initializes the channel to communicate with the Kernel for the current thread/component.
33pub fn initialize_component_logger(msngr: KernelMessager) {
34    let _ = COMPONENT_LOGGER.with(|v| {
35        let mut brw = v.borrow_mut();
36        *brw = msngr;
37        Ok::<(), ()>(())
38    });
39    // Wait for local_key_cell_methods
40    //COMPONENT_LOGGER.replace(msngr);
41}
42
43/// Use for fast initialization of components during testing. With component ID "1234" and name "Dummy"
44pub fn testing_component_logger_dummy() -> crossbeam_channel::Receiver<crate::prelude::SiemMessage>
45{
46    let (sender, receiver) = crossbeam_channel::unbounded();
47    let msngr = KernelMessager::new(1234, "Dummy".to_string(), sender);
48    initialize_component_logger(msngr);
49    receiver
50}
51
52/// Use for fast initialization of components during testing
53pub fn testing_component_logger(
54    id: u64,
55    name: &str,
56) -> crossbeam_channel::Receiver<crate::prelude::SiemMessage> {
57    let (sender, receiver) = crossbeam_channel::unbounded();
58    let msngr = KernelMessager::new(id, name.to_string(), sender);
59    initialize_component_logger(msngr);
60    receiver
61}