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..c6ef1b7 100644 --- a/src/file_spec.rs +++ b/src/file_spec.rs @@ -96,6 +96,15 @@ impl FileSpec { } } + /// Makes the logger not include a basename into the names of the log files + /// + /// Equivalent to `basename("")`. + #[must_use] + pub fn suppress_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 +223,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 +249,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 +277,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..c453ef9 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()) + .suppress_basename(), + ); + } + 7 => { + logger = logger.format(opt_format).log_to_file( + FileSpec::default() + .directory(self::test_utils::dir()) + .suppress_basename() + .discriminant("foo"), + ); + } COUNT..=u8::MAX => { unreachable!("dtrtgfg") }