blob: 016f3b2cc325fff695399cc0d491385d79a3123b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
use std::{fs, io, path::Path};
pub mod diag;
/// Unable to create new log file
#[derive(thiserror::Error, Debug)]
#[error("Unable to create new log file")]
pub struct RotateLogError(#[from] io::Error);
/// Create a new log file while backing up a previous version of it.
///
/// A new log file is created with the given file name, but if a file with that name already exists
/// it is backed up with the extension changed to `.old.log`.
pub fn rotate_log(file: &Path) -> Result<(), RotateLogError> {
let backup = file.with_extension("old.log");
if let Err(error) = fs::rename(file, &backup)
&& error.kind() != io::ErrorKind::NotFound
{
log::warn!(
"Failed to rotate log file to {}: {}",
backup.display(),
error
);
}
fs::File::create(file).map(|_| ()).map_err(RotateLogError)
}
|