2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-02-04 17:02:50 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::io::stderr;
|
2017-08-26 13:27:13 +00:00
|
|
|
use std::collections::BTreeMap;
|
2017-10-08 16:06:43 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
use std::ops::Deref;
|
2016-02-04 17:02:50 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
use runtime::Runtime;
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-08-26 13:27:13 +00:00
|
|
|
use clap::ArgMatches;
|
2017-12-06 17:08:25 +00:00
|
|
|
use log::{Log, Level, Record, Metadata};
|
2017-08-26 13:27:13 +00:00
|
|
|
use toml::Value;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
2018-01-12 15:31:13 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-08-26 20:49:30 +00:00
|
|
|
use handlebars::Handlebars;
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
type ModuleName = String;
|
|
|
|
|
|
|
|
enum LogDestination {
|
|
|
|
Stderr,
|
2017-10-08 16:06:43 +00:00
|
|
|
File(Arc<Mutex<::std::fs::File>>),
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LogDestination {
|
|
|
|
fn default() -> LogDestination {
|
|
|
|
LogDestination::Stderr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ModuleSettings {
|
2017-08-28 09:40:50 +00:00
|
|
|
enabled: bool,
|
2017-12-06 17:08:25 +00:00
|
|
|
level: Option<Level>,
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
#[allow(unused)]
|
2017-08-28 09:40:50 +00:00
|
|
|
destinations: Option<Vec<LogDestination>>,
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
2016-08-02 09:38:38 +00:00
|
|
|
|
2017-02-21 14:26:10 +00:00
|
|
|
/// Logger implementation for `log` crate.
|
2016-01-10 14:04:06 +00:00
|
|
|
pub struct ImagLogger {
|
2017-12-06 17:08:25 +00:00
|
|
|
global_loglevel : Level,
|
2017-08-26 13:27:13 +00:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
global_destinations : Vec<LogDestination>,
|
|
|
|
// global_format_trace : ,
|
|
|
|
// global_format_debug : ,
|
|
|
|
// global_format_info : ,
|
|
|
|
// global_format_warn : ,
|
|
|
|
// global_format_error : ,
|
|
|
|
module_settings : BTreeMap<ModuleName, ModuleSettings>,
|
2017-08-26 20:49:30 +00:00
|
|
|
|
|
|
|
handlebars: Handlebars,
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImagLogger {
|
|
|
|
|
2017-02-21 14:26:10 +00:00
|
|
|
/// Create a new ImagLogger object with a certain level
|
2017-10-28 10:50:22 +00:00
|
|
|
pub fn new(matches: &ArgMatches, config: Option<&Value>) -> Result<ImagLogger> {
|
2017-08-26 20:49:30 +00:00
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
2017-09-20 17:12:06 +00:00
|
|
|
handlebars.register_escape_fn(::handlebars::no_escape);
|
|
|
|
|
2017-10-12 14:55:03 +00:00
|
|
|
::libimaginteraction::format::register_all_color_helpers(&mut handlebars);
|
|
|
|
::libimaginteraction::format::register_all_format_helpers(&mut handlebars);
|
2017-08-30 19:16:31 +00:00
|
|
|
|
2017-08-26 20:49:30 +00:00
|
|
|
{
|
2018-11-06 18:39:41 +00:00
|
|
|
use self::log_lvl_aggregate::*;
|
|
|
|
let _ = aggregate::<Trace>(&mut handlebars, config, "TRACE")?;
|
|
|
|
let _ = aggregate::<Debug>(&mut handlebars, config, "DEBUG")?;
|
|
|
|
let _ = aggregate::<Info>(&mut handlebars, config, "INFO")?;
|
|
|
|
let _ = aggregate::<Warn>(&mut handlebars, config, "WARN")?;
|
|
|
|
let _ = aggregate::<Error>(&mut handlebars, config, "ERROR")?;
|
2017-08-26 20:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
Ok(ImagLogger {
|
2017-10-21 14:17:35 +00:00
|
|
|
global_loglevel : aggregate_global_loglevel(matches, config)?,
|
|
|
|
global_destinations : aggregate_global_destinations(matches, config)?,
|
2018-10-30 17:40:50 +00:00
|
|
|
module_settings : aggregate_module_settings(matches, config)?,
|
2017-08-26 20:49:30 +00:00
|
|
|
handlebars : handlebars,
|
2017-08-26 13:27:13 +00:00
|
|
|
})
|
2016-08-02 09:38:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:08:25 +00:00
|
|
|
pub fn global_loglevel(&self) -> Level {
|
2017-08-26 13:27:13 +00:00
|
|
|
self.global_loglevel
|
2016-08-02 09:38:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Log for ImagLogger {
|
|
|
|
|
2017-12-06 17:08:25 +00:00
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
2017-08-26 13:27:13 +00:00
|
|
|
metadata.level() <= self.global_loglevel
|
2017-01-21 11:56:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:08:25 +00:00
|
|
|
fn flush(&self) {
|
|
|
|
// nothing?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &Record) {
|
2017-08-26 20:49:30 +00:00
|
|
|
let mut data = BTreeMap::new();
|
2017-08-28 10:09:23 +00:00
|
|
|
|
2017-08-26 20:49:30 +00:00
|
|
|
{
|
2017-08-28 10:09:23 +00:00
|
|
|
data.insert("level", format!("{}", record.level()));
|
2017-12-06 17:08:25 +00:00
|
|
|
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)));
|
2017-08-28 10:09:23 +00:00
|
|
|
data.insert("target", String::from(record.target()));
|
|
|
|
data.insert("message", format!("{}", record.args()));
|
2017-08-26 20:49:30 +00:00
|
|
|
}
|
2017-08-28 10:09:23 +00:00
|
|
|
|
2017-08-26 20:49:30 +00:00
|
|
|
let logtext = self
|
|
|
|
.handlebars
|
2017-08-28 10:09:23 +00:00
|
|
|
.render(&format!("{}", record.level()), &data)
|
2017-08-26 20:49:30 +00:00
|
|
|
.unwrap_or_else(|e| format!("Failed rendering logging data: {:?}\n", e));
|
|
|
|
|
2017-10-08 16:06:43 +00:00
|
|
|
let log_to_destination = |d: &LogDestination| match d {
|
|
|
|
&LogDestination::Stderr => {
|
|
|
|
let _ = write!(stderr(), "{}\n", logtext);
|
|
|
|
},
|
|
|
|
&LogDestination::File(ref arc_mutex_logdest) => {
|
|
|
|
// if there is an error in the lock, we cannot do anything. So we ignore it here.
|
|
|
|
let _ = arc_mutex_logdest
|
|
|
|
.deref()
|
|
|
|
.lock()
|
|
|
|
.map(|mut logdest| {
|
|
|
|
write!(logdest, "{}\n", logtext)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hack to get the right target configuration.
|
|
|
|
// If there is no element here, we use the empty string which automatically drops through
|
|
|
|
// to the unwrap_or_else() case
|
|
|
|
let record_target = record
|
|
|
|
.target()
|
|
|
|
.split("::")
|
|
|
|
.next()
|
|
|
|
.unwrap_or("");
|
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
self.module_settings
|
2017-10-08 16:06:43 +00:00
|
|
|
.get(record_target)
|
2017-08-26 13:27:13 +00:00
|
|
|
.map(|module_setting| {
|
2017-08-28 10:09:23 +00:00
|
|
|
let set = module_setting.enabled &&
|
|
|
|
module_setting.level.unwrap_or(self.global_loglevel) >= record.level();
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
if set {
|
2017-10-08 16:06:43 +00:00
|
|
|
module_setting.destinations.as_ref().map(|destinations| for d in destinations {
|
2018-10-30 17:40:50 +00:00
|
|
|
// If there's an error, we cannot do anything, can we?
|
|
|
|
let _ = log_to_destination(&d);
|
2017-10-08 16:06:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for d in self.global_destinations.iter() {
|
2018-10-30 17:40:50 +00:00
|
|
|
// If there's an error, we cannot do anything, can we?
|
|
|
|
let _ = log_to_destination(&d);
|
2017-10-08 16:06:43 +00:00
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-08 16:06:43 +00:00
|
|
|
.unwrap_or_else(|| {
|
2018-10-30 17:40:50 +00:00
|
|
|
if self.global_loglevel >= record.level() {
|
|
|
|
// Yes, we log
|
|
|
|
for d in self.global_destinations.iter() {
|
|
|
|
// If there's an error, we cannot do anything, can we?
|
|
|
|
let _ = log_to_destination(&d);
|
|
|
|
}
|
2017-10-08 16:06:43 +00:00
|
|
|
}
|
|
|
|
});
|
2017-01-21 11:37:11 +00:00
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
2017-01-21 11:37:11 +00:00
|
|
|
|
2017-12-06 17:08:25 +00:00
|
|
|
fn match_log_level_str(s: &str) -> Result<Level> {
|
2017-08-26 13:27:13 +00:00
|
|
|
match s {
|
2017-12-06 17:08:25 +00:00
|
|
|
"trace" => Ok(Level::Trace),
|
|
|
|
"debug" => Ok(Level::Debug),
|
|
|
|
"info" => Ok(Level::Info),
|
|
|
|
"warn" => Ok(Level::Warn),
|
|
|
|
"error" => Ok(Level::Error),
|
2018-10-30 17:40:50 +00:00
|
|
|
lvl => return Err(format_err!("Invalid logging level: {}", lvl)),
|
2016-08-02 09:38:38 +00:00
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
2016-08-02 09:38:38 +00:00
|
|
|
|
2017-12-06 17:08:25 +00:00
|
|
|
fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Value>) -> Result<Level>
|
2017-08-26 13:27:13 +00:00
|
|
|
{
|
2017-12-06 17:08:25 +00:00
|
|
|
fn get_arg_loglevel(matches: &ArgMatches) -> Result<Option<Level>> {
|
2017-10-15 17:45:34 +00:00
|
|
|
if matches.is_present(Runtime::arg_debugging_name()) {
|
2017-12-06 17:08:25 +00:00
|
|
|
return Ok(Some(Level::Debug))
|
2017-10-15 17:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match matches.value_of(Runtime::arg_verbosity_name()) {
|
|
|
|
Some(v) => match_log_level_str(v).map(Some),
|
2018-04-14 19:25:57 +00:00
|
|
|
None => if matches.is_present(Runtime::arg_verbosity_name()) {
|
|
|
|
Ok(Some(Level::Info))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
},
|
2017-10-15 17:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(cfg) = config {
|
2018-01-04 22:08:14 +00:00
|
|
|
let cfg_loglevel = cfg
|
2018-10-30 17:40:50 +00:00
|
|
|
.read_string("imag.logging.level")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::TomlQueryError)?
|
|
|
|
.ok_or(err_msg("Global log level config missing"))
|
2018-01-12 15:31:13 +00:00
|
|
|
.and_then(|s| match_log_level_str(&s))?;
|
2016-08-02 09:38:38 +00:00
|
|
|
|
2017-10-15 17:45:34 +00:00
|
|
|
if let Some(cli_loglevel) = get_arg_loglevel(matches)? {
|
|
|
|
if cli_loglevel > cfg_loglevel {
|
|
|
|
return Ok(cli_loglevel)
|
|
|
|
}
|
2016-08-02 09:38:38 +00:00
|
|
|
}
|
2017-10-15 17:45:34 +00:00
|
|
|
|
|
|
|
Ok(cfg_loglevel)
|
|
|
|
|
|
|
|
} else {
|
2017-12-06 17:08:25 +00:00
|
|
|
get_arg_loglevel(matches).map(|o| o.unwrap_or(Level::Info))
|
2016-08-02 09:38:38 +00:00
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn translate_destination(raw: &str) -> Result<LogDestination> {
|
|
|
|
use std::fs::OpenOptions;
|
2016-08-02 09:38:38 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
match raw {
|
|
|
|
"-" => Ok(LogDestination::Stderr),
|
|
|
|
other => {
|
|
|
|
OpenOptions::new()
|
|
|
|
.append(true)
|
|
|
|
.create(true)
|
|
|
|
.open(other)
|
2017-10-08 16:06:43 +00:00
|
|
|
.map(Mutex::new)
|
|
|
|
.map(Arc::new)
|
2017-08-26 13:27:13 +00:00
|
|
|
.map(LogDestination::File)
|
2018-10-30 17:40:50 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::IO)
|
|
|
|
.map_err(Error::from)
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
fn translate_destinations(raw: &Vec<Value>) -> Result<Vec<LogDestination>> {
|
|
|
|
raw.iter()
|
|
|
|
.fold(Ok(vec![]), |acc, val| {
|
|
|
|
acc.and_then(|mut v| {
|
2018-01-04 22:08:14 +00:00
|
|
|
let dest = val.as_str()
|
|
|
|
.ok_or_else(|| {
|
2018-11-06 18:45:50 +00:00
|
|
|
let msg = "Type error at 'imag.logging.modules.<mod>.destinations', expected Array<String>";
|
|
|
|
Error::from(err_msg(msg))
|
2018-01-04 22:08:14 +00:00
|
|
|
})
|
|
|
|
.and_then(translate_destination)?;
|
2017-08-26 13:27:13 +00:00
|
|
|
v.push(dest);
|
|
|
|
Ok(v)
|
|
|
|
})
|
|
|
|
})
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 10:50:22 +00:00
|
|
|
fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Value>)
|
2017-08-26 13:27:13 +00:00
|
|
|
-> Result<Vec<LogDestination>>
|
|
|
|
{
|
2018-10-30 17:40:50 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
match config {
|
2018-01-04 22:08:14 +00:00
|
|
|
Some(cfg) => cfg
|
2018-10-30 17:40:50 +00:00
|
|
|
.read("imag.logging.destinations")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::TomlQueryError)?
|
|
|
|
.ok_or_else(|| err_msg("Global log destination config missing"))?
|
2018-01-04 22:08:14 +00:00
|
|
|
.as_array()
|
|
|
|
.ok_or_else(|| {
|
2018-11-06 18:45:50 +00:00
|
|
|
let msg = "Type error at 'imag.logging.destinations', expected 'Array'";
|
|
|
|
Error::from(err_msg(msg))
|
2018-01-04 22:08:14 +00:00
|
|
|
})
|
|
|
|
.and_then(translate_destinations),
|
2017-08-26 13:27:13 +00:00
|
|
|
None => {
|
|
|
|
if let Some(values) = matches.value_of(Runtime::arg_logdest_name()) {
|
|
|
|
// parse logdest specification from commandline
|
|
|
|
|
|
|
|
values.split(",")
|
|
|
|
.fold(Ok(vec![]), move |acc, dest| {
|
|
|
|
acc.and_then(|mut v| {
|
2017-10-21 14:17:35 +00:00
|
|
|
v.push(translate_destination(dest)?);
|
2017-08-26 13:27:13 +00:00
|
|
|
Ok(v)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(vec![ LogDestination::default() ])
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
2017-08-26 13:27:13 +00:00
|
|
|
}
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2018-11-06 18:39:41 +00:00
|
|
|
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))))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2018-11-06 18:39:41 +00:00
|
|
|
pub trait LogLevelAggregator {
|
|
|
|
fn aggregate(config: Option<&Value>) -> Result<String>;
|
|
|
|
}
|
2017-08-26 20:49:30 +00:00
|
|
|
|
2018-11-06 18:39:41 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-08-26 20:49:30 +00:00
|
|
|
|
2018-11-06 18:39:41 +00:00
|
|
|
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");
|
2017-08-26 20:49:30 +00:00
|
|
|
|
|
|
|
}
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Value>)
|
2017-08-26 13:27:13 +00:00
|
|
|
-> Result<BTreeMap<ModuleName, ModuleSettings>>
|
|
|
|
{
|
2018-01-04 22:08:14 +00:00
|
|
|
// Helper macro to return the error from Some(Err(_)) and map everything else to an
|
|
|
|
// Option<_>
|
|
|
|
macro_rules! inner_try {
|
|
|
|
($v:expr) => {
|
|
|
|
match $v {
|
|
|
|
Some(Ok(v)) => Some(v),
|
|
|
|
Some(Err(e)) => return Err(e),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
match config {
|
2018-10-30 17:40:50 +00:00
|
|
|
Some(cfg) => match cfg.read("imag.logging.modules").map_err(Error::from) {
|
2017-09-09 20:15:18 +00:00
|
|
|
Ok(Some(&Value::Table(ref t))) => {
|
|
|
|
// translate the module settings from the table `t`
|
|
|
|
let mut settings = BTreeMap::new();
|
|
|
|
|
|
|
|
for (module_name, v) in t {
|
2018-01-04 22:08:14 +00:00
|
|
|
let destinations = inner_try! {
|
2018-10-30 17:40:50 +00:00
|
|
|
v.read("destinations")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::TomlQueryError)?
|
2018-01-04 22:08:14 +00:00
|
|
|
.map(|val| {
|
|
|
|
val.as_array()
|
|
|
|
.ok_or_else(|| {
|
2018-11-06 18:45:50 +00:00
|
|
|
let msg = "Type error at 'imag.logging.modules.<mod>.destinations', expected 'Array'";
|
|
|
|
Error::from(err_msg(msg))
|
2018-01-04 22:08:14 +00:00
|
|
|
})
|
|
|
|
.and_then(translate_destinations)
|
|
|
|
})
|
2017-10-21 14:45:57 +00:00
|
|
|
};
|
2017-09-09 20:15:18 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
let level = inner_try! {
|
|
|
|
v.read_string("level")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::TomlQueryError)?
|
|
|
|
.map(|s| match_log_level_str(&s))
|
2017-10-21 14:45:57 +00:00
|
|
|
};
|
2017-09-09 20:15:18 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
let enabled = v.read("enabled")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::TomlQueryError)?
|
|
|
|
.map(|v| v.as_bool().unwrap_or(false))
|
|
|
|
.ok_or_else(|| {
|
2018-11-06 18:45:50 +00:00
|
|
|
let msg = "Type error at 'imag.logging.modules.<mod>.enabled', expected 'Boolean'";
|
|
|
|
Error::from(err_msg(msg))
|
2018-10-30 17:40:50 +00:00
|
|
|
})?;
|
2017-09-09 20:15:18 +00:00
|
|
|
|
|
|
|
let module_settings = ModuleSettings {
|
|
|
|
enabled: enabled,
|
|
|
|
level: level,
|
|
|
|
destinations: destinations,
|
|
|
|
};
|
|
|
|
|
|
|
|
// We don't care whether there was a value, we override it.
|
|
|
|
let _ = settings.insert(module_name.to_owned(), module_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(settings)
|
|
|
|
},
|
2017-09-09 20:24:58 +00:00
|
|
|
Ok(Some(_)) => {
|
2018-11-06 18:45:50 +00:00
|
|
|
let msg = "Type error at 'imag.logging.modules', expected 'Table'";
|
|
|
|
Err(Error::from(err_msg(msg)))
|
2017-09-09 20:24:58 +00:00
|
|
|
},
|
2017-09-09 20:15:18 +00:00
|
|
|
Ok(None) => {
|
|
|
|
// No modules configured. This is okay!
|
|
|
|
Ok(BTreeMap::new())
|
2017-08-26 13:27:13 +00:00
|
|
|
},
|
2018-10-30 17:40:50 +00:00
|
|
|
Err(e) => Err(e).context(EM::TomlQueryError).map_err(Error::from),
|
2017-09-09 20:15:18 +00:00
|
|
|
},
|
2017-08-26 13:27:13 +00:00
|
|
|
None => {
|
|
|
|
write!(stderr(), "No Configuration.").ok();
|
|
|
|
write!(stderr(), "cannot find module-settings for logging.").ok();
|
|
|
|
write!(stderr(), "Will use global defaults").ok();
|
2016-05-15 15:38:02 +00:00
|
|
|
|
2017-08-26 13:27:13 +00:00
|
|
|
Ok(BTreeMap::new())
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|