Add a logger
OpenSMTPD plans to gather all logs from filters. Therefore, a default logger that implements the smtpd (future) log interface has to be available.
This commit is contained in:
parent
4b1f99db7e
commit
ae8b64941e
6 changed files with 64 additions and 13 deletions
|
@ -12,9 +12,9 @@ license = "CECILL-B"
|
|||
include = ["src/**/*", "Cargo.toml", "Licence_*.txt"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
log = {version = "0.4", features = ["std"]}
|
||||
nom = "4.1"
|
||||
opensmtpd_derive = { path = "../opensmtpd-derive", version="0.1" }
|
||||
opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.1" }
|
||||
|
||||
[[example]]
|
||||
name = "dummy"
|
||||
|
@ -23,6 +23,3 @@ path = "examples/dummy.rs"
|
|||
[[example]]
|
||||
name = "counter"
|
||||
path = "examples/session_event_counter.rs"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.6"
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use env_logger::{Builder, Env};
|
||||
use log::{debug, info};
|
||||
use opensmtpd::{event, handlers, Entry, SmtpIn};
|
||||
use log::{debug, info, Level};
|
||||
use opensmtpd::{event, handlers, Entry, SmtpIn, SmtpdLogger};
|
||||
|
||||
#[event(Any)]
|
||||
fn on_event(entry: &Entry) {
|
||||
|
@ -13,7 +12,7 @@ fn on_connect(entry: &Entry) {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||
let _ = SmtpdLogger::new().set_level(Level::Debug).init();
|
||||
SmtpIn::new()
|
||||
.event_handlers(handlers!(on_event, on_connect))
|
||||
.run();
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use env_logger::{Builder, Env};
|
||||
use log::info;
|
||||
use opensmtpd::{event, handlers, Entry, SmtpIn};
|
||||
use log::{info, Level};
|
||||
use opensmtpd::{event, handlers, Entry, SmtpIn, SmtpdLogger};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct MyContext {
|
||||
|
@ -14,6 +13,6 @@ fn on_event(ctx: &mut MyContext, entry: &Entry) {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||
let _ = SmtpdLogger::new().set_level(Level::Debug).init();
|
||||
SmtpIn::new().event_handlers(handlers!(on_event)).run();
|
||||
}
|
||||
|
|
|
@ -26,6 +26,12 @@ impl From<std::io::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<log::SetLoggerError> for Error {
|
||||
fn from(error: log::SetLoggerError) -> Self {
|
||||
Error::new(&format!("Logger error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<nom::Err<&str>> for Error {
|
||||
fn from(error: nom::Err<&str>) -> Self {
|
||||
let msg = match error {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
mod entry;
|
||||
mod errors;
|
||||
mod event_handlers;
|
||||
mod logger;
|
||||
|
||||
use log::{debug, error, warn};
|
||||
use std::collections::HashMap;
|
||||
|
@ -12,6 +13,7 @@ use std::thread;
|
|||
pub use crate::entry::{Entry, Event};
|
||||
pub use crate::errors::Error;
|
||||
pub use crate::event_handlers::{Callback, EventHandler, MatchEvent};
|
||||
pub use crate::logger::SmtpdLogger;
|
||||
pub use opensmtpd_derive::event;
|
||||
|
||||
#[macro_export]
|
||||
|
|
48
opensmtpd/src/logger.rs
Normal file
48
opensmtpd/src/logger.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use crate::errors::Error;
|
||||
use log::{Level, Metadata, Record};
|
||||
use std::io::{self, Write};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SmtpdLogger {
|
||||
level: Level,
|
||||
}
|
||||
|
||||
impl SmtpdLogger {
|
||||
pub fn new() -> Self {
|
||||
SmtpdLogger::default()
|
||||
}
|
||||
|
||||
pub fn set_level(&mut self, level: Level) -> Self {
|
||||
self.level = level;
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn init(self) -> Result<(), Error> {
|
||||
let level = self.level.to_level_filter();
|
||||
log::set_boxed_logger(Box::new(self))?;
|
||||
log::set_max_level(level);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SmtpdLogger {
|
||||
fn default() -> Self {
|
||||
SmtpdLogger { level: Level::Warn }
|
||||
}
|
||||
}
|
||||
|
||||
impl log::Log for SmtpdLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= self.level
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
let stderr = io::stderr();
|
||||
let mut handle = stderr.lock();
|
||||
writeln!(handle, "{}: {}", record.level(), record.args()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
Reference in a new issue