diff --git a/opensmtpd/Cargo.toml b/opensmtpd/Cargo.toml index d16eba1..3e38ded 100644 --- a/opensmtpd/Cargo.toml +++ b/opensmtpd/Cargo.toml @@ -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" diff --git a/opensmtpd/examples/dummy.rs b/opensmtpd/examples/dummy.rs index de1c079..673dab1 100644 --- a/opensmtpd/examples/dummy.rs +++ b/opensmtpd/examples/dummy.rs @@ -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(); diff --git a/opensmtpd/examples/session_event_counter.rs b/opensmtpd/examples/session_event_counter.rs index f143370..c899da8 100644 --- a/opensmtpd/examples/session_event_counter.rs +++ b/opensmtpd/examples/session_event_counter.rs @@ -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(); } diff --git a/opensmtpd/src/errors.rs b/opensmtpd/src/errors.rs index 26f707b..503cac4 100644 --- a/opensmtpd/src/errors.rs +++ b/opensmtpd/src/errors.rs @@ -26,6 +26,12 @@ impl From for Error { } } +impl From for Error { + fn from(error: log::SetLoggerError) -> Self { + Error::new(&format!("Logger error: {}", error)) + } +} + impl From> for Error { fn from(error: nom::Err<&str>) -> Self { let msg = match error { diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index 487bc72..00c227a 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -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] diff --git a/opensmtpd/src/logger.rs b/opensmtpd/src/logger.rs new file mode 100644 index 0000000..0c5a813 --- /dev/null +++ b/opensmtpd/src/logger.rs @@ -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) {} +}