Skip to content

Commit

Permalink
Added no_basename() for FileSpec and fixed log file name starting wit…
Browse files Browse the repository at this point in the history
…h _ when basename is blank
  • Loading branch information
Krystian Wiecheć authored and emabee committed Jan 20, 2024
1 parent f004d1e commit 6053a1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ todo
tests/*logspec.toml
*~
.*~
.vscode
.vscode
.idea/
15 changes: 12 additions & 3 deletions src/file_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion tests/test_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 6053a1a

Please sign in to comment.