mirror of
https://github.com/chatmail/core.git
synced 2026-04-21 15:36:30 +03:00
Use new logging in context and log macros
This adopts the new logging functionality in the Context and makes the existing macros use it. The main thing to note is that the logger is in an RwLock, which serialises all threads on writing logs. Not doing that would need an immutable logger object, which would turn into doing many more syscalls for each log write and possibly need a per-thread logfile.
This commit is contained in:
@@ -15,6 +15,7 @@ use crate::imap::*;
|
||||
use crate::job::*;
|
||||
use crate::job_thread::JobThread;
|
||||
use crate::key::*;
|
||||
use crate::log;
|
||||
use crate::login_param::LoginParam;
|
||||
use crate::lot::Lot;
|
||||
use crate::message::{self, Message, MsgId};
|
||||
@@ -62,6 +63,7 @@ pub struct Context {
|
||||
/// Mutex to avoid generating the key for the user more than once.
|
||||
pub generating_key_mutex: Mutex<()>,
|
||||
pub translated_stockstrings: RwLock<HashMap<usize, String>>,
|
||||
pub logger: RwLock<log::Logger>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
@@ -212,6 +214,7 @@ impl Context {
|
||||
perform_inbox_jobs_needed: Arc::new(RwLock::new(false)),
|
||||
generating_key_mutex: Mutex::new(()),
|
||||
translated_stockstrings: RwLock::new(HashMap::new()),
|
||||
logger: RwLock::new(log::Logger::new(logdir)?),
|
||||
};
|
||||
|
||||
ensure!(
|
||||
|
||||
@@ -22,7 +22,7 @@ extern crate jetscii;
|
||||
extern crate debug_stub_derive;
|
||||
|
||||
#[macro_use]
|
||||
mod log;
|
||||
pub mod log;
|
||||
#[macro_use]
|
||||
pub mod error;
|
||||
|
||||
|
||||
@@ -153,6 +153,9 @@ macro_rules! info {
|
||||
};
|
||||
($ctx:expr, $msg:expr, $($args:expr),* $(,)?) => {{
|
||||
let formatted = format!($msg, $($args),*);
|
||||
if let Ok(mut logger) = $ctx.logger.write() {
|
||||
logger.log($crate::log::LogLevel::Info, callsite!(), &formatted).ok();
|
||||
}
|
||||
emit_event!($ctx, $crate::Event::Info(formatted));
|
||||
}};
|
||||
}
|
||||
@@ -164,6 +167,9 @@ macro_rules! warn {
|
||||
};
|
||||
($ctx:expr, $msg:expr, $($args:expr),* $(,)?) => {{
|
||||
let formatted = format!($msg, $($args),*);
|
||||
if let Ok(mut logger) = $ctx.logger.write() {
|
||||
logger.log($crate::log::LogLevel::Warning, callsite!(), &formatted).ok();
|
||||
}
|
||||
emit_event!($ctx, $crate::Event::Warning(formatted));
|
||||
}};
|
||||
}
|
||||
@@ -175,6 +181,9 @@ macro_rules! error {
|
||||
};
|
||||
($ctx:expr, $msg:expr, $($args:expr),* $(,)?) => {{
|
||||
let formatted = format!($msg, $($args),*);
|
||||
if let Ok(mut logger) = $ctx.logger.write() {
|
||||
logger.log($crate::log::LogLevel::Error, callsite!(), &formatted).ok();
|
||||
}
|
||||
emit_event!($ctx, $crate::Event::Error(formatted));
|
||||
}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user