STUFUFUFUFF

This commit is contained in:
2024-09-21 14:08:24 +02:00
parent d180e72373
commit 7110a9e8f6
21 changed files with 1190 additions and 370 deletions

78
snitch-lib/src/message.rs Normal file
View File

@ -0,0 +1,78 @@
use std::str::FromStr;
use chrono::{DateTime, Local};
use eyre::{eyre, ContextCompat};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Severity {
Fatal,
Error,
Warning,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LogMsg {
/// The time that the error was reported.
pub time: Option<DateTime<Local>>,
pub severity: Severity,
/// The service which reported the error.
pub service: String,
/// The log message.
pub message: String,
/// The host that the service is running on.
pub hostname: String,
pub file: Option<String>,
pub line: Option<u32>,
}
impl LogMsg {
pub fn new(service: String, message: String, hostname: String) -> Self {
Self {
time: Some(Local::now()),
severity: Severity::Error,
service,
message,
hostname,
file: None,
line: None,
}
}
}
impl Severity {
pub fn as_str(&self) -> &'static str {
match self {
Severity::Fatal => "FATAL",
Severity::Error => "ERROR",
Severity::Warning => "WARNING",
}
}
}
impl FromStr for Severity {
type Err = eyre::Report;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let err = || eyre!("{s:?} is not a valid severity level");
let first_char = s
.trim()
.chars()
.next()
.wrap_err_with(err)?
.to_ascii_lowercase();
Ok(match first_char {
'f' => Severity::Fatal,
'e' => Severity::Error,
'w' => Severity::Warning,
_ => return Err(err()),
})
}
}