feat: add a buffering feature to AdminLog

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2025-07-28 09:18:11 +03:00
parent 1f85b1b9e0
commit 6af9c661aa
+20
View File
@@ -1432,6 +1432,7 @@ pub mod helper {
filename: Option<PathBuf>,
file: Option<File>,
stderr: bool,
buffer: Option<Vec<u8>>,
}
impl AdminLog {
@@ -1447,9 +1448,25 @@ pub mod helper {
filename: None,
file: None,
stderr: true,
buffer: None,
}
}
/// Capture output into a buffer.
pub fn capture() -> Self {
Self {
filename: None,
file: None,
stderr: false,
buffer: Some(vec![]),
}
}
/// Return current buffer created by `capture`.
pub fn capture_buffer(&self) -> Option<&[u8]> {
self.buffer.as_deref()
}
/// Create an admin log that writes to a named file.
pub fn open(filename: &Path) -> Result<Self, LogError> {
let file = OpenOptions::new()
@@ -1461,6 +1478,7 @@ pub mod helper {
filename: Some(filename.into()),
file: Some(file),
stderr: false,
buffer: None,
})
}
@@ -1483,6 +1501,8 @@ pub mod helper {
std::io::stderr()
.write_all(msg.as_bytes())
.map_err(LogError::WriteLogStderr)?;
} else if let Some(buf) = self.buffer.as_mut() {
buf.extend_from_slice(msg.as_bytes());
}
Ok(())
}