imag/src/runtime.rs

142 lines
3.3 KiB
Rust
Raw Normal View History

use std::fmt::{Debug, Formatter, Error};
2015-10-26 19:53:12 +00:00
extern crate log;
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
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,
}
}
pub fn init(config: &CliConfig) -> Result<(), SetLoggerError> {
let lvl = if config.is_debugging() {
2015-10-26 20:41:45 +00:00
LogLevelFilter::Debug
} 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>,
pub configuration : Cfg,
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> {
let sp = config.store_path().unwrap_or(cfg.store_path());
2015-10-18 18:52:52 +00:00
Runtime {
config: config,
configuration: cfg,
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 {
self.config.is_verbose() || self.configuration.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 {
self.config.is_debugging() || self.configuration.is_verbose()
2015-10-18 18:52:52 +00:00
}
2015-12-29 15:54:18 +00:00
/**
* Get the store path we are currently using
*/
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
*/
pub fn store(&self) -> &Store {
&self.store
}
2015-12-29 15:54:18 +00:00
/**
* Get the runtime path we are currently using
*/
pub fn get_rtp(&self) -> String {
if let Some(rtp) = self.config.get_rtp() {
rtp
} else {
self.configuration.get_rtp()
}
}
pub fn editor(&self) -> String {
use std::env::var;
if let Some(editor) = self.config.editor() {
editor + &self.config.editor_opts()[..]
} else if let Some(editor) = self.configuration.editor() {
editor + &self.configuration.editor_opts()[..]
} 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())
}
}