Merge branch 'rewrite-logging-aggregation' into master

This commit is contained in:
Matthias Beyer 2019-05-30 10:08:45 +02:00
commit c27fec14bf
4 changed files with 58 additions and 74 deletions

View file

@ -16,7 +16,7 @@ matrix:
- bash ./scripts/branch-contains-no-tmp-commits - bash ./scripts/branch-contains-no-tmp-commits
- bash ./scripts/version-updated - bash ./scripts/version-updated
- language: rust - language: rust
rust: 1.33.0 rust: 1.34.0
cache: cache:
directories: directories:
- /home/travis/.cargo - /home/travis/.cargo
@ -26,7 +26,7 @@ matrix:
- cargo build --all --all-features -j 1 || exit 1 - cargo build --all --all-features -j 1 || exit 1
- cargo test --all --all-features -j 1 || exit 1 - cargo test --all --all-features -j 1 || exit 1
- language: rust - language: rust
rust: 1.34.2 rust: 1.35.0
cache: cache:
directories: directories:
- /home/travis/.cargo - /home/travis/.cargo

View file

@ -29,6 +29,8 @@ toml-query = "0.9"
atty = "0.2" atty = "0.2"
failure = "0.1" failure = "0.1"
failure_derive = "0.1" failure_derive = "0.1"
serde_derive = "1"
serde = "1"
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
@ -43,7 +45,7 @@ features = ["suggestions", "color", "wrap_help"]
[dependencies.log] [dependencies.log]
version = "0.4" version = "0.4"
default-features = false default-features = false
features = ["std"] features = ["std", "serde"]
[dependencies.handlebars] [dependencies.handlebars]
version = "^1.0.5" version = "^1.0.5"

View file

@ -37,15 +37,17 @@
while_true, while_true,
)] )]
#[macro_use] extern crate log; extern crate log;
extern crate itertools; extern crate itertools;
extern crate ansi_term; extern crate ansi_term;
extern crate handlebars; extern crate handlebars;
extern crate serde;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate failure; #[macro_use] extern crate failure;
#[macro_use] extern crate toml_query;
extern crate clap; extern crate clap;
extern crate toml; extern crate toml;
extern crate toml_query;
extern crate atty; extern crate atty;
extern crate libimagstore; extern crate libimagstore;

View file

@ -349,79 +349,59 @@ mod log_lvl_aggregate {
fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Value>) fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Value>)
-> Result<BTreeMap<ModuleName, ModuleSettings>> -> Result<BTreeMap<ModuleName, ModuleSettings>>
{ {
// Helper macro to return the error from Some(Err(_)) and map everything else to an use std::convert::TryInto;
// Option<_>
macro_rules! inner_try { //
($v:expr) => { // We define helper types here for deserializing easily using typed toml-query functionality.
match $v { //
Some(Ok(v)) => Some(v), // We need the helper types because we cannot deserialize in the target types directly, because
Some(Err(e)) => return Err(e), // of the `File(Arc<Mutex<::std::fs::File>>)` variant in `LogDestination`, which would
None => None, // technically possible to deserialize the toml into the type, but it might be a bad idea.
//
// This code is idomatic enough for the conversions, so it is not a big painpoint.
//
#[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) // This is why we do this whole thing
.collect::<Result<Vec<LogDestination>>>()?)
},
});
} }
Ok(map)
} }
}; }
match config { match config {
Some(cfg) => match cfg.read("imag.logging.modules").map_err(Error::from) { Some(cfg) => cfg.read_partial::<LoggingModuleConfigMap>()?
Ok(Some(&Value::Table(ref t))) => { .ok_or_else(|| err_msg("Logging configuration missing"))?
// translate the module settings from the table `t` .try_into()
let mut settings = BTreeMap::new(); .map_err(Error::from),
for (module_name, v) in t {
let destinations = inner_try! {
v.read("destinations")
.context("Failed reading header 'destinations'")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.map(|val| {
val.as_array()
.ok_or_else(|| {
let msg = "Type error at 'imag.logging.modules.<mod>.destinations', expected 'Array'";
Error::from(err_msg(msg))
})
.and_then(translate_destinations)
})
};
let level = inner_try! {
v.read_string("level")
.context("Failed reading header 'level'")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.map(|s| match_log_level_str(&s))
};
let enabled = v.read("enabled")
.map_err(Error::from)
.context(EM::TomlQueryError)?
.map(|v| v.as_bool().unwrap_or(false))
.ok_or_else(|| {
let msg = "Type error at 'imag.logging.modules.<mod>.enabled', expected 'Boolean'";
Error::from(err_msg(msg))
})?;
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)
},
Ok(Some(_)) => {
let msg = "Type error at 'imag.logging.modules', expected 'Table'";
Err(Error::from(err_msg(msg)))
},
Ok(None) => {
// No modules configured. This is okay!
Ok(BTreeMap::new())
},
Err(e) => Err(e).context(EM::TomlQueryError).map_err(Error::from),
},
None => { None => {
write!(stderr(), "No Configuration.").ok(); write!(stderr(), "No Configuration.").ok();
write!(stderr(), "cannot find module-settings for logging.").ok(); write!(stderr(), "cannot find module-settings for logging.").ok();