logging: Move all facilities to one file

This commit is contained in:
Dorota Czaplejewicz
2019-12-16 19:15:40 +00:00
parent 1237537005
commit e2b89e85f7
6 changed files with 50 additions and 42 deletions

43
src/logging.rs Normal file
View File

@ -0,0 +1,43 @@
/*! Logging library */
use std::error::Error;
/// Sugar for logging errors in results
pub trait Warn {
type Value;
fn ok_warn(self, msg: &str) -> Option<Self::Value>;
}
impl<T, E: Error> Warn for Result<T, E> {
type Value = T;
fn ok_warn(self, msg: &str) -> Option<T> {
self.map_err(|e| {
eprintln!("{}: {}", msg, e);
e
}).ok()
}
}
pub trait WarningHandler {
/// Handle a warning
fn handle(&mut self, warning: &str);
}
/// Prints warnings to stderr
pub struct PrintWarnings;
impl WarningHandler for PrintWarnings {
fn handle(&mut self, warning: &str) {
eprintln!("{}", warning);
}
}
/// Warning handler that will panic at any warning.
/// Don't use except in tests
pub struct PanicWarn;
impl WarningHandler for PanicWarn {
fn handle(&mut self, warning: &str) {
panic!("{}", warning);
}
}