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"]
[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() {
Builder::from_env(Env::default().default_filter_or("debug")).init();
opensmtpd::run();
}

View file

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

View file

@ -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());
for e in rx.iter() {
println!(
"Debug: thread {}: {:?}",
thread::current().name().unwrap(),
e
debug!(
"New thread for session {}",
thread::current().name().unwrap()
);
for e in rx.iter() {
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),
}
}
}