space2 is now debugging better
This commit is contained in:
@@ -4,26 +4,35 @@ use client::run_ui;
|
||||
use client::utils::debug_logger::UiDebugWriter;
|
||||
use dotenvy::dotenv;
|
||||
use anyhow::Result;
|
||||
use tracing_subscriber;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use std::env;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
#[cfg(feature = "ui-debug")]
|
||||
{
|
||||
// If ui-debug is on, set up our custom writer.
|
||||
let writer = UiDebugWriter::new();
|
||||
tracing_subscriber::fmt()
|
||||
.with_level(false) // Don't show INFO, ERROR, etc.
|
||||
.with_target(false) // Don't show the module path.
|
||||
.without_time() // This is the correct and simpler method.
|
||||
.with_writer(move || writer.clone())
|
||||
.init();
|
||||
use std::sync::Once;
|
||||
static INIT_LOGGER: Once = Once::new();
|
||||
|
||||
INIT_LOGGER.call_once(|| {
|
||||
let writer = UiDebugWriter::new();
|
||||
let _ = tracing_subscriber::fmt()
|
||||
.with_max_level(tracing::Level::DEBUG)
|
||||
.with_target(false)
|
||||
.without_time()
|
||||
.with_writer(move || writer.clone())
|
||||
// Filter out noisy grpc/h2 internals
|
||||
.with_env_filter("client=debug,tonic=info,h2=info,tower=info")
|
||||
.try_init();
|
||||
|
||||
client::utils::debug_logger::spawn_file_logger();
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ui-debug"))]
|
||||
{
|
||||
if env::var("ENABLE_TRACING").is_ok() {
|
||||
tracing_subscriber::fmt::init();
|
||||
let _ = tracing_subscriber::fmt::try_init();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
// client/src/utils/debug_logger.rs
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::VecDeque; // <-- FIX: Import VecDeque
|
||||
use std::io;
|
||||
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)>>> =
|
||||
@@ -27,11 +30,11 @@ impl UiDebugWriter {
|
||||
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);
|
||||
let trimmed_message = message.trim().to_string();
|
||||
let is_error = trimmed_message.starts_with("ERROR");
|
||||
// Add the new message to the back of the queue
|
||||
buffer.push_back((trimmed_message, is_error));
|
||||
let message = String::from_utf8_lossy(buf).trim().to_string();
|
||||
let is_error = message.starts_with("ERROR");
|
||||
|
||||
// Always keep in memory
|
||||
buffer.push_back((message, is_error));
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
@@ -44,3 +47,22 @@ impl io::Write for UiDebugWriter {
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user