Merge branch 'logging'
This commit is contained in:
commit
b207e1ad3d
3 changed files with 59 additions and 20 deletions
|
@ -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,11 +11,13 @@ 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!");
|
||||
info!("Hello, world!");
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,60 @@
|
|||
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);
|
||||
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 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 {
|
||||
self.config.is_verbose()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue