Merge branch 'logging'

This commit is contained in:
Matthias Beyer 2015-10-26 20:59:22 +01:00
commit b207e1ad3d
3 changed files with 59 additions and 20 deletions

View file

@ -1,7 +1,8 @@
#[macro_use] extern crate clap; #[macro_use] extern crate clap;
#[macro_use] extern crate log;
use cli::Config; use cli::Config;
use runtime::Runtime; use runtime::{ImagLogger, Runtime};
use clap::App; use clap::App;
mod cli; mod cli;
@ -10,11 +11,13 @@ mod module;
mod storage; mod storage;
fn main() { fn main() {
let early_logger = ImagLogger::early().unwrap();
let yaml = load_yaml!("../etc/cli.yml"); let yaml = load_yaml!("../etc/cli.yml");
let app = App::from_yaml(yaml); let app = App::from_yaml(yaml);
let mut config = Config::new(app); let mut config = Config::new(app);
let logger = ImagLogger::init(&config);
let rt = Runtime::new(config); let rt = Runtime::new(config);
println!("Hello, world!"); info!("Hello, world!");
} }

View file

@ -37,8 +37,7 @@ impl Error for ModuleError {
impl Display for ModuleError { impl Display for ModuleError {
fn fmt(&self, f: &mut Formatter) -> FMTResult { fn fmt(&self, f: &mut Formatter) -> FMTResult {
write!(f, "ModuleError: {}", write!(f, "ModuleError: {}", self.description())
self.description())
} }
} }

View file

@ -1,7 +1,60 @@
extern crate log;
pub use cli::Config; pub use cli::Config;
use std::io::stderr; use std::io::stderr;
use std::io::Write; use std::io::Write;
use log::{LogRecord, LogLevel, LogLevelFilter, LogMetadata, SetLoggerError};
pub struct ImagLogger {
lvl: LogLevel,
}
impl ImagLogger {
pub fn new(lvl: LogLevel) -> ImagLogger {
ImagLogger {
lvl: lvl,
}
}
pub fn early() -> Result<(), SetLoggerError> {
ImagLogger::init_logger(LogLevelFilter::Error)
}
pub fn init(config: &Config) -> Result<(), SetLoggerError> {
if config.is_debugging() {
ImagLogger::init_logger(LogLevelFilter::Debug)
} else if config.is_verbose() {
ImagLogger::init_logger(LogLevelFilter::Info)
} else {
ImagLogger::init_logger(LogLevelFilter::Error)
}
}
fn init_logger(lvlflt : LogLevelFilter) -> Result<(), SetLoggerError> {
log::set_logger(|max_log_lvl| {
max_log_lvl.set(lvlflt);
debug!("Init logger with: {}", lvlflt);
Box::new(ImagLogger::new(lvlflt.to_log_level().unwrap()))
})
}
}
impl log::Log for ImagLogger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
metadata.level() <= self.lvl
}
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
println!("[{}]: {}", record.level(), record.args());
}
}
}
pub struct Runtime<'a> { pub struct Runtime<'a> {
pub config : Config<'a>, pub config : Config<'a>,
@ -15,22 +68,6 @@ impl<'a> Runtime<'a> {
} }
} }
pub fn debug(&self, string : &'static str) {
if self.config.is_debugging() {
println!("{}", string);
}
}
pub fn print(&self, string : &'static str) {
if self.config.is_verbose() || self.config.is_debugging() {
println!("{}", string);
}
}
pub fn trace(string : &'static str) {
// writeln!(&mut stderr, "{}", string);
}
pub fn is_verbose(&self) -> bool { pub fn is_verbose(&self) -> bool {
self.config.is_verbose() self.config.is_verbose()
} }