STUFUFUFUFF
This commit is contained in:
78
snitch-lib/src/message.rs
Normal file
78
snitch-lib/src/message.rs
Normal 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()),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user