mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-10-18 19:30:29 +08:00
feat: add a buffering feature to AdminLog
Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
+20
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user