Split Memory Log into Log Levels, support different lenght limits (hardcoded for now)
All checks were successful
Run Tests on Code / run-tests (push) Successful in 17s
All checks were successful
Run Tests on Code / run-tests (push) Successful in 17s
This commit is contained in:
parent
ff0360ac07
commit
ebbfdcea63
1 changed files with 43 additions and 13 deletions
|
@ -1,22 +1,52 @@
|
|||
use std::collections::VecDeque;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::Level;
|
||||
use parking_lot::Mutex;
|
||||
use systemd_journal_logger::connected_to_journal;
|
||||
|
||||
fn mem_log(msg: Option<String>) -> VecDeque<LogEvent> {
|
||||
static MEM_LOG: Mutex<VecDeque<LogEvent>> = Mutex::new(VecDeque::new());
|
||||
fn mem_log(level: Level, msg: Option<String>) -> VecDeque<LogEvent> {
|
||||
static MEM_LOG_DEBUG: Mutex<VecDeque<LogEvent>> = Mutex::new(VecDeque::new());
|
||||
static MEM_LOG_INFO: Mutex<VecDeque<LogEvent>> = Mutex::new(VecDeque::new());
|
||||
static MEM_LOG_WARN: Mutex<VecDeque<LogEvent>> = Mutex::new(VecDeque::new());
|
||||
static MEM_LOG_ERROR: Mutex<VecDeque<LogEvent>> = Mutex::new(VecDeque::new());
|
||||
|
||||
let max_len: i8;
|
||||
|
||||
let mut list = match level {
|
||||
Level::Debug => {
|
||||
max_len = 10;
|
||||
MEM_LOG_DEBUG.lock()
|
||||
},
|
||||
Level::Info => {
|
||||
max_len = 20;
|
||||
MEM_LOG_INFO.lock()
|
||||
},
|
||||
Level::Warn => {
|
||||
max_len = 40;
|
||||
MEM_LOG_WARN.lock()
|
||||
},
|
||||
Level::Error => {
|
||||
max_len = -1;
|
||||
MEM_LOG_ERROR.lock()
|
||||
},
|
||||
Level::Trace => {
|
||||
max_len = 10;
|
||||
MEM_LOG_DEBUG.lock()
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(msg) = msg {
|
||||
let now = Utc::now();
|
||||
let log_event = LogEvent::new(now, msg);
|
||||
let mut lock = MEM_LOG.lock();
|
||||
lock.push_back(log_event);
|
||||
while lock.len() > 10 {
|
||||
lock.pop_front();
|
||||
list.push_back(log_event);
|
||||
if max_len != -1 {
|
||||
while list.len() > max_len as usize {
|
||||
list.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MEM_LOG.lock().clone();
|
||||
list.clone()
|
||||
}
|
||||
|
||||
pub struct Logging {
|
||||
|
@ -29,7 +59,7 @@ impl Logging {
|
|||
true => log::debug!("{msg}"),
|
||||
false => println!("{msg}"),
|
||||
}
|
||||
mem_log(Some(msg));
|
||||
mem_log(Level::Debug, Some(msg));
|
||||
}
|
||||
|
||||
pub fn info(msg: &str) {
|
||||
|
@ -38,7 +68,7 @@ impl Logging {
|
|||
true => log::info!("{msg}"),
|
||||
false => println!("{msg}"),
|
||||
}
|
||||
mem_log(Some(msg));
|
||||
mem_log(Level::Info, Some(msg));
|
||||
}
|
||||
|
||||
pub fn warn(msg: &str) {
|
||||
|
@ -47,7 +77,7 @@ impl Logging {
|
|||
true => log::warn!("{msg}"),
|
||||
false => println!("{msg}"),
|
||||
}
|
||||
mem_log(Some(msg));
|
||||
mem_log(Level::Warn, Some(msg));
|
||||
}
|
||||
|
||||
pub fn error(msg: &str) {
|
||||
|
@ -56,11 +86,11 @@ impl Logging {
|
|||
true => log::error!("{msg}"),
|
||||
false => eprintln!("{msg}"),
|
||||
}
|
||||
mem_log(Some(msg));
|
||||
mem_log(Level::Error, Some(msg));
|
||||
}
|
||||
|
||||
pub fn get_mem_log() -> VecDeque<LogEvent> {
|
||||
mem_log(None)
|
||||
pub fn get_mem_log(level: Level) -> VecDeque<LogEvent> {
|
||||
mem_log(level, None)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue