feat: buffer tracing logs before console init

This commit is contained in:
Ryan Walters
2025-09-01 17:22:22 -05:00
parent b1b03b0e9c
commit c79ba0d824
6 changed files with 184 additions and 14 deletions

View File

@@ -2,8 +2,6 @@
use crate::{app::App, constants::LOOP_TIME};
use tracing::info;
use tracing_error::ErrorLayer;
use tracing_subscriber::layer::SubscriberExt;
mod app;
mod asset;
@@ -23,14 +21,22 @@ mod texture;
/// This function initializes SDL, the window, the game state, and then enters
/// the main game loop.
pub fn main() {
// Setup tracing
let subscriber = tracing_subscriber::fmt()
.with_ansi(cfg!(not(target_os = "emscripten")))
.with_max_level(tracing::Level::DEBUG)
.finish()
.with(ErrorLayer::default());
// Setup buffered tracing subscriber that will buffer logs until console is ready
let switchable_writer = platform::tracing_buffer::setup_switchable_subscriber();
tracing::subscriber::set_global_default(subscriber).expect("Could not set global default");
// Log early to show buffering is working
tracing::debug!("Tracing subscriber initialized with buffering - logs will be buffered until console is ready");
// Initialize platform-specific console
tracing::debug!("Starting console initialization...");
platform::get_platform().init_console().expect("Could not initialize console");
tracing::debug!("Console initialization completed");
// Now that console is initialized, flush buffered logs and switch to direct output
tracing::debug!("Switching to direct logging mode and flushing buffer...");
if let Err(e) = switchable_writer.switch_to_direct_mode() {
tracing::warn!("Failed to flush buffered logs to console: {}", e);
}
let mut app = App::new().expect("Could not create app");