Merge pull request #1063 from matthiasbeyer/libimagrt/error-opt
libimagrt: error optimizations
This commit is contained in:
commit
baf8eda571
2 changed files with 96 additions and 95 deletions
|
@ -22,6 +22,11 @@ error_chain! {
|
||||||
RuntimeError, RuntimeErrorKind, ResultExt, Result;
|
RuntimeError, RuntimeErrorKind, ResultExt, Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreign_links {
|
||||||
|
TomlError(::toml_query::error::Error);
|
||||||
|
HandlebarsTemplateError(::handlebars::TemplateError);
|
||||||
|
}
|
||||||
|
|
||||||
errors {
|
errors {
|
||||||
Instantiate {
|
Instantiate {
|
||||||
description("Could not instantiate")
|
description("Could not instantiate")
|
||||||
|
@ -43,14 +48,9 @@ error_chain! {
|
||||||
display("IO Error: Could not open logfile")
|
display("IO Error: Could not open logfile")
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigReadError {
|
ConfigTypeError(path: String, should_be_type: &'static str) {
|
||||||
description("Error while reading the configuration")
|
|
||||||
display("Error while reading the configuration")
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigTypeError {
|
|
||||||
description("Error while reading the configuration: Type Error")
|
description("Error while reading the configuration: Type Error")
|
||||||
display("Error while reading the configuration: Type Error")
|
display("Type Error: '{}' should be '{}'", path, should_be_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalLogLevelConfigMissing {
|
GlobalLogLevelConfigMissing {
|
||||||
|
@ -68,16 +68,6 @@ error_chain! {
|
||||||
display("Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed")
|
display("Invalid log level specification: Only 'trace', 'debug', 'info', 'warn', 'error' are allowed")
|
||||||
}
|
}
|
||||||
|
|
||||||
TomlReadError {
|
|
||||||
description("Error while reading in TOML document")
|
|
||||||
display("Error while reading in TOML document")
|
|
||||||
}
|
|
||||||
|
|
||||||
TemplateStringRegistrationError {
|
|
||||||
description("Error while registering logging template string")
|
|
||||||
display("Error while registering logging template string")
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigMissingLoggingFormatTrace {
|
ConfigMissingLoggingFormatTrace {
|
||||||
description("Missing config for logging format for trace logging")
|
description("Missing config for logging format for trace logging")
|
||||||
display("Missing config for logging format for trace logging")
|
display("Missing config for logging format for trace logging")
|
||||||
|
|
|
@ -93,28 +93,23 @@ impl ImagLogger {
|
||||||
|
|
||||||
{
|
{
|
||||||
let fmt = try!(aggregate_global_format_trace(matches, config));
|
let fmt = try!(aggregate_global_format_trace(matches, config));
|
||||||
try!(handlebars.register_template_string("TRACE", fmt) // name must be uppercase
|
try!(handlebars.register_template_string("TRACE", fmt)); // name must be uppercase
|
||||||
.chain_err(|| EK::TemplateStringRegistrationError));
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let fmt = try!(aggregate_global_format_debug(matches, config));
|
let fmt = try!(aggregate_global_format_debug(matches, config));
|
||||||
try!(handlebars.register_template_string("DEBUG", fmt) // name must be uppercase
|
try!(handlebars.register_template_string("DEBUG", fmt)); // name must be uppercase
|
||||||
.chain_err(|| EK::TemplateStringRegistrationError));
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let fmt = try!(aggregate_global_format_info(matches, config));
|
let fmt = try!(aggregate_global_format_info(matches, config));
|
||||||
try!(handlebars.register_template_string("INFO", fmt) // name must be uppercase
|
try!(handlebars.register_template_string("INFO", fmt)); // name must be uppercase
|
||||||
.chain_err(|| EK::TemplateStringRegistrationError));
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let fmt = try!(aggregate_global_format_warn(matches, config));
|
let fmt = try!(aggregate_global_format_warn(matches, config));
|
||||||
try!(handlebars.register_template_string("WARN", fmt) // name must be uppercase
|
try!(handlebars.register_template_string("WARN", fmt)); // name must be uppercase
|
||||||
.chain_err(|| EK::TemplateStringRegistrationError));
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let fmt = try!(aggregate_global_format_error(matches, config));
|
let fmt = try!(aggregate_global_format_error(matches, config));
|
||||||
try!(handlebars.register_template_string("ERROR", fmt) // name must be uppercase
|
try!(handlebars.register_template_string("ERROR", fmt)); // name must be uppercase
|
||||||
.chain_err(|| EK::TemplateStringRegistrationError));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ImagLogger {
|
Ok(ImagLogger {
|
||||||
|
@ -200,15 +195,16 @@ fn aggregate_global_loglevel(matches: &ArgMatches, config: Option<&Configuration
|
||||||
-> Result<LogLevel>
|
-> Result<LogLevel>
|
||||||
{
|
{
|
||||||
match config {
|
match config {
|
||||||
Some(cfg) => match cfg
|
Some(cfg) => match cfg.read("imag.logging.level") {
|
||||||
.read("imag.logging.level")
|
Ok(Some(&Value::String(ref s))) => match_log_level_str(s),
|
||||||
.chain_err(|| EK::ConfigReadError)
|
Ok(Some(_)) => {
|
||||||
{
|
let path = "imag.logging.level".to_owned();
|
||||||
Ok(Some(&Value::String(ref s))) => match_log_level_str(s),
|
let ty = "String";
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)),
|
|
||||||
Err(e) => Err(e)
|
|
||||||
},
|
},
|
||||||
|
Ok(None) => Err(RE::from_kind(EK::GlobalLogLevelConfigMissing)),
|
||||||
|
Err(e) => Err(e).map_err(From::from),
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
if matches.is_present(Runtime::arg_debugging_name()) {
|
if matches.is_present(Runtime::arg_debugging_name()) {
|
||||||
return Ok(LogLevel::Debug)
|
return Ok(LogLevel::Debug)
|
||||||
|
@ -245,7 +241,11 @@ fn translate_destinations(raw: &Vec<Value>) -> Result<Vec<LogDestination>> {
|
||||||
acc.and_then(|mut v| {
|
acc.and_then(|mut v| {
|
||||||
let dest = match *val {
|
let dest = match *val {
|
||||||
Value::String(ref s) => try!(translate_destination(s)),
|
Value::String(ref s) => try!(translate_destination(s)),
|
||||||
_ => return Err(RE::from_kind(EK::ConfigTypeError)),
|
_ => {
|
||||||
|
let path = "imag.logging.modules.<mod>.destinations".to_owned();
|
||||||
|
let ty = "Array<String>";
|
||||||
|
return Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
|
},
|
||||||
};
|
};
|
||||||
v.push(dest);
|
v.push(dest);
|
||||||
Ok(v)
|
Ok(v)
|
||||||
|
@ -258,15 +258,16 @@ fn aggregate_global_destinations(matches: &ArgMatches, config: Option<&Configura
|
||||||
{
|
{
|
||||||
|
|
||||||
match config {
|
match config {
|
||||||
Some(cfg) => match cfg
|
Some(cfg) => match cfg.read("imag.logging.destinations") {
|
||||||
.read("imag.logging.destinations")
|
Ok(Some(&Value::Array(ref a))) => translate_destinations(a),
|
||||||
.chain_err(|| EK::ConfigReadError)
|
Ok(Some(_)) => {
|
||||||
{
|
let path = "imag.logging.destinations".to_owned();
|
||||||
Ok(Some(&Value::Array(ref a))) => translate_destinations(a),
|
let ty = "Array";
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
Ok(None) => Err(RE::from_kind(EK::GlobalDestinationConfigMissing)),
|
|
||||||
Err(e) => Err(e)
|
|
||||||
},
|
},
|
||||||
|
Ok(None) => Err(RE::from_kind(EK::GlobalDestinationConfigMissing)),
|
||||||
|
Err(e) => Err(e).map_err(From::from),
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
if let Some(values) = matches.value_of(Runtime::arg_logdest_name()) {
|
if let Some(values) = matches.value_of(Runtime::arg_logdest_name()) {
|
||||||
// parse logdest specification from commandline
|
// parse logdest specification from commandline
|
||||||
|
@ -296,15 +297,12 @@ fn aggregate_global_format(
|
||||||
-> Result<String>
|
-> Result<String>
|
||||||
{
|
{
|
||||||
match config {
|
match config {
|
||||||
Some(cfg) => match cfg
|
Some(cfg) => match cfg.read(read_str) {
|
||||||
.read(read_str)
|
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
|
||||||
.chain_err(|| EK::ConfigReadError)
|
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError(read_str.to_owned(), "String"))),
|
||||||
{
|
Ok(None) => Err(RE::from_kind(error_kind_if_missing)),
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
|
Err(e) => Err(e).map_err(From::from),
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
},
|
||||||
Ok(None) => Err(RE::from_kind(error_kind_if_missing)),
|
|
||||||
Err(e) => Err(e)
|
|
||||||
},
|
|
||||||
None => match matches.value_of(cli_match_name).map(String::from) {
|
None => match matches.value_of(cli_match_name).map(String::from) {
|
||||||
Some(s) => Ok(s),
|
Some(s) => Ok(s),
|
||||||
None => Err(RE::from_kind(error_kind_if_missing))
|
None => Err(RE::from_kind(error_kind_if_missing))
|
||||||
|
@ -366,55 +364,68 @@ fn aggregate_module_settings(_matches: &ArgMatches, config: Option<&Configuratio
|
||||||
-> Result<BTreeMap<ModuleName, ModuleSettings>>
|
-> Result<BTreeMap<ModuleName, ModuleSettings>>
|
||||||
{
|
{
|
||||||
match config {
|
match config {
|
||||||
Some(cfg) => match cfg
|
Some(cfg) => match cfg.read("imag.logging.modules") {
|
||||||
.read("imag.logging.modules")
|
Ok(Some(&Value::Table(ref t))) => {
|
||||||
.chain_err(|| EK::ConfigReadError)
|
// translate the module settings from the table `t`
|
||||||
{
|
let mut settings = BTreeMap::new();
|
||||||
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 {
|
for (module_name, v) in t {
|
||||||
let destinations = try!(match v.read("destinations") {
|
let destinations = try!(match v.read("destinations") {
|
||||||
Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some),
|
Ok(Some(&Value::Array(ref a))) => translate_destinations(a).map(Some),
|
||||||
Ok(None) => Ok(None),
|
Ok(None) => Ok(None),
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
Ok(Some(_)) => {
|
||||||
Err(e) => Err(e).chain_err(|| EK::TomlReadError),
|
let path = "imag.logging.modules.<mod>.destinations".to_owned();
|
||||||
});
|
let ty = "Array";
|
||||||
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
|
},
|
||||||
|
Err(e) => Err(e).map_err(From::from),
|
||||||
|
});
|
||||||
|
|
||||||
let level = try!(match v.read("level") {
|
let level = try!(match v.read("level") {
|
||||||
Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some),
|
Ok(Some(&Value::String(ref s))) => match_log_level_str(s).map(Some),
|
||||||
Ok(None) => Ok(None),
|
Ok(None) => Ok(None),
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
Ok(Some(_)) => {
|
||||||
Err(e) => Err(e).chain_err(|| EK::TomlReadError),
|
let path = "imag.logging.modules.<mod>.level".to_owned();
|
||||||
});
|
let ty = "String";
|
||||||
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
|
},
|
||||||
|
Err(e) => Err(e).map_err(From::from),
|
||||||
|
});
|
||||||
|
|
||||||
let enabled = try!(match v.read("enabled") {
|
let enabled = try!(match v.read("enabled") {
|
||||||
Ok(Some(&Value::Boolean(b))) => Ok(b),
|
Ok(Some(&Value::Boolean(b))) => Ok(b),
|
||||||
Ok(None) => Ok(false),
|
Ok(None) => Ok(false),
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
Ok(Some(_)) => {
|
||||||
Err(e) => Err(e).chain_err(|| EK::TomlReadError),
|
let path = "imag.logging.modules.<mod>.enabled".to_owned();
|
||||||
});
|
let ty = "Boolean";
|
||||||
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
|
},
|
||||||
|
Err(e) => Err(e).map_err(From::from),
|
||||||
|
});
|
||||||
|
|
||||||
let module_settings = ModuleSettings {
|
let module_settings = ModuleSettings {
|
||||||
enabled: enabled,
|
enabled: enabled,
|
||||||
level: level,
|
level: level,
|
||||||
destinations: destinations,
|
destinations: destinations,
|
||||||
};
|
};
|
||||||
|
|
||||||
// We don't care whether there was a value, we override it.
|
// We don't care whether there was a value, we override it.
|
||||||
let _ = settings.insert(module_name.to_owned(), module_settings);
|
let _ = settings.insert(module_name.to_owned(), module_settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(settings)
|
Ok(settings)
|
||||||
},
|
|
||||||
Ok(Some(_)) => Err(RE::from_kind(EK::ConfigTypeError)),
|
|
||||||
Ok(None) => {
|
|
||||||
// No modules configured. This is okay!
|
|
||||||
Ok(BTreeMap::new())
|
|
||||||
},
|
|
||||||
Err(e) => Err(e),
|
|
||||||
},
|
},
|
||||||
|
Ok(Some(_)) => {
|
||||||
|
let path = "imag.logging.modules".to_owned();
|
||||||
|
let ty = "Table";
|
||||||
|
Err(RE::from_kind(EK::ConfigTypeError(path, ty)))
|
||||||
|
},
|
||||||
|
Ok(None) => {
|
||||||
|
// No modules configured. This is okay!
|
||||||
|
Ok(BTreeMap::new())
|
||||||
|
},
|
||||||
|
Err(e) => Err(e).map_err(From::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();
|
||||||
|
|
Loading…
Reference in a new issue