From 6053a1a5565a27b35fb918596f72da3a31aedb43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Wieche=C4=87?= Date: Sun, 14 Jan 2024 00:38:00 +0100 Subject: [PATCH] Added no_basename() for FileSpec and fixed log file name starting with _ when basename is blank --- .gitignore | 3 ++- src/file_spec.rs | 15 ++++++++++++--- tests/test_file_writer.rs | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6a3af20..6d1bf10 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ todo tests/*logspec.toml *~ .*~ -.vscode \ No newline at end of file +.vscode +.idea/ diff --git a/src/file_spec.rs b/src/file_spec.rs index f08031a..b9d8236 100644 --- a/src/file_spec.rs +++ b/src/file_spec.rs @@ -96,6 +96,13 @@ impl FileSpec { } } + /// Excludes the basename from the log filename. + #[must_use] + pub fn no_basename(mut self) -> Self { + self.basename = "".into(); + self + } + /// The specified String is used as the basename of the log file name, /// instead of the program name. Using a file separator within the argument is discouraged. #[must_use] @@ -214,10 +221,11 @@ impl FileSpec { filename.reserve(50); if let Some(discriminant) = &self.o_discriminant { - filename.push('_'); + if !filename.is_empty() { filename.push('_'); } filename.push_str(discriminant); } if let Some(timestamp) = &self.timestamp_cfg.get_timestamp() { + if !filename.is_empty() { filename.push('_'); } filename.push_str(timestamp); } if let Some(infix) = o_infix { @@ -239,10 +247,11 @@ impl FileSpec { filename.reserve(50); if let Some(discriminant) = &self.o_discriminant { - filename.push('_'); + if !filename.is_empty() { filename.push('_'); } filename.push_str(discriminant); } if let Some(timestamp) = &self.timestamp_cfg.get_timestamp() { + if !filename.is_empty() { filename.push('_'); } filename.push_str(timestamp); } filename.push_str(infix_pattern); @@ -266,7 +275,7 @@ impl FileSpec { } } -const TS_USCORE_DASHES_USCORE_DASHES: &str = "_%Y-%m-%d_%H-%M-%S"; +const TS_USCORE_DASHES_USCORE_DASHES: &str = "%Y-%m-%d_%H-%M-%S"; #[derive(Debug, Clone, Eq, PartialEq)] enum TimestampCfg { diff --git a/tests/test_file_writer.rs b/tests/test_file_writer.rs index 4955efe..500fbf6 100644 --- a/tests/test_file_writer.rs +++ b/tests/test_file_writer.rs @@ -3,7 +3,7 @@ mod test_utils; use flexi_logger::{detailed_format, opt_format, Cleanup, Criterion, FileSpec, Logger, Naming}; use log::*; -const COUNT: u8 = 6; +const COUNT: u8 = 8; #[test] fn test_write_modes() { @@ -73,6 +73,21 @@ fn work(value: u8) { .discriminant("foo"), ); } + 6 => { + logger = logger.format(opt_format).log_to_file( + FileSpec::default() + .directory(self::test_utils::dir()) + .no_basename(), + ); + } + 7 => { + logger = logger.format(opt_format).log_to_file( + FileSpec::default() + .directory(self::test_utils::dir()) + .no_basename() + .discriminant("foo"), + ); + } COUNT..=u8::MAX => { unreachable!("dtrtgfg") }