From c41ec1d85c82fab967cd4ec64ce13b6fc55377de Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 20:53:12 +0100 Subject: [PATCH 1/3] Add logger with logging library --- src/main.rs | 5 ++++- src/runtime.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index bfcd9726..08e02a79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ #[macro_use] extern crate clap; +#[macro_use] extern crate log; use cli::Config; -use runtime::Runtime; +use runtime::{ImagLogger, Runtime}; use clap::App; mod cli; @@ -10,10 +11,12 @@ mod module; mod storage; fn main() { + let early_logger = ImagLogger::early().unwrap(); let yaml = load_yaml!("../etc/cli.yml"); let app = App::from_yaml(yaml); let mut config = Config::new(app); + let logger = ImagLogger::init(&config); let rt = Runtime::new(config); println!("Hello, world!"); diff --git a/src/runtime.rs b/src/runtime.rs index 7c8c2021..e65d3ecf 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,7 +1,59 @@ +extern crate log; + pub use cli::Config; use std::io::stderr; 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); + 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 config : Config<'a>, From 127c99f16f212365c655d585fff601c2a7793f17 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 20:53:49 +0100 Subject: [PATCH 2/3] Remove old logging functions --- src/runtime.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index e65d3ecf..195cd620 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -67,22 +67,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 { self.config.is_verbose() } From 7f224e8f5850dca94ab18c4c8fc6def6a2932292 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 20:55:31 +0100 Subject: [PATCH 3/3] Replace all output with macros from logging library --- src/main.rs | 2 +- src/module/mod.rs | 3 +-- src/runtime.rs | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 08e02a79..57a8400d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,5 +19,5 @@ fn main() { let logger = ImagLogger::init(&config); let rt = Runtime::new(config); - println!("Hello, world!"); + info!("Hello, world!"); } diff --git a/src/module/mod.rs b/src/module/mod.rs index 8e98d1d2..9fd8b88a 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -37,8 +37,7 @@ impl Error for ModuleError { impl Display for ModuleError { fn fmt(&self, f: &mut Formatter) -> FMTResult { - write!(f, "ModuleError: {}", - self.description()) + write!(f, "ModuleError: {}", self.description()) } } diff --git a/src/runtime.rs b/src/runtime.rs index 195cd620..def9a8f7 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -36,6 +36,7 @@ impl ImagLogger { 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())) }) }