| 93 | } |
| 94 | |
| 95 | fn remove_old_files(&self, keep_count: usize) -> std::io::Result<()> { |
| 96 | let mut files = fs::read_dir(&self.dir)? |
| 97 | .filter_map(|entry| { |
| 98 | let entry = entry.ok()?; |
| 99 | let path = entry.path(); |
| 100 | let old_file_name = path.file_name()?.to_string_lossy().into_owned(); |
| 101 | |
| 102 | if old_file_name.starts_with(&self.file_name) |
| 103 | && old_file_name != format!("{}.log", self.file_name) |
| 104 | { |
| 105 | let date = old_file_name |
| 106 | .strip_prefix(&self.file_name)? |
| 107 | .strip_prefix('_')? |
| 108 | .strip_suffix(".log")?; |
| 109 | Some((path, date.to_string())) |
| 110 | } else { |
| 111 | None |
| 112 | } |
| 113 | }) |
| 114 | .collect::<Vec<_>>(); |
| 115 | |
| 116 | files.sort_by(|a, b| a.1.cmp(&b.1)); |
| 117 | |
| 118 | if files.len() > keep_count { |
| 119 | for (old_log_path, _) in files.iter().take(files.len() - keep_count) { |
| 120 | fs::remove_file(old_log_path)?; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | Ok(()) |
| 125 | } |
| 126 | |
| 127 | fn rename_file_to_dated(&self) -> std::io::Result<()> { |
| 128 | let to = self.dir.join(format!( |