imag/src/runtime.rs

83 lines
1.9 KiB
Rust
Raw Normal View History

2015-10-26 19:53:12 +00:00
extern crate log;
2015-10-19 16:09:51 +00:00
pub use cli::Config;
2015-10-26 22:59:24 +00:00
pub use configuration::Configuration as Cfg;
2015-10-18 18:52:52 +00:00
use std::io::stderr;
use std::io::Write;
2015-10-26 19:53:12 +00:00
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)
}
2015-10-26 22:59:24 +00:00
pub fn init(cfg: &Cfg, config: &Config) -> Result<(), SetLoggerError> {
if config.is_debugging() || cfg.is_debugging() {
2015-10-26 19:53:12 +00:00
ImagLogger::init_logger(LogLevelFilter::Debug)
2015-10-26 22:59:24 +00:00
} else if config.is_verbose() || cfg.is_debugging() {
2015-10-26 19:53:12 +00:00
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);
2015-10-26 19:53:12 +00:00
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());
}
}
}
2015-10-18 18:52:52 +00:00
2015-10-25 18:47:17 +00:00
pub struct Runtime<'a> {
2015-10-25 19:09:49 +00:00
pub config : Config<'a>,
pub configuration : Cfg,
2015-10-18 18:52:52 +00:00
}
2015-10-25 18:47:17 +00:00
impl<'a> Runtime<'a> {
2015-10-18 18:52:52 +00:00
pub fn new(cfg: Cfg, config : Config<'a>) -> Runtime<'a> {
2015-10-18 18:52:52 +00:00
Runtime {
config: config,
configuration: cfg,
2015-10-18 18:52:52 +00:00
}
}
pub fn is_verbose(&self) -> bool {
self.config.is_verbose() || self.configuration.is_verbose()
2015-10-18 18:52:52 +00:00
}
pub fn is_debugging(&self) -> bool {
self.config.is_debugging() || self.configuration.is_verbose()
2015-10-18 18:52:52 +00:00
}
}