From 76886622ece1dc3b4ca1831b1a06fa2d2af115bb Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 3 Jun 2023 23:37:09 -0400 Subject: [PATCH] Use tracing logger --- common/src/cava.rs | 15 ++++++------- common/src/final_ring.rs | 5 +++-- common/src/pattern/visualizer.rs | 3 ++- lunatic-webui/src/main.rs | 2 -- src/main.rs | 5 +++-- src/ui.rs | 2 +- webui/Cargo.toml | 1 + webui/src/lib.rs | 36 +++++++++++++++++--------------- 8 files changed, 37 insertions(+), 32 deletions(-) diff --git a/common/src/cava.rs b/common/src/cava.rs index 8b609f7..4bcf78d 100644 --- a/common/src/cava.rs +++ b/common/src/cava.rs @@ -1,4 +1,5 @@ use crossbeam_channel::{select, unbounded, Sender}; +use tracing::{info, error}; use std::{ fs::File, io::Write, @@ -35,7 +36,7 @@ impl Cava { .spawn() .map_err(|e| PatternError::CommandNotFound(e.to_string()))?; - eprintln!("New cava process spawned"); + info!("New cava process spawned"); // Send cava stderr to our stderr // TODO: Can we just remove the stderr(piped) above? @@ -47,10 +48,10 @@ impl Cava { Ok(cava_process) } pub fn new(num_bars: u16) -> PatternResult { - eprintln!("Making config file"); + info!("Making config file"); let config_file = Self::write_cava_config_file(num_bars as usize * 2)?; - eprintln!("Starting cava"); + info!("Starting cava"); let mut cava_process = Self::spawn_cava(&config_file)?; // TODO: * 2 because it's a u16 @@ -80,15 +81,15 @@ impl Cava { true } recv(reader_error_rx) -> _ => { - eprintln!("Cava reader crashed. Will attempt a restart"); + error!("Cava reader crashed. Will attempt a restart"); false } }; // We cannot kill a thread. Instead, kill the cava process and expect the threads to die - eprintln!("Terminating cava process"); + info!("Terminating cava process"); if let Err(e) = cava_process.kill() { - eprintln!("Error trying to kill cava process: {e:?}"); + error!("Error trying to kill cava process: {e:?}"); } if should_terminate { @@ -101,7 +102,7 @@ impl Cava { cava_process = match Self::spawn_cava(&config_file) { Ok(c) => c, Err(e) => { - eprintln!("Could not spawn a new cava process: {e:?}"); + error!("Could not spawn a new cava process: {e:?}"); break; } } diff --git a/common/src/final_ring.rs b/common/src/final_ring.rs index 09d14d4..21198ff 100644 --- a/common/src/final_ring.rs +++ b/common/src/final_ring.rs @@ -1,4 +1,5 @@ use parking_lot::Mutex; +use tracing::{info, error}; use std::io::{Read, Result}; #[derive(Debug)] @@ -9,7 +10,7 @@ pub struct FinalRing { impl FinalRing { pub fn new(size: usize) -> Self { - eprintln!("Initializing with size: {size}"); + info!("Initializing FinalRing with size: {size}"); Self { size, inner: Mutex::new(vec![0; size]), @@ -24,7 +25,7 @@ impl FinalRing { let mut buf = vec![0; self.size]; loop { if let Err(e) = read.read_exact(&mut buf) { - eprintln!("Error reading: {e:?}"); + error!("Error reading: {e:?}"); break Err(e); } diff --git a/common/src/pattern/visualizer.rs b/common/src/pattern/visualizer.rs index 2c38b4d..6eb0995 100644 --- a/common/src/pattern/visualizer.rs +++ b/common/src/pattern/visualizer.rs @@ -1,4 +1,5 @@ use std::collections::VecDeque; +use tracing::info; use super::{ColorIterator, FormRender, Pattern, PatternError, PatternResult}; use crate::{ @@ -70,7 +71,7 @@ impl Pattern for Visualizer { self.lights_buf_max = color::stretch(&RAINBOW[..], num_lights as usize).collect(); - eprintln!("Visualizer got lights buf max: {:?}", self.lights_buf_max); + info!("Visualizer got lights buf max: {:?}", self.lights_buf_max); Ok(()) } diff --git a/lunatic-webui/src/main.rs b/lunatic-webui/src/main.rs index beb561f..bdc420c 100644 --- a/lunatic-webui/src/main.rs +++ b/lunatic-webui/src/main.rs @@ -3,8 +3,6 @@ use submillisecond::{router, static_router, Application}; use submillisecond_live_view::prelude::*; fn main() -> std::io::Result<()> { - println!("Hello, world!"); - Application::new(router! { "/" => Counter::handler("index.html", "#app") "/static" => static_router!("./static") diff --git a/src/main.rs b/src/main.rs index 9ed3204..92674d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ use common::{ error::{self, ProgramError, ProgramResult}, strip::Message, }; +use tracing::info; use std::{ sync::mpsc::{channel, Sender}, thread, @@ -72,8 +73,8 @@ fn main() -> ProgramResult<()> { loop { match message_rx.recv() { - Ok(error::Message::String(s)) => println!("\r{s}"), - Ok(error::Message::Error(e)) => println!("\rError!! {e:?}"), + Ok(error::Message::String(s)) => info!("\r{s}"), + Ok(error::Message::Error(e)) => info!("\rError!! {e:?}"), Ok(error::Message::Terminated) => { // First, try to turn the strip off let _ = strip_tx.send(Message::Quit); diff --git a/src/ui.rs b/src/ui.rs index 7663d3e..7e0dd83 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -13,7 +13,7 @@ pub fn console_ui_loop( loop { let line = get_line(message_tx, "Command (cfqs)")?; if let Err(msg) = parse_cmd(strip_tx, &line) { - println!("Command error: {msg}"); + error!("Command error: {msg}"); } } } diff --git a/webui/Cargo.toml b/webui/Cargo.toml index 2ec7da4..3ada5d3 100644 --- a/webui/Cargo.toml +++ b/webui/Cargo.toml @@ -15,3 +15,4 @@ serde_json = "1" live-view = { path = "./liveview-rust/" } askama = "0.10.5" common = { path = "../common" } +tracing = "0.1.37" diff --git a/webui/src/lib.rs b/webui/src/lib.rs index 7b231ce..01c3dd0 100644 --- a/webui/src/lib.rs +++ b/webui/src/lib.rs @@ -1,7 +1,9 @@ mod template; use actix_web::HttpResponse; use actix_web_actors::ws; -use common::{error, pattern, strip}; +use common::{ pattern, strip}; +use tracing::error; +use tracing::info; use live_view::{LiveView, StateSocket, Template}; use std::str::FromStr; use template::{AppTemplate, ControlTemplate}; @@ -26,7 +28,7 @@ async fn set_color_json( data: web::Data, params: web::Json, ) -> Result { - println!("Got params: {params:?}"); + info!("Got params: {params:?}"); data.strip_tx .lock() .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to get a lock"))? @@ -37,10 +39,10 @@ async fn set_color_json( #[actix_web::main] pub async fn start( - message_tx: Sender, + message_tx: Sender, strip_tx: Sender, ) -> std::io::Result<()> { - let _drop = message_tx.send(error::Message::String(String::from("Starting webui"))); + let _drop = message_tx.send(common::error::Message::String(String::from("Starting webui"))); HttpServer::new(move || { App::new() .data(AppState { @@ -50,14 +52,14 @@ pub async fn start( web::scope("/api") .app_data( JsonConfig::default().error_handler(|err: JsonPayloadError, _req| { - // let _drop = message_tx.send(error::Message::String(format!("JSON error: {:?}", err))); - println!("JSON error: {err:?}"); + // let _drop = message_tx.send(common::error::Message::String(format!("JSON error: {:?}", err))); + error!("JSON error: {err:?}"); err.into() }), ) .app_data(web::FormConfig::default().error_handler( |err: UrlencodedError, _req| { - println!("{err:?}"); + error!("{err:?}"); err.into() }, )) @@ -80,7 +82,7 @@ async fn initial_load(data: web::Data, _req: HttpRequest) -> impl Resp } .render() .map_err(|e| { - println!("Internal error e found: {e:?}"); + error!("Internal error e found: {e:?}"); ErrorInternalServerError(e) })?, // ..AppTemplate::default() @@ -98,7 +100,7 @@ async fn start_socket( ) -> impl Responder { let mut live_view: LiveView = LiveView::default(); live_view.on_input("change-template", |event, state| { - println!("Got change template event: {event:?}"); + info!("Got change template event: {event:?}"); let template_name = event.data.as_ref()?; let params = pattern::Parameters::from_str(template_name).ok()?; @@ -107,7 +109,7 @@ async fn start_socket( state .render() .map(|s| { - println!("{s}"); + info!("{s}"); s }) .map_err(|e| { @@ -118,30 +120,30 @@ async fn start_socket( }); live_view.on_submit("form", |event, state| { - // println!("(submit) Form submit: {:?}", event); + // info!("(submit) Form submit: {:?}", event); - println!( + info!( "Current value - {:?}", serde_json::to_string(&state.parameters) ); - println!("Form data: {:?}", event.data); + info!("Form data: {:?}", event.data); let p: pattern::Parameters = serde_json::from_str(event.data.as_ref()?) .map_err(|e| { - eprintln!("Error parsing: {e:?}"); + error!("Error parsing: {e:?}"); e }) .ok()?; state.parameters = p; - println!("Set state parameters to: {:?}", state.parameters); + info!("Set state parameters to: {:?}", state.parameters); state .strip_tx .as_ref()? .lock() .map_err(|_| { - println!( + info!( "{:?}", io::Error::new(io::ErrorKind::Other, "Failed to get a lock") ) @@ -149,7 +151,7 @@ async fn start_socket( .ok()? .send(strip::Message::ChangePattern(state.parameters.to_pattern())) .map_err(|_| { - println!( + info!( "{:?}", io::Error::new(io::ErrorKind::Other, "Failed to send to channel") )