Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
feat: add a time stamp to each log message
Lars Wirzenius committed 1 year ago
commit f3d4e4a99b582d116c888e23b7915ff4a83ee0f4
parent d115185
1 file changed +15 -0
modified src/logfile.rs
@@ -4,6 +4,8 @@ use std::{
    path::{Path, PathBuf},
};

+
use time::{macros::format_description, OffsetDateTime};
+

#[derive(Debug)]
pub struct AdminLog {
    filename: PathBuf,
@@ -24,6 +26,9 @@ impl AdminLog {
    }

    pub fn writeln(&mut self, text: &str) -> Result<(), LogError> {
+
        self.write("[")?;
+
        self.write(&now()?)?;
+
        self.write("] ")?;
        self.write(text)?;
        self.write("\n")?;
        Ok(())
@@ -36,6 +41,13 @@ impl AdminLog {
    }
}

+
fn now() -> Result<String, LogError> {
+
    let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
+
    OffsetDateTime::now_utc()
+
        .format(fmt)
+
        .map_err(LogError::TimeFormat)
+
}
+

#[derive(Debug, thiserror::Error)]
pub enum LogError {
    #[error("failed to open log file {0}")]
@@ -43,4 +55,7 @@ pub enum LogError {

    #[error("failed to write to log file {0}")]
    WriteLogFile(PathBuf, #[source] std::io::Error),
+

+
    #[error("failed to format time stamp")]
+
    TimeFormat(#[source] time::error::Format),
}