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:
Floris Bruynooghe
2019-11-23 16:05:07 +01:00
parent 4312c03a0b
commit 780cd9d864
3 changed files with 13 additions and 1 deletions

View File

@@ -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!(

View File

@@ -22,7 +22,7 @@ extern crate jetscii;
extern crate debug_stub_derive;
#[macro_use]
mod log;
pub mod log;
#[macro_use]
pub mod error;

View File

@@ -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));
}};
}