outputting to the status line
This commit is contained in:
46
client/src/utils/debug_logger.rs
Normal file
46
client/src/utils/debug_logger.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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()
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
// src/utils/mod.rs
|
||||
|
||||
pub mod columns;
|
||||
pub mod debug_logger;
|
||||
pub use columns::*;
|
||||
pub use debug_logger::*;
|
||||
|
||||
Reference in New Issue
Block a user