Skip to content

Commit

Permalink
[0.15.2] Improve handling of parse-errors
Browse files Browse the repository at this point in the history
- Fix default format for files (was and is documented to be uncolored, but was colored).
  • Loading branch information
emabee committed Mar 25, 2020
1 parent 92db792 commit cf4f032
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flexi_logger"
version = "0.15.1"
version = "0.15.2"
authors = ["emabee <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions src/flexi_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, LogSpecification),
/// Failed to parse log specification.
#[error("Failed to parse log specification: {0}")]
Parse(String, LogSpecification),

/// Logger initialization failed.
#[error("Logger initialization failed")]
Expand Down
5 changes: 2 additions & 3 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <br>
/// <code><span style="color:red">ERROR</span>
/// &#91;my_prog::some_submodule&#93;
/// <span style="color:red">File not found</span></code>
/// <code><span style="color:red">ERROR</span> &#91;my_prog::some_submodule&#93; <span
/// style="color:red">File not found</span></code>
///
/// Only available with feature `colors`.
///
Expand Down
32 changes: 17 additions & 15 deletions src/log_specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl LogSpecification {
///
/// `FlexiLoggerError::Parse` if the input is malformed.
pub fn parse(spec: &str) -> Result<Self, FlexiLoggerError> {
let mut parse_errs = Vec::<String>::new();
let mut parse_errs = String::new();
let mut dirs = Vec::<ModuleFilter>::new();

let mut parts = spec.split('/');
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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
}
});
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -252,7 +252,7 @@ impl LogSpecification {
}

let logspec_ff: LogSpecFileFormat = toml::from_str(s)?;
let mut parse_errs = Vec::<String>::new();
let mut parse_errs = String::new();
let mut module_filters = Vec::<ModuleFilter>::new();

if let Some(s) = logspec_ff.global_level {
Expand All @@ -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
}
},
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -372,13 +372,15 @@ impl LogSpecification {
}
}

fn push_err(s: String, parse_errs: &mut Vec<String>) {
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<String>,
errors: String,
logspec: LogSpecification,
) -> Result<LogSpecification, FlexiLoggerError> {
Err(FlexiLoggerError::Parse(errors, logspec))
Expand All @@ -400,11 +402,11 @@ fn parse_level_filter<S: AsRef<str>>(s: S) -> Result<LevelFilter, FlexiLoggerErr
}
}

fn contains_dash_or_whitespace(s: &str, parse_errs: &mut Vec<String>) -> 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
),
Expand Down
18 changes: 8 additions & 10 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::sync::{Arc, RwLock};
///
pub struct Logger {
spec: LogSpecification,
parse_errs: Option<Vec<String>>,
parse_errs: Option<String>,
log_target: LogTarget,
duplicate: Duplicate,
format_for_file: FormatFunction,
Expand Down Expand Up @@ -120,20 +120,18 @@ impl Logger {
Self::from_result(LogSpecification::env_or_parse(s))
}

fn from_spec_and_errs(spec: LogSpecification, parse_errs: Option<Vec<String>>) -> 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<String>) -> 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::<String, Box<dyn LogWriter>>::new(),
}
Expand Down
4 changes: 2 additions & 2 deletions src/writers/file_log_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -1304,7 +1304,7 @@ mod test {
}

fn get_hackyfilepath(infix: &str, discr: &str) -> Box<Path> {
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()
Expand Down

0 comments on commit cf4f032

Please sign in to comment.