2015-12-05 00:14:13 +00:00
|
|
|
use std::fmt::{Debug, Formatter, Error};
|
|
|
|
|
2015-10-26 19:53:12 +00:00
|
|
|
extern crate log;
|
2015-12-05 00:14:13 +00:00
|
|
|
use log::{LogRecord, LogLevel, LogLevelFilter, LogMetadata, SetLoggerError};
|
2015-10-26 19:53:12 +00:00
|
|
|
|
2015-10-26 23:02:42 +00:00
|
|
|
pub use cli::CliConfig;
|
2015-10-26 22:59:24 +00:00
|
|
|
pub use configuration::Configuration as Cfg;
|
2015-10-18 18:52:52 +00:00
|
|
|
|
2015-12-27 23:39:55 +00:00
|
|
|
use storage::Store;
|
|
|
|
|
2015-10-26 19:53:12 +00:00
|
|
|
pub struct ImagLogger {
|
|
|
|
lvl: LogLevel,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImagLogger {
|
|
|
|
|
|
|
|
pub fn new(lvl: LogLevel) -> ImagLogger {
|
|
|
|
ImagLogger {
|
|
|
|
lvl: lvl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 23:02:42 +00:00
|
|
|
pub fn init(cfg: &Cfg, config: &CliConfig) -> Result<(), SetLoggerError> {
|
2015-10-26 20:41:45 +00:00
|
|
|
let lvl = if config.is_debugging() || cfg.is_debugging() {
|
|
|
|
LogLevelFilter::Debug
|
2015-10-26 22:59:24 +00:00
|
|
|
} else if config.is_verbose() || cfg.is_debugging() {
|
2015-10-26 20:41:45 +00:00
|
|
|
LogLevelFilter::Info
|
2015-10-26 19:53:12 +00:00
|
|
|
} else {
|
2015-10-26 20:41:45 +00:00
|
|
|
LogLevelFilter::Error
|
|
|
|
};
|
2015-10-26 19:53:12 +00:00
|
|
|
|
|
|
|
log::set_logger(|max_log_lvl| {
|
2015-10-26 20:41:45 +00:00
|
|
|
max_log_lvl.set(lvl);
|
|
|
|
debug!("Init logger with: {}", lvl);
|
|
|
|
Box::new(ImagLogger::new(lvl.to_log_level().unwrap()))
|
2015-10-26 19:53:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-26 23:02:42 +00:00
|
|
|
pub config : CliConfig<'a>,
|
2015-10-26 23:01:11 +00:00
|
|
|
pub configuration : Cfg,
|
2015-12-27 23:39:55 +00:00
|
|
|
pub store : Store,
|
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
|
|
|
|
2015-10-26 23:02:42 +00:00
|
|
|
pub fn new(cfg: Cfg, config : CliConfig<'a>) -> Runtime<'a> {
|
2015-12-27 23:39:55 +00:00
|
|
|
let sp = config.store_path().unwrap_or(cfg.store_path());
|
2015-10-18 18:52:52 +00:00
|
|
|
Runtime {
|
|
|
|
config: config,
|
2015-10-26 23:01:11 +00:00
|
|
|
configuration: cfg,
|
2015-12-27 23:39:55 +00:00
|
|
|
store: Store::new(sp),
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_verbose(&self) -> bool {
|
2015-10-26 23:01:11 +00:00
|
|
|
self.config.is_verbose() || self.configuration.is_verbose()
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_debugging(&self) -> bool {
|
2015-10-26 23:01:11 +00:00
|
|
|
self.config.is_debugging() || self.configuration.is_verbose()
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 17:37:44 +00:00
|
|
|
pub fn store_path(&self) -> String {
|
|
|
|
self.config.store_path().unwrap_or(self.configuration.store_path())
|
|
|
|
}
|
|
|
|
|
2015-12-27 23:39:55 +00:00
|
|
|
pub fn store(&self) -> &Store {
|
|
|
|
&self.store
|
|
|
|
}
|
|
|
|
|
2015-11-23 18:48:33 +00:00
|
|
|
pub fn get_rtp(&self) -> String {
|
|
|
|
if let Some(rtp) = self.config.get_rtp() {
|
|
|
|
rtp
|
|
|
|
} else {
|
|
|
|
self.configuration.get_rtp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 01:41:11 +00:00
|
|
|
pub fn editor(&self) -> String {
|
|
|
|
use std::env::var;
|
|
|
|
|
|
|
|
if let Some(editor) = self.config.editor() {
|
|
|
|
editor
|
|
|
|
} else if let Some(editor) = self.configuration.editor() {
|
|
|
|
editor
|
|
|
|
} else if let Ok(editor) = var("EDITOR") {
|
|
|
|
editor
|
|
|
|
} else {
|
|
|
|
String::from("vim")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
2015-11-27 18:21:40 +00:00
|
|
|
|
|
|
|
impl<'a> Debug for Runtime<'a> {
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
write!(f, "Runtime (verbose: {}, debugging: {}, rtp: {})",
|
|
|
|
self.is_verbose(),
|
|
|
|
self.is_debugging(),
|
|
|
|
self.get_rtp())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|