comparison rust/hg-core/src/logging.rs @ 51864:db7dbe6f7bb2

rust: add Vfs trait This will allow for the use of multiple vfs like in the Python implementation, as well as hiding the details of the upcoming Python vfs wrapper to hg-core.
author Rapha?l Gom?s <rgomes@octobus.net>
date Wed, 19 Jun 2024 14:49:35 +0200
parents 9cd35c8c6044
children 7be39c5110c9
comparison
equal deleted inserted replaced
51863:69b804c8e09e 51864:db7dbe6f7bb2
1 use crate::errors::{HgError, HgResultExt, IoErrorContext, IoResultExt}; 1 use crate::errors::{HgError, HgResultExt, IoErrorContext, IoResultExt};
2 use crate::vfs::Vfs; 2 use crate::vfs::VfsImpl;
3 use std::io::Write; 3 use std::io::Write;
4 4
5 /// An utility to append to a log file with the given name, and optionally 5 /// An utility to append to a log file with the given name, and optionally
6 /// rotate it after it reaches a certain maximum size. 6 /// rotate it after it reaches a certain maximum size.
7 /// 7 ///
8 /// Rotation works by renaming "example.log" to "example.log.1", after renaming 8 /// Rotation works by renaming "example.log" to "example.log.1", after renaming
9 /// "example.log.1" to "example.log.2" etc up to the given maximum number of 9 /// "example.log.1" to "example.log.2" etc up to the given maximum number of
10 /// files. 10 /// files.
11 pub struct LogFile<'a> { 11 pub struct LogFile<'a> {
12 vfs: Vfs<'a>, 12 vfs: VfsImpl,
13 name: &'a str, 13 name: &'a str,
14 max_size: Option<u64>, 14 max_size: Option<u64>,
15 max_files: u32, 15 max_files: u32,
16 } 16 }
17 17
18 impl<'a> LogFile<'a> { 18 impl<'a> LogFile<'a> {
19 pub fn new(vfs: Vfs<'a>, name: &'a str) -> Self { 19 pub fn new(vfs: VfsImpl, name: &'a str) -> Self {
20 Self { 20 Self {
21 vfs, 21 vfs,
22 name, 22 name,
23 max_size: None, 23 max_size: None,
24 max_files: 0, 24 max_files: 0,
85 } 85 }
86 86
87 #[test] 87 #[test]
88 fn test_rotation() { 88 fn test_rotation() {
89 let temp = tempfile::tempdir().unwrap(); 89 let temp = tempfile::tempdir().unwrap();
90 let vfs = Vfs { base: temp.path() }; 90 let vfs = VfsImpl {
91 let logger = LogFile::new(vfs, "log").max_size(Some(3)).max_files(2); 91 base: temp.path().to_owned(),
92 };
93 let logger = LogFile::new(vfs.clone(), "log")
94 .max_size(Some(3))
95 .max_files(2);
92 logger.write(b"one\n").unwrap(); 96 logger.write(b"one\n").unwrap();
93 logger.write(b"two\n").unwrap(); 97 logger.write(b"two\n").unwrap();
94 logger.write(b"3\n").unwrap(); 98 logger.write(b"3\n").unwrap();
95 logger.write(b"four\n").unwrap(); 99 logger.write(b"four\n").unwrap();
96 logger.write(b"five\n").unwrap(); 100 logger.write(b"five\n").unwrap();