Use logging

This commit is contained in:
2025-01-14 23:33:30 -05:00
parent 102aff1580
commit f031bb17b2
7 changed files with 148 additions and 17 deletions
+9 -5
View File
@@ -6,6 +6,7 @@ use egui::{
ViewportCommand, ViewportId, ViewportInfo,
};
use gilrs::{EventType, Gilrs};
use tracing::{error, warn};
use winit::{
application::ApplicationHandler,
event::WindowEvent,
@@ -230,8 +231,8 @@ impl ApplicationHandler<UserEvent> for Application {
fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
let (sender, receiver) = oneshot::channel();
if self.client.send_command(EmulatorCommand::Exit(sender)) {
if let Err(err) = receiver.recv_timeout(Duration::from_secs(5)) {
eprintln!("could not gracefully exit: {}", err);
if let Err(error) = receiver.recv_timeout(Duration::from_secs(5)) {
error!(%error, "could not gracefully exit.");
}
}
}
@@ -434,9 +435,12 @@ fn create_window_and_state(
}
fn process_gamepad_input(mappings: MappingProvider, proxy: EventLoopProxy<UserEvent>) {
let Ok(mut gilrs) = Gilrs::new() else {
eprintln!("could not connect gamepad listener");
return;
let mut gilrs = match Gilrs::new() {
Ok(gilrs) => gilrs,
Err(error) => {
warn!(%error, "could not connect gamepad listener");
return;
}
};
while let Some(event) = gilrs.next_event_blocking(None) {
if event.event == EventType::Connected {
+2 -1
View File
@@ -4,6 +4,7 @@ use anyhow::{bail, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use itertools::Itertools;
use rubato::{FftFixedInOut, Resampler};
use tracing::error;
pub struct Audio {
#[allow(unused)]
@@ -54,7 +55,7 @@ impl Audio {
}
chunk.commit_all();
},
move |err| eprintln!("stream error: {err}"),
move |error| error!(%error, "stream error"),
None,
)?;
stream.play()?;
+3 -2
View File
@@ -15,6 +15,7 @@ use anyhow::Result;
use atomic::Atomic;
use bytemuck::NoUninit;
use egui_toast::{Toast, ToastKind, ToastOptions};
use tracing::{error, warn};
use crate::{audio::Audio, graphics::TextureSink};
use shrooms_vb_core::{Sim, StopReason, EXPECTED_FRAME_SIZE};
@@ -608,7 +609,7 @@ impl Emulator {
return;
}
}
eprintln!("{}", message);
error!("{}", message);
}
}
@@ -704,7 +705,7 @@ impl EmulatorClient {
match self.queue.send(command) {
Ok(()) => true,
Err(err) => {
eprintln!(
warn!(
"could not send command {:?} as emulator is shut down",
err.0
);
+13 -4
View File
@@ -12,6 +12,7 @@ use tokio::{
select,
sync::{mpsc, oneshot},
};
use tracing::{debug, enabled, error, info, Level};
use crate::emulator::{
DebugEvent, DebugStopReason, EmulatorClient, EmulatorCommand, SimId, VBWatchpointType,
@@ -78,15 +79,19 @@ async fn run_server(
port: u16,
status: &Mutex<GdbServerStatus>,
) {
info!("Connecting to debugger on port {port}...");
let Some(stream) = try_connect(port, status).await else {
return;
};
info!("Connected!");
let mut connection = GdbConnection::new(sim_id, client);
match connection.run(stream).await {
Ok(()) => {
info!("Finished debugging.");
*status.lock().unwrap() = GdbServerStatus::Stopped;
}
Err(error) => {
error!(%error, "Error from debugger.");
*status.lock().unwrap() = GdbServerStatus::Error(error.to_string());
}
}
@@ -97,6 +102,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
let listener = match TcpListener::bind(("127.0.0.1", port)).await {
Ok(l) => l,
Err(err) => {
error!(%err, "Could not open port.");
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
return None;
}
@@ -107,6 +113,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
Some(stream)
}
Err(err) => {
error!(%err, "Could not connect to debugger.");
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
None
}
@@ -169,9 +176,11 @@ impl GdbConnection {
};
if let Some(res) = response {
let buffer = res.finish();
match std::str::from_utf8(&buffer) {
Ok(text) => println!("response: {text}"),
Err(_) => println!("response: {buffer:02x?}"),
if enabled!(Level::DEBUG) {
match std::str::from_utf8(&buffer) {
Ok(text) => debug!("response: {text}"),
Err(_) => debug!("response: {buffer:02x?}"),
}
}
tx.write_all(&buffer).await?;
self.response_buf = Some(buffer);
@@ -196,7 +205,7 @@ impl GdbConnection {
}
fn handle_request(&mut self, mut req: Request<'_>) -> Result<Option<Response>> {
println!("received {:02x?}", req);
debug!("received {:02x?}", req);
if req.kind == RequestKind::Signal {
self.client
+13 -2
View File
@@ -8,6 +8,8 @@ use app::Application;
use clap::Parser;
use emulator::EmulatorBuilder;
use thread_priority::{ThreadBuilder, ThreadPriority};
use tracing::error;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
use winit::event_loop::{ControlFlow, EventLoop};
mod app;
@@ -29,6 +31,13 @@ struct Args {
debug_port: Option<u16>,
}
fn init_logger() {
let directives = std::env::var("RUST_LOG").unwrap_or("error,lemur=info".into());
let filter = EnvFilter::builder().parse_lossy(directives);
let layer = tracing_subscriber::fmt::layer().with_filter(filter);
tracing_subscriber::registry().with(layer).init();
}
fn set_panic_handler() {
std::panic::set_hook(Box::new(|info| {
let mut message = String::new();
@@ -76,6 +85,8 @@ fn set_process_priority_to_high() -> Result<()> {
}
fn main() -> Result<()> {
init_logger();
set_panic_handler();
#[cfg(windows)]
@@ -97,8 +108,8 @@ fn main() -> Result<()> {
.spawn_careless(move || {
let mut emulator = match builder.build() {
Ok(e) => e,
Err(err) => {
eprintln!("Error initializing emulator: {err}");
Err(error) => {
error!(%error, "Error initializing emulator");
process::exit(1);
}
};