review and QA

This commit is contained in:
2026-08-15 22:49:18 +01:00
parent 3c7043562e
commit 7f01f1a341
10 changed files with 477 additions and 220 deletions
+30 -9
View File
@@ -1,8 +1,12 @@
// src/error_logger.rs
#![warn(clippy::all, clippy::pedantic)]
use chrono::{DateTime, Utc};
use serde::Serialize;
use std::fs::OpenOptions;
use std::io::Write;
use std::io::{BufWriter, Write};
use std::path::Path;
use std::sync::Mutex;
use url::Url;
#[derive(Serialize)]
@@ -15,6 +19,10 @@ pub struct ErrorEntry {
pub status_code: Option<u16>,
}
lazy_static::lazy_static! {
static ref LOG_WRITER: Mutex<Option<BufWriter<std::fs::File>>> = Mutex::new(None);
}
pub fn log_error(
log_path: &Path,
url: &Url,
@@ -38,14 +46,27 @@ pub fn log_error(
}
};
if let Ok(mut file) = OpenOptions::new().append(true).create(true).open(log_path) {
if let Err(e) = file.write_all(line.as_bytes()) {
log::error!("Failed to write error log: {}", e);
} else {
let _ = file.flush();
log::info!("LOGGED ERROR [{}]: {}", error_type, message);
// Buffered writer approach
{
let mut guard = LOG_WRITER.lock().unwrap();
match guard.as_mut() {
Some(writer) => {
if let Err(e) = writer.write_all(line.as_bytes()) {
log::error!("Failed to write error log: {}", e);
}
}
None => {
if let Ok(file) = OpenOptions::new().append(true).create(true).open(log_path) {
let mut writer = BufWriter::new(file);
let _ = writer.write_all(line.as_bytes());
let _ = writer.flush();
} else {
log::error!("Failed to open error log file: {}", log_path.display());
return;
}
}
}
} else {
log::error!("Failed to open error log file: {}", log_path.display());
}
log::info!("LOGGED ERROR [{}]: {}", error_type, message);
}