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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 14:29:11 +00:00
|
|
|
pub fn init(config: &CliConfig) -> Result<(), SetLoggerError> {
|
|
|
|
let lvl = if config.is_debugging() {
|
2015-10-26 20:41:45 +00:00
|
|
|
LogLevelFilter::Debug
|
2016-01-02 14:29:11 +00:00
|
|
|
} else if config.is_verbose() {
|
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-12-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Runtime object, represents a single interface to both the CLI configuration and the
|
|
|
|
* configuration file. Also carries the store object around and is basically an object which
|
|
|
|
* contains everything which is required to run a command/module.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Check whether we run verbose
|
|
|
|
*/
|
2015-10-18 18:52:52 +00:00
|
|
|
pub fn is_verbose(&self) -> bool {
|
2016-01-02 14:39:57 +00:00
|
|
|
self.config.is_verbose()
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Check whether we run in debugging
|
|
|
|
*/
|
2015-10-18 18:52:52 +00:00
|
|
|
pub fn is_debugging(&self) -> bool {
|
2016-01-02 14:39:57 +00:00
|
|
|
self.config.is_debugging()
|
2015-10-18 18:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Get the store path we are currently using
|
|
|
|
*/
|
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-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Get the store object
|
|
|
|
*/
|
2015-12-27 23:39:55 +00:00
|
|
|
pub fn store(&self) -> &Store {
|
|
|
|
&self.store
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:54:18 +00:00
|
|
|
/**
|
|
|
|
* Get the runtime path we are currently using
|
|
|
|
*/
|
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() {
|
2015-12-30 01:53:05 +00:00
|
|
|
editor + &self.config.editor_opts()[..]
|
2015-12-30 01:41:11 +00:00
|
|
|
} else if let Some(editor) = self.configuration.editor() {
|
2015-12-30 01:53:05 +00:00
|
|
|
editor + &self.configuration.editor_opts()[..]
|
2015-12-30 01:41:11 +00:00
|
|
|
} 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|