From cf4f0325da6444fbaac58964291c3db92ac48ffe Mon Sep 17 00:00:00 2001 From: emabee Date: Wed, 25 Mar 2020 09:29:50 +0100 Subject: [PATCH] [0.15.2] Improve handling of parse-errors - Fix default format for files (was and is documented to be uncolored, but was colored). --- CHANGELOG.md | 6 ++++++ Cargo.toml | 2 +- src/flexi_error.rs | 6 +++--- src/formats.rs | 5 ++--- src/log_specification.rs | 32 +++++++++++++++++--------------- src/logger.rs | 18 ++++++++---------- src/writers/file_log_writer.rs | 4 ++-- 7 files changed, 39 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 277999e..e20ecf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.15.2] - 2020-03-24 + +Improve handling of parse-errors. + +Fix default format for files (was and is documented to be uncolored, but was colored). + ## [0.15.1] - 2020-03-04 Make the textfilter functionality an optional default feature; diff --git a/Cargo.toml b/Cargo.toml index 1e1a6f9..ad70fb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "flexi_logger" -version = "0.15.1" +version = "0.15.2" authors = ["emabee "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/flexi_error.rs b/src/flexi_error.rs index 9f58a14..ce170bb 100644 --- a/src/flexi_error.rs +++ b/src/flexi_error.rs @@ -46,9 +46,9 @@ pub enum FlexiLoggerError { #[error("Invalid level filter")] LevelFilter(String), - /// Parsing a log specification failed. - #[error("Parsing a log specification failed")] - Parse(Vec, LogSpecification), + /// Failed to parse log specification. + #[error("Failed to parse log specification: {0}")] + Parse(String, LogSpecification), /// Logger initialization failed. #[error("Logger initialization failed")] diff --git a/src/formats.rs b/src/formats.rs index 8bb49cc..0b71fe8 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -25,9 +25,8 @@ pub fn default_format( #[allow(clippy::doc_markdown)] /// A colored version of the logline-formatter `default_format` /// that produces log lines like
-/// ERROR -/// [my_prog::some_submodule] -/// File not found +/// ERROR [my_prog::some_submodule] File not found /// /// Only available with feature `colors`. /// diff --git a/src/log_specification.rs b/src/log_specification.rs index b3fe027..bca4a10 100644 --- a/src/log_specification.rs +++ b/src/log_specification.rs @@ -115,7 +115,7 @@ impl LogSpecification { /// /// `FlexiLoggerError::Parse` if the input is malformed. pub fn parse(spec: &str) -> Result { - let mut parse_errs = Vec::::new(); + let mut parse_errs = String::new(); let mut dirs = Vec::::new(); let mut parts = spec.split('/'); @@ -124,7 +124,7 @@ impl LogSpecification { let filter = parts.next(); if parts.next().is_some() { push_err( - format!("invalid log spec '{}' (too many '/'s), ignoring it", spec), + &format!("invalid log spec '{}' (too many '/'s), ignoring it", spec), &mut parse_errs, ); return parse_err(parse_errs, Self::off()); @@ -167,14 +167,14 @@ impl LogSpecification { match parse_level_filter(part_1.trim()) { Ok(num) => (num, Some(part_0.trim())), Err(e) => { - push_err(e.to_string(), &mut parse_errs); + push_err(&e.to_string(), &mut parse_errs); continue; } } } _ => { push_err( - format!("invalid part in log spec '{}', ignoring it", s), + &format!("invalid part in log spec '{}', ignoring it", s), &mut parse_errs, ); continue; @@ -191,7 +191,7 @@ impl LogSpecification { let textfilter = filter.and_then(|filter| match Regex::new(filter) { Ok(re) => Some(re), Err(e) => { - push_err(format!("invalid regex filter - {}", e), &mut parse_errs); + push_err(&format!("invalid regex filter - {}", e), &mut parse_errs); None } }); @@ -205,7 +205,7 @@ impl LogSpecification { if parse_errs.is_empty() { Ok(logspec) } else { - Err(FlexiLoggerError::Parse(parse_errs, logspec)) + parse_err(parse_errs, logspec) } } @@ -252,7 +252,7 @@ impl LogSpecification { } let logspec_ff: LogSpecFileFormat = toml::from_str(s)?; - let mut parse_errs = Vec::::new(); + let mut parse_errs = String::new(); let mut module_filters = Vec::::new(); if let Some(s) = logspec_ff.global_level { @@ -275,7 +275,7 @@ impl LogSpecification { Some(s) => match Regex::new(&s) { Ok(re) => Some(re), Err(e) => { - push_err(format!("invalid regex filter - {}", e), &mut parse_errs); + push_err(&format!("invalid regex filter - {}", e), &mut parse_errs); None } }, @@ -289,7 +289,7 @@ impl LogSpecification { if parse_errs.is_empty() { Ok(logspec) } else { - Err(FlexiLoggerError::Parse(parse_errs, logspec)) + parse_err(parse_errs, logspec) } } @@ -372,13 +372,15 @@ impl LogSpecification { } } -fn push_err(s: String, parse_errs: &mut Vec) { - println!("flexi_logger warning: {}", s); - parse_errs.push(s); +fn push_err(s: &str, parse_errs: &mut String) { + if !parse_errs.is_empty() { + parse_errs.push_str("; "); + } + parse_errs.push_str(s); } fn parse_err( - errors: Vec, + errors: String, logspec: LogSpecification, ) -> Result { Err(FlexiLoggerError::Parse(errors, logspec)) @@ -400,11 +402,11 @@ fn parse_level_filter>(s: S) -> Result) -> bool { +fn contains_dash_or_whitespace(s: &str, parse_errs: &mut String) -> bool { let result = s.find('-').is_some() || s.find(' ').is_some() || s.find('\t').is_some(); if result { push_err( - format!( + &format!( "ignoring invalid part in log spec '{}' (contains a dash or whitespace)", s ), diff --git a/src/logger.rs b/src/logger.rs index f750c6d..06135f5 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -46,7 +46,7 @@ use std::sync::{Arc, RwLock}; /// pub struct Logger { spec: LogSpecification, - parse_errs: Option>, + parse_errs: Option, log_target: LogTarget, duplicate: Duplicate, format_for_file: FormatFunction, @@ -120,20 +120,18 @@ impl Logger { Self::from_result(LogSpecification::env_or_parse(s)) } - fn from_spec_and_errs(spec: LogSpecification, parse_errs: Option>) -> Self { - #[cfg(feature = "colors")] - let default_format = formats::colored_default_format; - #[cfg(not(feature = "colors"))] - let default_format = formats::default_format; - + fn from_spec_and_errs(spec: LogSpecification, parse_errs: Option) -> Self { Self { spec, parse_errs, log_target: LogTarget::StdErr, duplicate: Duplicate::None, - format_for_file: default_format, - format_for_stderr: default_format, - format_for_writer: default_format, + format_for_file: formats::default_format, + #[cfg(feature = "colors")] + format_for_stderr: formats::colored_default_format, + #[cfg(not(feature = "colors"))] + format_for_stderr: formats::default_format, + format_for_writer: formats::default_format, flwb: FileLogWriter::builder(), other_writers: HashMap::>::new(), } diff --git a/src/writers/file_log_writer.rs b/src/writers/file_log_writer.rs index ddd3bb8..7764da3 100644 --- a/src/writers/file_log_writer.rs +++ b/src/writers/file_log_writer.rs @@ -214,7 +214,7 @@ impl FileLogWriterBuilder { return Err(FlexiLoggerError::OutputBadDirectory); }; - let arg0 = env::args().nth(0).unwrap_or_else(|| "rs".to_owned()); + let arg0 = env::args().next().unwrap_or_else(|| "rs".to_owned()); self.config.filename_config.file_basename = Path::new(&arg0).file_stem().unwrap(/*cannot fail*/).to_string_lossy().to_string(); @@ -1304,7 +1304,7 @@ mod test { } fn get_hackyfilepath(infix: &str, discr: &str) -> Box { - let arg0 = std::env::args().nth(0).unwrap(); + let arg0 = std::env::args().next().unwrap(); let mut s_filename = Path::new(&arg0) .file_stem() .unwrap()