diff --git a/Cargo.toml b/Cargo.toml index a50bd80..93feb7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,12 @@ license = "CECILL-B" include = ["src/**/*", "Cargo.toml", "Licence_*.txt"] [dependencies] -nom = "^4.1" +log = "0.4" +nom = "4.1" + +[[example]] +name = "dummy" +path = "examples/dummy.rs" + +[dev-dependencies] +env_logger = "0.6" diff --git a/examples/dummy.rs b/examples/dummy.rs index 6cab936..dec43fa 100644 --- a/examples/dummy.rs +++ b/examples/dummy.rs @@ -1,3 +1,6 @@ +use env_logger::{Builder, Env}; + fn main() { + Builder::from_env(Env::default().default_filter_or("debug")).init(); opensmtpd::run(); } diff --git a/src/errors.rs b/src/errors.rs index afe293d..e2b6c11 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,5 @@ use crate::entry::Entry; +use std::fmt; pub struct Error { message: String, @@ -10,9 +11,11 @@ impl Error { message: msg.to_string(), } } +} - pub fn display(&self) { - eprintln!("Error: {}", self.message); +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.message) } } diff --git a/src/lib.rs b/src/lib.rs index ce71a66..098a700 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ mod errors; use crate::entry::Entry; use crate::errors::Error; +use log::{debug, error, warn}; use std::collections::HashMap; use std::io; use std::sync::mpsc; @@ -32,13 +33,12 @@ fn dispatch( let (tx, rx) = mpsc::channel(); let name = entry.session_id.to_string(); let handle = thread::Builder::new().name(name).spawn(move || { - println!("New thread: {}", thread::current().name().unwrap()); + debug!( + "New thread for session {}", + thread::current().name().unwrap() + ); for e in rx.iter() { - println!( - "Debug: thread {}: {:?}", - thread::current().name().unwrap(), - e - ); + debug!("thread {}: {:?}", thread::current().name().unwrap(), e); } })?; sessions.insert(entry.session_id, (tx, handle)); @@ -74,13 +74,13 @@ pub fn run() { Ok(l) => l, Err(e) => { graceful_exit_children(&mut sessions); - e.display(); + error!("{}", e); std::process::exit(1); } }; match dispatch(&mut sessions, &line) { Ok(_) => {} - Err(e) => e.display(), + Err(e) => warn!("{}", e), } } }