From 09f09687551ff836656d3c59d6fc21e77b2c08ee Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 6 Nov 2018 19:39:41 +0100 Subject: Remove repetitive code by abstracting it With this patch, the log level aggregation is abstracted away by using zero sized types and a macro to implement the whole thing. This does not alter functionality, but makes the code more readable. Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/logger.rs | 123 ++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 72 deletions(-) (limited to 'lib/core/libimagrt') diff --git a/lib/core/libimagrt/src/logger.rs b/lib/core/libimagrt/src/logger.rs index 4fd773a6..4d493a2e 100644 --- a/lib/core/libimagrt/src/logger.rs +++ b/lib/core/libimagrt/src/logger.rs @@ -88,34 +88,12 @@ impl ImagLogger { ::libimaginteraction::format::register_all_format_helpers(&mut handlebars); { - let fmt = aggregate_global_format_trace(config)?; - handlebars.register_template_string("TRACE", fmt) - .map_err(Error::from) - .context(err_msg("Handlebars template error"))?; // name must be uppercase - } - { - let fmt = aggregate_global_format_debug(config)?; - handlebars.register_template_string("DEBUG", fmt) - .map_err(Error::from) - .context(err_msg("Handlebars template error"))?; // name must be uppercase - } - { - let fmt = aggregate_global_format_info(config)?; - handlebars.register_template_string("INFO", fmt) - .map_err(Error::from) - .context(err_msg("Handlebars template error"))?; // name must be uppercase - } - { - let fmt = aggregate_global_format_warn(config)?; - handlebars.register_template_string("WARN", fmt) - .map_err(Error::from) - .context(err_msg("Handlebars template error"))?; // name must be uppercase - } - { - let fmt = aggregate_global_format_error(config)?; - handlebars.register_template_string("ERROR", fmt) - .map_err(Error::from) - .context(err_msg("Handlebars template error"))?; // name must be uppercase + use self::log_lvl_aggregate::*; + let _ = aggregate::(&mut handlebars, config, "TRACE")?; + let _ = aggregate::(&mut handlebars, config, "DEBUG")?; + let _ = aggregate::(&mut handlebars, config, "INFO")?; + let _ = aggregate::(&mut handlebars, config, "WARN")?; + let _ = aggregate::(&mut handlebars, config, "ERROR")?; } Ok(ImagLogger { @@ -335,54 +313,55 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Value>) } } -macro_rules! aggregate_global_format { - ($read_str:expr, $error_msg_if_missing:expr, $config:expr) => { - try!($config.ok_or_else(|| Error::from(err_msg($error_msg_if_missing)))) - .read_string($read_str) - .map_err(Error::from) - .context(EM::TomlQueryError)? - .ok_or_else(|| Error::from(err_msg($error_msg_if_missing))) - }; -} - -fn aggregate_global_format_trace(config: Option<&Value>) - -> Result -{ - aggregate_global_format!("imag.logging.format.trace", - "Config missing: Logging format: Trace", - config) -} +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 { + 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)))) + }) + } + } + }; + } -fn aggregate_global_format_debug(config: Option<&Value>) - -> Result -{ - aggregate_global_format!("imag.logging.format.debug", - "Config missing: Logging format: Debug", - config) -} + pub trait LogLevelAggregator { + fn aggregate(config: Option<&Value>) -> Result; + } -fn aggregate_global_format_info(config: Option<&Value>) - -> Result -{ - aggregate_global_format!("imag.logging.format.info", - "Config missing: Logging format: Info", - config) -} + pub fn aggregate(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) + } -fn aggregate_global_format_warn(config: Option<&Value>) - -> Result -{ - aggregate_global_format!("imag.logging.format.warn", - "Config missing: Logging format: Warn", - config) -} + 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_global_format_error(config: Option<&Value>) - -> Result -{ - aggregate_global_format!("imag.logging.format.error", - "Config missing: Logging format: Error", - config) } fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Value>) -- cgit v1.2.3