mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
feat: initial commit of SteamFrame FPV Control Center
This commit is contained in:
commit
4459f5ffa4
18 files changed
+8571
No files matched your search
+101
@@ -0,0 +1,101 @@
|
||||
use chrono::Local;
|
||||
use log::{Level, LevelFilter, Log, Metadata, Record};
|
||||
use parking_lot::Mutex;
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
const MAX_LOG_ENTRIES: usize = 1000;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LogEntry {
|
||||
pub timestamp: String,
|
||||
pub level: Level,
|
||||
pub target: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct LogBuffer {
|
||||
pub entries: VecDeque<LogEntry>,
|
||||
pub error_count: usize,
|
||||
pub warn_count: usize,
|
||||
}
|
||||
|
||||
/// Logger centralisé capturant tous les événements et erreurs pour l'interface graphique
|
||||
/// et les enregistrant simultanément dans `steamframe_fpv.log`.
|
||||
pub struct GuiLogger {
|
||||
buffer: Arc<Mutex<LogBuffer>>,
|
||||
log_file_path: String,
|
||||
}
|
||||
|
||||
impl GuiLogger {
|
||||
pub fn init(log_file_path: &str) -> Arc<Mutex<LogBuffer>> {
|
||||
let buffer = Arc::new(Mutex::new(LogBuffer::default()));
|
||||
let logger = Box::new(Self {
|
||||
buffer: Arc::clone(&buffer),
|
||||
log_file_path: log_file_path.to_string(),
|
||||
});
|
||||
|
||||
if log::set_boxed_logger(logger).is_ok() {
|
||||
log::set_max_level(LevelFilter::Info);
|
||||
}
|
||||
|
||||
buffer
|
||||
}
|
||||
}
|
||||
|
||||
impl Log for GuiLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= Level::Info
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if !self.enabled(record.metadata()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let now = Local::now().format("%H:%M:%S%.3f").to_string();
|
||||
let entry = LogEntry {
|
||||
timestamp: now.clone(),
|
||||
level: record.level(),
|
||||
target: record.target().to_string(),
|
||||
message: format!("{}", record.args()),
|
||||
};
|
||||
|
||||
// Affichage console standard
|
||||
eprintln!(
|
||||
"[{}] [{:<5}] ({}) {}",
|
||||
entry.timestamp, entry.level, entry.target, entry.message
|
||||
);
|
||||
|
||||
// Écriture persistante sur disque pour diagnostic après crash éventuel
|
||||
if let Ok(mut file) = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&self.log_file_path)
|
||||
{
|
||||
let _ = writeln!(
|
||||
file,
|
||||
"[{}] [{:<5}] ({}) {}",
|
||||
entry.timestamp, entry.level, entry.target, entry.message
|
||||
);
|
||||
}
|
||||
|
||||
// Ajout dans le tampon de l'interface graphique
|
||||
let mut lock = self.buffer.lock();
|
||||
if record.level() == Level::Error {
|
||||
lock.error_count += 1;
|
||||
} else if record.level() == Level::Warn {
|
||||
lock.warn_count += 1;
|
||||
}
|
||||
|
||||
if lock.entries.len() >= MAX_LOG_ENTRIES {
|
||||
lock.entries.pop_front();
|
||||
}
|
||||
lock.entries.push_back(entry);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
Reference in new issue
Block a user