Skip to content

Commit

Permalink
Add FileLimit::Unlimited (implements #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ploppz committed May 16, 2024
1 parent 3acb684 commit fc60342
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "file-rotate"
version = "0.7.5"
version = "0.7.6"
authors = ["Kevin Robert Stravers <[email protected]>", "Erlend Langseth <[email protected]>"]
edition = "2018"
description = "Log rotation for files"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ impl<S: SuffixScheme> FileRotate<S> {
let mut result = Ok(());
for (i, suffix) in self.suffixes.iter().enumerate().rev() {
if self.suffix_scheme.too_old(&suffix.suffix, i) {
result = result.and(std::fs::remove_file(suffix.to_path(&self.basepath)));
result = result.and(fs::remove_file(suffix.to_path(&self.basepath)));
youngest_old = Some((*suffix).clone());
} else {
break;
Expand Down
7 changes: 5 additions & 2 deletions src/suffix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait SuffixScheme {

/// Find all files in the basepath.parent() directory that has path equal to basepath + a valid
/// suffix. Return sorted collection - sorted from most recent to oldest based on the
/// [Ord](std::cmp::Ord) implementation of `Self::Repr`.
/// [Ord] implementation of `Self::Repr`.
fn scan_suffixes(&self, basepath: &Path) -> BTreeSet<SuffixInfo<Self::Repr>> {
let mut suffixes = BTreeSet::new();
let filename_prefix = basepath
Expand Down Expand Up @@ -294,6 +294,7 @@ impl SuffixScheme for AppendTimestamp {
let old_timestamp = (Local::now() - age).format(self.format).to_string();
suffix.timestamp < old_timestamp
}
FileLimit::Unlimited => false,
}
}
}
Expand All @@ -304,6 +305,8 @@ pub enum FileLimit {
MaxFiles(usize),
/// Delete files whose age exceeds the `Duration` - age is determined by the suffix of the file
Age(Duration),
/// Never delete files
Unlimited,
}

#[cfg(test)]
Expand Down Expand Up @@ -410,7 +413,7 @@ mod test {
let log_path = dir.join("file");

for suffix in case.suffixes.iter().chain(case.incorrect_suffixes) {
std::fs::File::create(dir.join(format!("file.{}", suffix))).unwrap();
File::create(dir.join(format!("file.{}", suffix))).unwrap();
}

let scheme = AppendTimestamp::with_format(
Expand Down
14 changes: 7 additions & 7 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn timestamp_max_age_deletion() {
);
writeln!(log, "trigger\nat\nleast\none\nrotation").unwrap();

let mut filenames = std::fs::read_dir(dir)
let mut filenames = fs::read_dir(dir)
.unwrap()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().is_file())
Expand Down Expand Up @@ -278,7 +278,7 @@ fn byte_count_recalculation() {
let parent = tmp_dir.path();
let log_path = parent.join("log");

std::fs::write(&log_path, b"a").unwrap();
fs::write(&log_path, b"a").unwrap();

let mut file_rotate = FileRotate::new(
&*log_path.to_string_lossy(),
Expand All @@ -292,10 +292,10 @@ fn byte_count_recalculation() {
write!(file_rotate, "bc").unwrap();
assert_eq!(file_rotate.log_paths().len(), 1);
// The size of the rotated file should be 2 ('ab)
let rotated_content = std::fs::read(&file_rotate.log_paths()[0]).unwrap();
let rotated_content = fs::read(&file_rotate.log_paths()[0]).unwrap();
assert_eq!(rotated_content, b"ab");
// The size of the main file should be 1 ('c')
let main_content = std::fs::read(log_path).unwrap();
let main_content = fs::read(log_path).unwrap();
assert_eq!(main_content, b"c");
}

Expand All @@ -307,7 +307,7 @@ fn line_count_recalculation() {
let parent = tmp_dir.path();
let log_path = parent.join("log");

std::fs::write(&log_path, b"a\n").unwrap();
fs::write(&log_path, b"a\n").unwrap();

let mut file_rotate = FileRotate::new(
&*log_path.to_string_lossy(),
Expand Down Expand Up @@ -510,14 +510,14 @@ fn test_file_limit() {
let log_path = dir.join("file");
let old_file = dir.join("file.2022-02-01");

std::fs::File::create(&old_file).unwrap();
File::create(&old_file).unwrap();

let first = get_fake_date_time("2022-02-02T01:00:00");
let second = get_fake_date_time("2022-02-03T01:00:00");
let third = get_fake_date_time("2022-02-04T01:00:00");

let mut log = FileRotate::new(
&log_path,
log_path,
AppendTimestamp::with_format("%Y-%m-%d", FileLimit::MaxFiles(1), DateFrom::DateYesterday),
ContentLimit::Time(TimeFrequency::Daily),
Compression::None,
Expand Down

0 comments on commit fc60342

Please sign in to comment.