Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat(src/msg.rs): allow logging to stderr in AdminLog
Merged liw opened 1 year ago

Signed-off-by: Lars Wirzenius liw@liw.fi

1 file changed +21 -2 68a7a596 5038769a
modified src/msg.rs
@@ -1365,15 +1365,25 @@ pub mod helper {
    pub struct AdminLog {
        filename: Option<PathBuf>,
        file: Option<File>,
+
        stderr: bool,
    }

    impl AdminLog {
-
        /// Create an admin log that doesn't write to a file, but to
-
        /// stderr.
+
        /// Create an admin log that doesn't write to a file, and
+
        /// neither to stderr.
        pub fn null() -> Self {
            Self::default()
        }

+
        /// Create an admin log that writes to stderr.
+
        pub fn stderr() -> Self {
+
            Self {
+
                filename: None,
+
                file: None,
+
                stderr: true,
+
            }
+
        }
+

        /// Create an admin log that writes to a named file.
        pub fn open(filename: &Path) -> Result<Self, LogError> {
            let file = OpenOptions::new()
@@ -1384,6 +1394,7 @@ pub mod helper {
            Ok(Self {
                filename: Some(filename.into()),
                file: Some(file),
+
                stderr: false,
            })
        }

@@ -1402,6 +1413,10 @@ pub mod helper {
                #[allow(clippy::unwrap_used)] // we know it's OK
                file.write_all(msg.as_bytes())
                    .map_err(|e| LogError::WriteLogFile(self.filename.clone().unwrap(), e))?;
+
            } else if self.stderr {
+
                std::io::stderr()
+
                    .write_all(msg.as_bytes())
+
                    .map_err(LogError::WriteLogStderr)?;
            }
            Ok(())
        }
@@ -1425,6 +1440,10 @@ pub mod helper {
        #[error("failed to write to log file {0}")]
        WriteLogFile(PathBuf, #[source] std::io::Error),

+
        /// Can't write to file.
+
        #[error("failed to write to log file {0}")]
+
        WriteLogStderr(#[source] std::io::Error),
+

        /// Can' format time stamp.
        #[error("failed to format time stamp")]
        TimeFormat(#[source] time::error::Format),