Skip to content

Commit

Permalink
Fix issue with Naming::TimestampsCustomFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
emabee committed Oct 29, 2024
1 parent 8dcebae commit 0bb276b
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 92 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ 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).

## [unpublished] - 202?-??-??
## [0.25.5] - 2024-10-29

Add badge for OpenSSF Best Practices.
Fix [issue #181](https://github.com/emabee/flexi_logger/issues/181).

Increase stack sizes for flusher threads from very minimal 128 to 1024 bytes.

Add badge for OpenSSF Best Practices.

## [0.29.4] - 2024-10-21

Fix [issue #179](https://github.com/emabee/flexi_logger/issues/179) that in rotation with
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.29.4"
version = "0.29.5"
authors = ["emabee <[email protected]>"]
categories = ["development-tools::debugging"]
description = """
Expand Down
7 changes: 6 additions & 1 deletion scripts/qualify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ macro_rules! run_command {
let mut child = command.spawn().unwrap();
let status = child.wait().unwrap();
if !status.success() {
print!("> {}", yansi::Paint::red("qualify terminates due to error"));
println!(
"{} in {}",
yansi::Paint::red("qualify terminates due to error"),
yansi::Paint::yellow($cmd)
);
std::process::exit(-1);
}
};
Expand Down Expand Up @@ -42,6 +46,7 @@ fn run_script(s: &str) {

fn main() {
println!("Qualify flexi_logger");
run_command!("date");

// format
run_command!("cargo fmt");
Expand Down
27 changes: 14 additions & 13 deletions src/parameters/file_spec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::writers::file_log_writer::InfixFilter;
use crate::{DeferredNow, FlexiLoggerError};
use std::{
ffi::{OsStr, OsString},
ops::Add,
path::{Path, PathBuf},
};

/// Builder object for specifying the name and path of the log output file.
///
/// The filename is built from several partially components, using this pattern:
Expand Down Expand Up @@ -343,10 +343,11 @@ impl FileSpec {
}
}

pub(crate) fn list_of_files<F>(&self, infix_filter: F, o_suffix: Option<&str>) -> Vec<PathBuf>
where
F: Fn(&str) -> bool,
{
pub(crate) fn list_of_files(
&self,
infix_filter: &InfixFilter,
o_suffix: Option<&str>,
) -> Vec<PathBuf> {
self.filter_files(&self.read_dir_related_files(), infix_filter, o_suffix)
}

Expand All @@ -372,15 +373,12 @@ impl FileSpec {
log_files
}

pub(crate) fn filter_files<F>(
pub(crate) fn filter_files(
&self,
files: &[PathBuf],
infix_filter: F,
infix_filter: &InfixFilter,
o_suffix: Option<&str>,
) -> Vec<PathBuf>
where
F: Fn(&str) -> bool,
{
) -> Vec<PathBuf> {
let fixed_name_part = self.fixed_name_part();
files
.iter()
Expand All @@ -407,7 +405,8 @@ impl FileSpec {
return false;
}
let maybe_infix = &stem[infix_start..];
infix_filter(maybe_infix)
let end = maybe_infix.find('.').unwrap_or(maybe_infix.len());
infix_filter.filter_infix(&maybe_infix[..end])
})
.map(PathBuf::clone)
.collect::<Vec<PathBuf>>()
Expand Down Expand Up @@ -443,6 +442,7 @@ impl TimestampCfg {
#[cfg(test)]
mod test {
use super::{FileSpec, TimestampCfg};
use crate::writers::file_log_writer::InfixFilter;
use std::{
fs::File,
path::{Path, PathBuf},
Expand Down Expand Up @@ -708,7 +708,8 @@ mod test {
}

println!("\nRelevant subset:");
for pb in filespec.list_of_files(|s: &str| s.starts_with("Infix"), Some("log")) {
for pb in filespec.list_of_files(&InfixFilter::StartsWth("Infix".to_string()), Some("log"))
{
println!(" {}", pb.display());
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/parameters/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ pub enum Naming {
/// See <https://docs.rs/chrono/latest/chrono/format/strftime/index.html> for a list of
/// supported specifiers.
///
/// Make sure you use a format that is compatible to your file system(s).
/// **Make sure to use a format**
///
/// - that is compatible to your file system(s) (e.g., don't use slashes),
/// - that can be used by
/// [chrono::NaiveDateTime](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html#method.parse_from_str)
/// or [chrono::NaiveDate](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html#method.parse_from_str)
///
/// Further, if you choose `current_infix` = `None` or `Some("")`, make sure to rotate only
/// by [age](crate::Criterion::Age), and choose an age that is not smaller than what
/// is expressed in the infix (e.g., don't rotate by minute if the infix only shows days).
///
/// Examples:
///
Expand All @@ -97,6 +106,14 @@ pub enum Naming {
}
impl Naming {
pub(crate) fn writes_direct(self) -> bool {
matches!(self, Naming::NumbersDirect | Naming::TimestampsDirect)
matches!(
self,
Naming::NumbersDirect
| Naming::TimestampsDirect
| Naming::TimestampsCustomFormat {
current_infix: None | Some(""),
format: _
}
)
}
}
7 changes: 5 additions & 2 deletions src/primary_writer/std_writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "async")]
use {
crate::util::{eprint_err, ErrorCode, ASYNC_FLUSH, ASYNC_SHUTDOWN},
crate::{
util::{eprint_err, ErrorCode, ASYNC_FLUSH, ASYNC_SHUTDOWN},
ZERO_DURATION,
},
crossbeam_channel::{SendError, Sender},
crossbeam_queue::ArrayQueue,
};
Expand All @@ -10,7 +13,7 @@ use {
crate::{
util::{io_err, write_buffered},
writers::LogWriter,
DeferredNow, EffectiveWriteMode, FormatFunction, WriteMode, ZERO_DURATION,
DeferredNow, EffectiveWriteMode, FormatFunction, WriteMode,
},
log::Record,
std::io::{BufWriter, Write},
Expand Down
3 changes: 3 additions & 0 deletions src/writers/file_log_writer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![allow(clippy::module_name_repetitions)]
mod builder;
mod config;
mod infix_filter;
mod state;
mod state_handle;
mod threads;

pub use self::builder::{ArcFileLogWriter, FileLogWriterBuilder, FileLogWriterHandle};
pub use self::config::FileLogWriterConfig;
pub(crate) use infix_filter::InfixFilter;

use self::{config::RotationConfig, state::State, state_handle::StateHandle};
use crate::{
Expand Down Expand Up @@ -199,6 +201,7 @@ impl Drop for FileLogWriter {

#[cfg(test)]
mod test {
#[cfg(feature = "async")]
use crate::ZERO_DURATION;
use crate::{writers::LogWriter, Cleanup, Criterion, DeferredNow, FileSpec, Naming, WriteMode};
use chrono::Local;
Expand Down
33 changes: 33 additions & 0 deletions src/writers/file_log_writer/infix_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::state::{timestamp_from_ts_infix, InfixFormat};

#[derive(Clone)]
pub(crate) enum InfixFilter {
Timstmps(InfixFormat),
Numbrs,
#[cfg(test)]
StartsWth(String),
Equls(String),
None,
}
impl InfixFilter {
pub(crate) fn filter_infix(&self, infix: &str) -> bool {
match self {
InfixFilter::Timstmps(infix_format) => {
let tmp = timestamp_from_ts_infix(infix, infix_format);
tmp.is_ok()
}
InfixFilter::Numbrs => {
if infix.len() > 2 {
let mut chars = infix.chars();
chars.next().unwrap() == 'r' && chars.next().unwrap().is_ascii_digit()
} else {
false
}
}
#[cfg(test)]
InfixFilter::StartsWth(s) => infix.starts_with(s),
InfixFilter::Equls(s) => infix.eq(s),
InfixFilter::None => false,
}
}
}
Loading

0 comments on commit 0bb276b

Please sign in to comment.