-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rotate file if exceeds max file size config (#3)
- Loading branch information
Showing
7 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::{fs::File, io::LineWriter, path::PathBuf, sync::Mutex}; | ||
|
||
use crate::Config; | ||
|
||
pub(crate) fn rotate_if_exceeds_max_file_size( | ||
file: &Mutex<LineWriter<File>>, | ||
file_path: PathBuf, | ||
config: &Config, | ||
) { | ||
if config.max_file_size.is_none() { | ||
return; | ||
} | ||
|
||
let mut file = file.lock().unwrap(); | ||
|
||
let md = file.get_ref().metadata().unwrap(); | ||
|
||
if md.len() > config.max_file_size.unwrap() { | ||
let path = file_path.to_str().unwrap(); | ||
|
||
let mut new_path = format!("{}.old", path); | ||
|
||
let mut counter = 1; | ||
while std::fs::metadata(&new_path).is_ok() { | ||
new_path = format!("{}.old{}", path, counter); | ||
counter += 1; | ||
} | ||
|
||
std::fs::rename(path, &new_path).unwrap(); | ||
|
||
let new_file = std::fs::File::create(path).unwrap(); | ||
*file = LineWriter::new(new_file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters