use std::io::Write;
use std::io::stderr;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::ops::Deref;
use failure::ResultExt;
use failure::Fallible as Result;
use failure::Error;
use failure::err_msg;
use clap::ArgMatches;
use log::{Log, Level, Record, Metadata};
use toml::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
use handlebars::Handlebars;
use libimagerror::errors::ErrorMsg as EM;
type ModuleName = String;
#[derive(Debug)]
enum LogDestination {
Stderr,
File(Arc<Mutex<::std::fs::File>>),
}
impl Default for LogDestination {
fn default() -> LogDestination {
LogDestination::Stderr
}
}
#[derive(Debug)]
struct ModuleSettings {
enabled: bool,
level: Option<Level>,
#[allow(unused)]
destinations: Option<Vec<LogDestination>>,
}
#[derive(Debug)]
pub struct ImagLogger {
global_loglevel : Level,
#[allow(unused)]
global_destinations : Vec<LogDestination>,
module_settings : BTreeMap<ModuleName, ModuleSettings>,
handlebars: Handlebars,
}
impl ImagLogger {
pub fn new(matches: &ArgMatches, config: Option<&Value>) -> Result<ImagLogger> {
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(::handlebars::no_escape);
::libimaginteraction::format::register_all_color_helpers(&mut handlebars);
::libimaginteraction::format::register_all_format_helpers(&mut handlebars);
{
use self::log_lvl_aggregate::*;
aggregate::<Trace>(&mut handlebars, config, "TRACE")?;
aggregate::<Debug>(&mut handlebars, config, "DEBUG")?;
aggregate::<Info>(&mut handlebars, config, "INFO")?;
aggregate::<Warn>(&mut handlebars, config, "WARN")?;
aggregate::<Error>(&mut handlebars, config, "ERROR")?;
}
Ok(ImagLogger {
global_loglevel : aggregate_global_loglevel(matches, config)?,
global_destinations : aggregate_global_destinations(config)?,
module_settings : aggregate_module_settings(matches, config)?,
handlebars,
})
}
pub fn global_loglevel(&self) -> Level {
self.global_loglevel
}
}
impl Log for ImagLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= self.global_loglevel
}
fn flush(&self) {
}
fn log(&self, record: &Record) {
let mut data = BTreeMap::new();
{
data.insert("level", format!("{}", record.level()));
data.insert("module_path", String::from(record.module_path().unwrap_or("<modulepath unknown>")));
data.insert("file", String::from(record.file().unwrap_or("<file unknown>")));
data.insert("line", format!("{}", record.line().unwrap_or(0)));
data.insert("target", String::from(record.target()));
data.insert("message", format!("{}", record.args()));
}
let logtext = self
.handlebars
.render(&format!("{}", record.level()), &data)
.unwrap_or_else(|e| format!("Failed rendering logging data: {:?}\n", e));
let log_to_destination = |d: &LogDestination| match *d {
LogDestination::Stderr => {
let _ = writeln!(stderr(), "{}", logtext);
},
LogDestination::File(ref arc_mutex_logdest) => {
let _ = arc_mutex_logdest
.deref()
.lock()
.map(|mut logdest| {
writeln!(logdest, "{}", logtext)
});
}
};
let record_target = record
.target()
.split("::")
.next()
.unwrap_or("");
self.module_settings
.get(record_target)
.map(|module_setting| {
let set = module_setting.enabled &&
module_setting.level.unwrap_or(self.global_loglevel) >= record.level();
if set {
if let Some(destinations) = &module_setting.destinations {
for d in destinations {
log_to_destination(&d);
}
}
for d in self.global_destinations.iter() {
log_to_destination(&d);
}
}
})
.unwrap_or_else(|| {
if self.global_loglevel >= record.level() {
for d in self.global_destinations.iter() {
log_to_destination(&d);
}
}
});
}
}
fn match_log_level_str(s: &str) -> Result<Level> {
match s {
"trace" => Ok(Level::Trace),
"debug" => Ok(Level::Debug),
"info" => Ok(Level::Info),
"warn" => Ok(Level::Warn),
"error" => Ok(Level::Error),
lvl => Err(format_err!("Invalid logging level: {}", lvl)),
}
}
fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Value>) -> Result<Level>
{
fn get_arg_loglevel(matches: &ArgMatches) -> Result<Option<Level>> {
if matches.is_present("debugging") {
return Ok(Some(Level::Debug))
}
match matches.value_of("verbosity") {
Some(v) => match_log_level_str(v).map(Some),
None => if matches.is_present("verbosity") {
Ok(Some(Level::Info))
} else {
Ok(None)
},
}
}
if let Some(cfg) = config {
let cfg_loglevel = cfg
.read_string("imag.logging.level")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.ok_or_else(|| err_msg("Global log level config missing"))
.and_then(|s| match_log_level_str(&s))?;
if let Some(cli_loglevel) = get_arg_loglevel(matches)? {
if cli_loglevel > cfg_loglevel {
return Ok(cli_loglevel)
}
}
Ok(cfg_loglevel)
} else {
get_arg_loglevel(matches).map(|o| o.unwrap_or(Level::Info))
}
}
fn translate_destination(raw: &str) -> Result<LogDestination> {
use std::fs::OpenOptions;
match raw {
"-" => Ok(LogDestination::Stderr),
other => {
OpenOptions::new()
.append(true)
.create(true)
.open(other)
.map(Mutex::new)
.map(Arc::new)
.map(LogDestination::File)
.map_err(Error::from)
.context(EM::IO)
.map_err(Error::from)
}
}
}
fn translate_destinations(raw: &[Value]) -> Result<Vec<LogDestination>> {
raw.iter()
.map(|val| {
val.as_str()
.ok_or_else(|| "Type error at 'imag.logging.modules.<mod>.destinations', expected Array<String>")
.map_err(err_msg)
.map_err(Error::from)
.and_then(|s| translate_destination(s))
})
.collect()
}
fn aggregate_global_destinations(config: Option<&Value>)
-> Result<Vec<LogDestination>>
{
match config {
None => Ok(vec![LogDestination::default()]),
Some(cfg) => cfg
.read("imag.logging.destinations")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.ok_or_else(|| err_msg("Global log destination config missing"))?
.as_array()
.ok_or_else(|| {
let msg = "Type error at 'imag.logging.destinations', expected 'Array'";
err_msg(msg)
})
.and_then(|val| translate_destinations(val)),
}
}
mod log_lvl_aggregate {
use failure::Fallible as Result;
use failure::Error as E;
use failure::ResultExt;
use failure::err_msg;
use toml::Value;
use toml_query::read::TomlValueReadTypeExt;
use handlebars::Handlebars;
use libimagerror::errors::ErrorMsg as EM;
macro_rules! aggregate_global_format_with {
($t:ident, $read_str:expr) => {
pub struct $t;
impl LogLevelAggregator for $t {
fn aggregate(config: Option<&Value>) -> Result<String> {
config.ok_or_else(|| {
E::from(err_msg(concat!("Config missing: Logging format: ", stringify!($t))))
})?
.read_string($read_str)
.map_err(E::from)
.context(EM::TomlQueryError)?
.ok_or_else(|| {
E::from(err_msg(concat!("Config missing: Logging format: ", stringify!($t))))
})
}
}
};
}
pub trait LogLevelAggregator {
fn aggregate(config: Option<&Value>) -> Result<String>;
}
pub fn aggregate<T: LogLevelAggregator>(hb: &mut Handlebars, config: Option<&Value>, lvlstr: &str)
-> Result<()>
{
hb.register_template_string(lvlstr, T::aggregate(config)?)
.map_err(E::from)
.context(err_msg("Handlebars template error"))
.map_err(E::from)
}
aggregate_global_format_with!(Trace, "imag.logging.format.trace");
aggregate_global_format_with!(Debug, "imag.logging.format.debug");
aggregate_global_format_with!(Info, "imag.logging.format.info");
aggregate_global_format_with!(Warn, "imag.logging.format.warn");
aggregate_global_format_with!(Error, "imag.logging.format.error");
}
fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Value>)
-> Result<BTreeMap<ModuleName, ModuleSettings>>
{
use std::convert::TryInto;
#[derive(Serialize, Deserialize, Debug)]
struct LoggingModuleConfig {
pub destinations: Option<Vec<String>>,
pub level: Option<Level>,
pub enabled: bool,
}
#[derive(Partial, Serialize, Deserialize, Debug)]
#[location = "imag.logging.modules"]
struct LoggingModuleConfigMap(BTreeMap<String, LoggingModuleConfig>);
impl TryInto<BTreeMap<String, ModuleSettings>> for LoggingModuleConfigMap {
type Error = Error;
fn try_into(self) -> Result<BTreeMap<String, ModuleSettings>> {
let mut map = BTreeMap::new();
for (key, value) in self.0.into_iter() {
map.insert(key, ModuleSettings {
enabled: value.enabled,
level: value.level.map(Into::into),
destinations: match value.destinations {
None => None,
Some(ds) => Some(ds
.iter()
.map(Deref::deref)
.map(translate_destination)
.collect::<Result<Vec<LogDestination>>>()?)
},
});
}
Ok(map)
}
}
match config {
Some(cfg) => cfg.read_partial::<LoggingModuleConfigMap>()?
.ok_or_else(|| err_msg("Logging configuration missing"))?
.try_into()
.map_err(Error::from),
None => {
write!(stderr(), "No Configuration.").ok();
write!(stderr(), "cannot find module-settings for logging.").ok();
write!(stderr(), "Will use global defaults").ok();
Ok(BTreeMap::new())
}
}
}