From 3a3d635adddf5cb16a93827e688e061613a083d7 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 5 Dec 2025 19:29:52 +0000 Subject: reworked logging --- examples/binary_sensor.rs | 2 +- examples/button.rs | 2 +- examples/common/mod.rs | 8 ++++++++ examples/common/std_async_tcp.rs | 10 +++++++++- examples/switch.rs | 3 +-- 5 files changed, 20 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/binary_sensor.rs b/examples/binary_sensor.rs index 7363a6c..27cfdb5 100644 --- a/examples/binary_sensor.rs +++ b/examples/binary_sensor.rs @@ -41,7 +41,7 @@ async fn main_task(spawner: Spawner) { async fn binary_sensor_class(mut switch: embassy_ha::BinarySensor<'static>) { loop { let state = switch.toggle(); - println!("state = {}", state); + tracing::info!("state = {}", state); Timer::after_secs(2).await; } } diff --git a/examples/button.rs b/examples/button.rs index d1d2159..e3d7086 100644 --- a/examples/button.rs +++ b/examples/button.rs @@ -31,7 +31,7 @@ async fn main_task(spawner: Spawner) { async fn button_task(mut button: embassy_ha::Button<'static>) { loop { button.pressed().await; - println!("The button has been pressed"); + tracing::info!("The button has been pressed"); } } diff --git a/examples/common/mod.rs b/examples/common/mod.rs index fd61e9d..db97463 100644 --- a/examples/common/mod.rs +++ b/examples/common/mod.rs @@ -9,6 +9,14 @@ pub static EXECUTOR: StaticCell = StaticCell::new(); macro_rules! example_main { () => { fn main() { + // Initialize tracing if tracing feature is enabled + #[cfg(feature = "tracing")] + { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::DEBUG) + .init(); + } + let executor = common::EXECUTOR.init(Executor::new()); executor.run(|spawner| { spawner.must_spawn(main_task(spawner)); diff --git a/examples/common/std_async_tcp.rs b/examples/common/std_async_tcp.rs index bd97fa9..10a9fd8 100644 --- a/examples/common/std_async_tcp.rs +++ b/examples/common/std_async_tcp.rs @@ -1,4 +1,5 @@ use std::{ + future::Future, io::{Read, Write}, net::{TcpStream, ToSocketAddrs}, sync::{Arc, Mutex}, @@ -16,7 +17,9 @@ pub struct AsyncTcp { impl AsyncTcp { pub fn connect(addr: impl ToSocketAddrs) -> Self { + tracing::info!("Connecting to TCP server"); let stream = TcpStream::connect(addr).expect("failed to connect to remote"); + tracing::info!("TCP connection established"); let mut read_stream = stream.try_clone().unwrap(); let mut write_stream = stream; @@ -34,9 +37,10 @@ impl AsyncTcp { std::mem::take(&mut *buffer) }; if !buffer.is_empty() { - println!("writing {} bytes", buffer.len()); + let len = buffer.len(); write_stream.write_all(&buffer).unwrap(); write_stream.flush().unwrap(); + tracing::debug!("Wrote {} bytes to TCP stream", len); } else { std::thread::park(); } @@ -52,9 +56,11 @@ impl AsyncTcp { loop { let n = read_stream.read(&mut scratch).unwrap(); if n == 0 { + tracing::warn!("TCP stream closed (EOF)"); panic!("EOF"); } + tracing::debug!("Read {} bytes from TCP stream", n); { let mut buffer = read_buffer.lock().unwrap(); buffer.extend_from_slice(&scratch[..n]); @@ -79,6 +85,7 @@ impl embedded_io_async::ErrorType for AsyncTcp { impl embedded_io_async::Write for AsyncTcp { async fn write(&mut self, buf: &[u8]) -> Result { + tracing::trace!("Queueing {} bytes for write", buf.len()); { let mut buffer = self.write_buffer.lock().unwrap(); buffer.extend_from_slice(buf); @@ -116,6 +123,7 @@ impl embedded_io_async::Read for AsyncTcp { let copy_n = buf.len().min(buffer.len()); buf[..copy_n].copy_from_slice(&buffer[..copy_n]); buffer.drain(..copy_n); + tracing::trace!("Async read returned {} bytes", copy_n); return Ok(copy_n); } } diff --git a/examples/switch.rs b/examples/switch.rs index 0571758..67767d7 100644 --- a/examples/switch.rs +++ b/examples/switch.rs @@ -41,8 +41,7 @@ async fn main_task(spawner: Spawner) { async fn switch_task(mut switch: embassy_ha::Switch<'static>) { loop { let state = switch.wait().await; - - println!("state = {}", state); + tracing::info!("state = {}", state); Timer::after_secs(1).await; } } -- cgit