47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
// client/src/utils/debug_logger.rs
|
|
use lazy_static::lazy_static;
|
|
use std::io;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
lazy_static! {
|
|
static ref UI_DEBUG_BUFFER: Arc<Mutex<String>> =
|
|
Arc::new(Mutex::new(String::from("Logger initialized...")));
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct UiDebugWriter;
|
|
|
|
impl Default for UiDebugWriter {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl UiDebugWriter {
|
|
pub fn new() -> Self {
|
|
Self
|
|
}
|
|
}
|
|
|
|
// Implement the io::Write trait for our writer.
|
|
// tracing_subscriber can use any type that implements `MakeWriter`.
|
|
// A simple way to do this is to have our writer implement `io::Write`.
|
|
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);
|
|
// We just want the latest message, trimmed of whitespace.
|
|
*buffer = message.trim().to_string();
|
|
Ok(buf.len())
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
// A public function to safely get the latest message from anywhere in the app.
|
|
pub fn get_latest_debug_message() -> String {
|
|
UI_DEBUG_BUFFER.lock().unwrap().clone()
|
|
}
|