diff options
| author | diogo464 <[email protected]> | 2025-12-05 19:29:52 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-05 19:29:52 +0000 |
| commit | 3a3d635adddf5cb16a93827e688e061613a083d7 (patch) | |
| tree | e6be0a075f80d5cfb11b1882e0086727bfe699e9 /examples/common | |
| parent | b609a315e7921dcc712da6955890f4dc7c2c4b9f (diff) | |
reworked logging
Diffstat (limited to 'examples/common')
| -rw-r--r-- | examples/common/mod.rs | 8 | ||||
| -rw-r--r-- | examples/common/std_async_tcp.rs | 10 |
2 files changed, 17 insertions, 1 deletions
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<Executor> = StaticCell::new(); | |||
| 9 | macro_rules! example_main { | 9 | macro_rules! example_main { |
| 10 | () => { | 10 | () => { |
| 11 | fn main() { | 11 | fn main() { |
| 12 | // Initialize tracing if tracing feature is enabled | ||
| 13 | #[cfg(feature = "tracing")] | ||
| 14 | { | ||
| 15 | tracing_subscriber::fmt() | ||
| 16 | .with_max_level(tracing::Level::DEBUG) | ||
| 17 | .init(); | ||
| 18 | } | ||
| 19 | |||
| 12 | let executor = common::EXECUTOR.init(Executor::new()); | 20 | let executor = common::EXECUTOR.init(Executor::new()); |
| 13 | executor.run(|spawner| { | 21 | executor.run(|spawner| { |
| 14 | spawner.must_spawn(main_task(spawner)); | 22 | 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 @@ | |||
| 1 | use std::{ | 1 | use std::{ |
| 2 | future::Future, | ||
| 2 | io::{Read, Write}, | 3 | io::{Read, Write}, |
| 3 | net::{TcpStream, ToSocketAddrs}, | 4 | net::{TcpStream, ToSocketAddrs}, |
| 4 | sync::{Arc, Mutex}, | 5 | sync::{Arc, Mutex}, |
| @@ -16,7 +17,9 @@ pub struct AsyncTcp { | |||
| 16 | 17 | ||
| 17 | impl AsyncTcp { | 18 | impl AsyncTcp { |
| 18 | pub fn connect(addr: impl ToSocketAddrs) -> Self { | 19 | pub fn connect(addr: impl ToSocketAddrs) -> Self { |
| 20 | tracing::info!("Connecting to TCP server"); | ||
| 19 | let stream = TcpStream::connect(addr).expect("failed to connect to remote"); | 21 | let stream = TcpStream::connect(addr).expect("failed to connect to remote"); |
| 22 | tracing::info!("TCP connection established"); | ||
| 20 | let mut read_stream = stream.try_clone().unwrap(); | 23 | let mut read_stream = stream.try_clone().unwrap(); |
| 21 | let mut write_stream = stream; | 24 | let mut write_stream = stream; |
| 22 | 25 | ||
| @@ -34,9 +37,10 @@ impl AsyncTcp { | |||
| 34 | std::mem::take(&mut *buffer) | 37 | std::mem::take(&mut *buffer) |
| 35 | }; | 38 | }; |
| 36 | if !buffer.is_empty() { | 39 | if !buffer.is_empty() { |
| 37 | println!("writing {} bytes", buffer.len()); | 40 | let len = buffer.len(); |
| 38 | write_stream.write_all(&buffer).unwrap(); | 41 | write_stream.write_all(&buffer).unwrap(); |
| 39 | write_stream.flush().unwrap(); | 42 | write_stream.flush().unwrap(); |
| 43 | tracing::debug!("Wrote {} bytes to TCP stream", len); | ||
| 40 | } else { | 44 | } else { |
| 41 | std::thread::park(); | 45 | std::thread::park(); |
| 42 | } | 46 | } |
| @@ -52,9 +56,11 @@ impl AsyncTcp { | |||
| 52 | loop { | 56 | loop { |
| 53 | let n = read_stream.read(&mut scratch).unwrap(); | 57 | let n = read_stream.read(&mut scratch).unwrap(); |
| 54 | if n == 0 { | 58 | if n == 0 { |
| 59 | tracing::warn!("TCP stream closed (EOF)"); | ||
| 55 | panic!("EOF"); | 60 | panic!("EOF"); |
| 56 | } | 61 | } |
| 57 | 62 | ||
| 63 | tracing::debug!("Read {} bytes from TCP stream", n); | ||
| 58 | { | 64 | { |
| 59 | let mut buffer = read_buffer.lock().unwrap(); | 65 | let mut buffer = read_buffer.lock().unwrap(); |
| 60 | buffer.extend_from_slice(&scratch[..n]); | 66 | buffer.extend_from_slice(&scratch[..n]); |
| @@ -79,6 +85,7 @@ impl embedded_io_async::ErrorType for AsyncTcp { | |||
| 79 | 85 | ||
| 80 | impl embedded_io_async::Write for AsyncTcp { | 86 | impl embedded_io_async::Write for AsyncTcp { |
| 81 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 87 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 88 | tracing::trace!("Queueing {} bytes for write", buf.len()); | ||
| 82 | { | 89 | { |
| 83 | let mut buffer = self.write_buffer.lock().unwrap(); | 90 | let mut buffer = self.write_buffer.lock().unwrap(); |
| 84 | buffer.extend_from_slice(buf); | 91 | buffer.extend_from_slice(buf); |
| @@ -116,6 +123,7 @@ impl embedded_io_async::Read for AsyncTcp { | |||
| 116 | let copy_n = buf.len().min(buffer.len()); | 123 | let copy_n = buf.len().min(buffer.len()); |
| 117 | buf[..copy_n].copy_from_slice(&buffer[..copy_n]); | 124 | buf[..copy_n].copy_from_slice(&buffer[..copy_n]); |
| 118 | buffer.drain(..copy_n); | 125 | buffer.drain(..copy_n); |
| 126 | tracing::trace!("Async read returned {} bytes", copy_n); | ||
| 119 | return Ok(copy_n); | 127 | return Ok(copy_n); |
| 120 | } | 128 | } |
| 121 | } | 129 | } |
