From 64f96092cd3b61d1b104fa764499670b80439ab2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Aug 2017 11:40:50 +0200 Subject: [PATCH] Fix aggregation --- lib/core/libimagrt/src/error.rs | 1 + lib/core/libimagrt/src/logger.rs | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/core/libimagrt/src/error.rs b/lib/core/libimagrt/src/error.rs index 9db8666f..ba2a34bc 100644 --- a/lib/core/libimagrt/src/error.rs +++ b/lib/core/libimagrt/src/error.rs @@ -28,6 +28,7 @@ generate_error_types!(RuntimeError, RuntimeErrorKind, ConfigReadError => "Error while reading the configuration", ConfigTypeError => "Error while reading the configuration: Type Error", GlobalLogLevelConfigMissing => "Global config 'imag.logging.level' missing", + GlobalDestinationConfigMissing => "Global config 'imag.logging.destinations' missing", InvalidLogLevelSpec => "Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed", TomlReadError => "Error while reading in TOML document", TemplateStringRegistrationError => "Error while registering logging template string", diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index 788af9cc..a29faf46 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -50,11 +50,11 @@ impl Default for LogDestination { } struct ModuleSettings { - enabled: bool, - level: LogLevel, + enabled: bool, + level: Option, #[allow(unused)] - destinations: Vec, + destinations: Option>, } /// Logger implementation for `log` crate. @@ -156,7 +156,7 @@ impl Log for ImagLogger { self.module_settings .get(log_target) .map(|module_setting| { - if module_setting.enabled && module_setting.level >= log_level { + if module_setting.enabled && module_setting.level.unwrap_or(self.global_loglevel) >= log_level { let _ = write!(stderr(), "{}\n", logtext); } }) @@ -248,7 +248,7 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Configura { Ok(Some(&Value::Array(ref a))) => translate_destinations(a), Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), + Ok(None) => Err(EK::GlobalDestinationConfigMissing.into_error()), Err(e) => Err(e) }, None => { @@ -286,7 +286,7 @@ fn aggregate_global_format( { Ok(Some(&Value::String(ref s))) => Ok(s.clone()), Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), + Ok(None) => Err(error_kind_if_missing.into_error()), Err(e) => Err(e) }, None => match matches.value_of(cli_match_name).map(String::from) { @@ -360,23 +360,23 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio for (module_name, v) in t { let destinations = try!(match v.read("destinations") { - Ok(Some(&Value::Array(ref a))) => translate_destinations(a), + Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some), + Ok(None) => Ok(None), Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), Err(e) => Err(e).map_err_into(EK::TomlReadError), }); let level = try!(match v.read("level") { - Ok(Some(&Value::String(ref s))) => match_log_level_str(s), + Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some), + Ok(None) => Ok(None), Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), Err(e) => Err(e).map_err_into(EK::TomlReadError), }); let enabled = try!(match v.read("enabled") { Ok(Some(&Value::Boolean(b))) => Ok(b), + Ok(None) => Ok(false), Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), Err(e) => Err(e).map_err_into(EK::TomlReadError), }); @@ -393,7 +393,10 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio Ok(settings) }, Ok(Some(_)) => Err(EK::ConfigTypeError.into_error()), - Ok(None) => Err(EK::GlobalLogLevelConfigMissing.into_error()), + Ok(None) => { + // No modules configured. This is okay! + Ok(BTreeMap::new()) + }, Err(e) => Err(e), }, None => {