79 lines
2.1 KiB
Rust
79 lines
2.1 KiB
Rust
// client/src/utils/debug_logger.rs
|
|
use lazy_static::lazy_static;
|
|
use std::collections::VecDeque; // <-- FIX: Import VecDeque
|
|
use std::io::{self, Write};
|
|
use std::sync::{Arc, Mutex}; // <-- FIX: Import Mutex
|
|
use std::fs::OpenOptions;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
lazy_static! {
|
|
static ref UI_DEBUG_BUFFER: Arc<Mutex<VecDeque<(String, bool)>>> =
|
|
Arc::new(Mutex::new(VecDeque::from([(String::from("Logger initialized..."), false)])));
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct UiDebugWriter;
|
|
|
|
impl Default for UiDebugWriter {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl UiDebugWriter {
|
|
pub fn new() -> Self {
|
|
Self
|
|
}
|
|
}
|
|
|
|
impl io::Write for UiDebugWriter {
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
let mut buffer = UI_DEBUG_BUFFER.lock().unwrap();
|
|
let message = String::from_utf8_lossy(buf).trim().to_string();
|
|
let is_error = message.starts_with("ERROR");
|
|
|
|
// Keep in memory for UI
|
|
buffer.push_back((message.clone(), is_error));
|
|
|
|
// ALSO log directly to file (non-blocking best effort)
|
|
if let Ok(mut file) = OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open("ui_debug.log")
|
|
{
|
|
let _ = writeln!(file, "{message}");
|
|
}
|
|
|
|
Ok(buf.len())
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
// A public function to pop the next message from the front of the queue.
|
|
pub fn pop_next_debug_message() -> Option<(String, bool)> {
|
|
UI_DEBUG_BUFFER.lock().unwrap().pop_front()
|
|
}
|
|
|
|
/// spawn a background thread that keeps draining UI_DEBUG_BUFFER
|
|
/// and writes messages into ui_debug.log continuously
|
|
pub fn spawn_file_logger() {
|
|
thread::spawn(|| loop {
|
|
// pop one message if present
|
|
if let Some((msg, _)) = pop_next_debug_message() {
|
|
if let Ok(mut file) = OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open("ui_debug.log")
|
|
{
|
|
let _ = writeln!(file, "{msg}");
|
|
}
|
|
}
|
|
// small sleep to avoid burning CPU
|
|
thread::sleep(Duration::from_millis(50));
|
|
});
|
|
}
|