Add proper logging

This commit is contained in:
Rodolphe Breard 2018-12-29 21:03:33 +01:00
parent 2fecbedfa1
commit c25dfb253a
4 changed files with 25 additions and 11 deletions

View file

@ -12,4 +12,12 @@ license = "CECILL-B"
include = ["src/**/*", "Cargo.toml", "Licence_*.txt"] include = ["src/**/*", "Cargo.toml", "Licence_*.txt"]
[dependencies] [dependencies]
nom = "^4.1" log = "0.4"
nom = "4.1"
[[example]]
name = "dummy"
path = "examples/dummy.rs"
[dev-dependencies]
env_logger = "0.6"

View file

@ -1,3 +1,6 @@
use env_logger::{Builder, Env};
fn main() { fn main() {
Builder::from_env(Env::default().default_filter_or("debug")).init();
opensmtpd::run(); opensmtpd::run();
} }

View file

@ -1,4 +1,5 @@
use crate::entry::Entry; use crate::entry::Entry;
use std::fmt;
pub struct Error { pub struct Error {
message: String, message: String,
@ -10,9 +11,11 @@ impl Error {
message: msg.to_string(), message: msg.to_string(),
} }
} }
}
pub fn display(&self) { impl fmt::Display for Error {
eprintln!("Error: {}", self.message); fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
} }
} }

View file

@ -3,6 +3,7 @@ mod errors;
use crate::entry::Entry; use crate::entry::Entry;
use crate::errors::Error; use crate::errors::Error;
use log::{debug, error, warn};
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use std::sync::mpsc; use std::sync::mpsc;
@ -32,13 +33,12 @@ fn dispatch(
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let name = entry.session_id.to_string(); let name = entry.session_id.to_string();
let handle = thread::Builder::new().name(name).spawn(move || { let handle = thread::Builder::new().name(name).spawn(move || {
println!("New thread: {}", thread::current().name().unwrap()); debug!(
for e in rx.iter() { "New thread for session {}",
println!( thread::current().name().unwrap()
"Debug: thread {}: {:?}",
thread::current().name().unwrap(),
e
); );
for e in rx.iter() {
debug!("thread {}: {:?}", thread::current().name().unwrap(), e);
} }
})?; })?;
sessions.insert(entry.session_id, (tx, handle)); sessions.insert(entry.session_id, (tx, handle));
@ -74,13 +74,13 @@ pub fn run() {
Ok(l) => l, Ok(l) => l,
Err(e) => { Err(e) => {
graceful_exit_children(&mut sessions); graceful_exit_children(&mut sessions);
e.display(); error!("{}", e);
std::process::exit(1); std::process::exit(1);
} }
}; };
match dispatch(&mut sessions, &line) { match dispatch(&mut sessions, &line) {
Ok(_) => {} Ok(_) => {}
Err(e) => e.display(), Err(e) => warn!("{}", e),
} }
} }
} }