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

11
snitch-lib/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "snitch-lib"
version = "1.0.0"
edition = "2021"
[dependencies]
log = "0.4"
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "serde_json", "blocking", "json"] }
eyre = "0.6.12"

View File

@ -0,0 +1,29 @@
use log::{Level, LevelFilter, Metadata, Record};
use snitch_lib::SnitchLogger;
struct SimpleLogger;
impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
eprintln!("{} - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}
fn main() {
let logger = SnitchLogger::new(SimpleLogger, "http://localhost:8000", "snitch-lib-example");
let logger = Box::leak(Box::new(logger));
log::set_logger(logger).expect("set logger");
log::set_max_level(LevelFilter::Info);
log::info!("Everything should work, let's try it!");
log::warn!("This is your last warning!");
log::error!("Error! Error!");
}

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

@ -0,0 +1,78 @@
use std::process::Command;
use log::{Level, Log, Metadata, Record};
use reqwest::blocking;
mod message;
pub use message::{LogMsg, Severity};
pub struct SnitchLogger<L: Log> {
wrapped: L,
url: String,
service_name: String,
hostname: String,
}
impl<L: Log> SnitchLogger<L> {
pub fn new(wrapped: L, url: impl Into<String>, service_name: impl Into<String>) -> Self {
SnitchLogger {
wrapped,
url: url.into(),
service_name: service_name.into(),
hostname: probe_hostname(),
}
}
}
impl<L: Log> Log for SnitchLogger<L> {
fn enabled(&self, metadata: &Metadata) -> bool {
self.wrapped.enabled(metadata) || metadata.level() <= Level::Warn
}
fn log(&self, record: &Record) {
self.wrapped.log(record);
let severity = match record.metadata().level() {
Level::Error => Severity::Error,
Level::Warn => Severity::Warning,
_ => return,
};
let record = LogMsg {
severity,
file: record.file().map(String::from),
line: record.line(),
..LogMsg::new(
self.service_name.clone(),
record.args().to_string(),
self.hostname.clone(),
)
};
let client = blocking::Client::new();
if let Err(e) = client.post(&self.url).json(&record).send() {
// TODO: log error (without sending it)
eprintln!("failed to send log record: {e:?}");
}
}
fn flush(&self) {
self.wrapped.flush();
}
}
pub fn probe_hostname() -> String {
let output = Command::new("uname").arg("-n").output().ok();
output
.as_ref()
.and_then(|output| std::str::from_utf8(&output.stdout).ok())
.unwrap_or("<unknown_hostname>")
.to_string()
}
impl<L: Log> Drop for SnitchLogger<L> {
fn drop(&mut self) {
self.flush();
}
}

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()),
})
}
}