Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: add a buffering feature to AdminLog
Lars Wirzenius committed 9 months ago
commit 6af9c661aaf93da72045e833dce3e9c1d1589b98
parent 1f85b1b
1 file changed +20 -0
modified src/msg.rs
@@ -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(())
        }