| 1 |
use std::io::Write; |
| 2 |
use std::path::Path; |
| 3 |
|
| 4 |
use crate::errors::HgError; |
| 5 |
use crate::errors::HgResultExt; |
| 6 |
use crate::errors::IoErrorContext; |
| 7 |
use crate::errors::IoResultExt; |
| 8 |
use crate::vfs::Vfs; |
| 9 |
use crate::vfs::VfsImpl; |
| 10 |
|
| 11 |
/// An utility to append to a log file with the given name, and optionally |
| 12 |
/// rotate it after it reaches a certain maximum size. |
| 13 |
/// |
| 14 |
/// Rotation works by renaming "example.log" to "example.log.1", after renaming |
| 15 |
/// "example.log.1" to "example.log.2" etc up to the given maximum number of |
| 16 |
/// files. |
| 17 |
pub struct LogFile<'a> { |
| 18 |
vfs: VfsImpl, |
| 19 |
name: &'a str, |
| 20 |
max_size: Option<u64>, |
| 21 |
max_files: u32, |
| 22 |
} |
| 23 |
|
| 24 |
impl<'a> LogFile<'a> { |
| 25 |
pub fn new(vfs: VfsImpl, name: &'a str) -> Self { |
| 26 |
Self { vfs, name, max_size: None, max_files: 0 } |
| 27 |
} |
| 28 |
|
| 29 |
/// Rotate before writing to a log file that was already larger than the |
| 30 |
/// given size, in bytes. `None` disables rotation. |
| 31 |
pub fn max_size(mut self, value: Option<u64>) -> Self { |
| 32 |
self.max_size = value; |
| 33 |
self |
| 34 |
} |
| 35 |
|
| 36 |
/// Keep this many rotated files `{name}.1` up to `{name}.{max}`, in |
| 37 |
/// addition to the original `{name}` file. |
| 38 |
pub fn max_files(mut self, value: u32) -> Self { |
| 39 |
self.max_files = value; |
| 40 |
self |
| 41 |
} |
| 42 |
|
| 43 |
/// Append the given `bytes` as-is to the log file, after rotating if |
| 44 |
/// needed. |
| 45 |
/// |
| 46 |
/// No trailing newline is added. Make sure to include one in `bytes` if |
| 47 |
/// desired. |
| 48 |
pub fn write(&self, bytes: &[u8]) -> Result<(), HgError> { |
| 49 |
let path = self.vfs.join(self.name); |
| 50 |
let context = || IoErrorContext::WritingFile(path.clone()); |
| 51 |
let open = || { |
| 52 |
std::fs::OpenOptions::new() |
| 53 |
.create(true) |
| 54 |
.append(true) |
| 55 |
.open(&path) |
| 56 |
.with_context(context) |
| 57 |
}; |
| 58 |
let mut file = open()?; |
| 59 |
if let Some(max_size) = self.max_size { |
| 60 |
if file.metadata().with_context(context)?.len() >= max_size { |
| 61 |
// For example with `max_files == 5`, the first iteration of |
| 62 |
// this loop has `i == 4` and renames `{name}.4` to `{name}.5`. |
| 63 |
// The last iteration renames `{name}.1` to |
| 64 |
// `{name}.2` |
| 65 |
for i in (1..self.max_files).rev() { |
| 66 |
self.vfs |
| 67 |
.rename( |
| 68 |
Path::new(&format!("{}.{}", self.name, i)), |
| 69 |
Path::new(&format!("{}.{}", self.name, i + 1)), |
| 70 |
false, |
| 71 |
) |
| 72 |
.io_not_found_as_none()?; |
| 73 |
} |
| 74 |
// Then rename `{name}` to `{name}.1`. This is the |
| 75 |
// previously-opened `file`. |
| 76 |
self.vfs |
| 77 |
.rename( |
| 78 |
Path::new(&self.name), |
| 79 |
Path::new(&format!("{}.1", self.name)), |
| 80 |
false, |
| 81 |
) |
| 82 |
.io_not_found_as_none()?; |
| 83 |
// Finally, create a new `{name}` file and replace our `file` |
| 84 |
// handle. |
| 85 |
file = open()?; |
| 86 |
} |
| 87 |
} |
| 88 |
file.write_all(bytes).with_context(context)?; |
| 89 |
file.sync_all().with_context(context) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_rotation() { |
| 95 |
use crate::revlog::path_encode::PathEncoding; |
| 96 |
let temp = tempfile::tempdir().unwrap(); |
| 97 |
let vfs = VfsImpl::new(temp.path().to_owned(), false, PathEncoding::None); |
| 98 |
let logger = |
| 99 |
LogFile::new(vfs.clone(), "log").max_size(Some(3)).max_files(2); |
| 100 |
logger.write(b"one\n").unwrap(); |
| 101 |
logger.write(b"two\n").unwrap(); |
| 102 |
logger.write(b"3\n").unwrap(); |
| 103 |
logger.write(b"four\n").unwrap(); |
| 104 |
logger.write(b"five\n").unwrap(); |
| 105 |
assert_eq!(vfs.read("log").unwrap(), b"five\n"); |
| 106 |
assert_eq!(vfs.read("log.1").unwrap(), b"3\nfour\n"); |
| 107 |
assert_eq!(vfs.read("log.2").unwrap(), b"two\n"); |
| 108 |
assert!(vfs.read("log.3").io_not_found_as_none().unwrap().is_none()); |
| 109 |
} |