From 9ec28b95bb24e2f173b9d1c50315c63a85721831 Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Fri, 24 Sep 2021 10:09:18 -0700 Subject: [PATCH 1/4] Enable logging for responses with only alloc. --- src/protocol/response_writer.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/protocol/response_writer.rs b/src/protocol/response_writer.rs index 13c1a97d..33a1ad19 100644 --- a/src/protocol/response_writer.rs +++ b/src/protocol/response_writer.rs @@ -1,3 +1,6 @@ +use alloc::vec::Vec; +use alloc::string::String; + use num_traits::PrimInt; use crate::internal::BeBytes; @@ -24,7 +27,7 @@ pub struct ResponseWriter<'a, C: Connection + 'a> { rle_char: u8, rle_repeat: u8, // buffer to log outgoing packets. only allocates if logging is enabled. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] msg: Vec, } @@ -37,7 +40,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { checksum: 0, rle_char: 0, rle_repeat: 0, - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] msg: Vec::new(), } } @@ -51,7 +54,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { // added to the checksum, and is just sitting in the RLE buffer) let checksum = self.checksum; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] trace!( "--> ${}#{:02x?}", String::from_utf8_lossy(&self.msg), @@ -73,7 +76,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { } fn inner_write(&mut self, byte: u8) -> Result<(), Error> { - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] if log_enabled!(log::Level::Trace) { match self.msg.as_slice() { [.., c, b'*'] => { From 71e7458b079e1eead5840c2a0465900a70d5fdbc Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Fri, 24 Sep 2021 11:36:35 -0700 Subject: [PATCH 2/4] Introduce trace-pkt feature. Also feature gate imports. --- Cargo.toml | 1 + src/protocol/response_writer.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4960a0be..5bb3be79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ goblin = "0.2" default = ["std"] alloc = ["managed/alloc"] std = ["alloc"] +trace-pkt = ["alloc"] paranoid_unsafe = [] # INTERNAL: enables the `__dead_code_marker!` macro. diff --git a/src/protocol/response_writer.rs b/src/protocol/response_writer.rs index 33a1ad19..0eb274fa 100644 --- a/src/protocol/response_writer.rs +++ b/src/protocol/response_writer.rs @@ -1,5 +1,7 @@ -use alloc::vec::Vec; +#[cfg(feature = "trace-pkt")] use alloc::string::String; +#[cfg(feature = "trace-pkt")] +use alloc::vec::Vec; use num_traits::PrimInt; @@ -27,7 +29,7 @@ pub struct ResponseWriter<'a, C: Connection + 'a> { rle_char: u8, rle_repeat: u8, // buffer to log outgoing packets. only allocates if logging is enabled. - #[cfg(feature = "alloc")] + #[cfg(feature = "trace-pkt")] msg: Vec, } @@ -40,7 +42,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { checksum: 0, rle_char: 0, rle_repeat: 0, - #[cfg(feature = "alloc")] + #[cfg(feature = "trace-pkt")] msg: Vec::new(), } } @@ -54,7 +56,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { // added to the checksum, and is just sitting in the RLE buffer) let checksum = self.checksum; - #[cfg(feature = "alloc")] + #[cfg(feature = "trace-pkt")] trace!( "--> ${}#{:02x?}", String::from_utf8_lossy(&self.msg), @@ -76,7 +78,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> { } fn inner_write(&mut self, byte: u8) -> Result<(), Error> { - #[cfg(feature = "alloc")] + #[cfg(feature = "trace-pkt")] if log_enabled!(log::Level::Trace) { match self.msg.as_slice() { [.., c, b'*'] => { From 5e21643e5de01a5b88f60d23cefa67da02c4fe79 Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Fri, 24 Sep 2021 11:37:50 -0700 Subject: [PATCH 3/4] Make trace-pkt a default feature. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5bb3be79..304464fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ pretty_env_logger = "0.4" goblin = "0.2" [features] -default = ["std"] +default = ["std", "trace-pkt"] alloc = ["managed/alloc"] std = ["alloc"] trace-pkt = ["alloc"] From 9e060d26869ccb2f470c83f7df0bdcbf9022421c Mon Sep 17 00:00:00 2001 From: Gerd Zellweger Date: Fri, 24 Sep 2021 11:39:51 -0700 Subject: [PATCH 4/4] Feature gate trace! for received packets. --- src/protocol/recv_packet.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/protocol/recv_packet.rs b/src/protocol/recv_packet.rs index 8a9adf80..251e7981 100644 --- a/src/protocol/recv_packet.rs +++ b/src/protocol/recv_packet.rs @@ -53,6 +53,7 @@ impl RecvPacketStateMachine { } if matches!(self.state, State::Ready) { + #[cfg(feature = "trace-pkt")] trace!( "<-- {}", core::str::from_utf8(buf.as_slice()).unwrap_or("") @@ -111,6 +112,7 @@ impl RecvPacketBlocking { buf.push(get_byte().map_err(RecvPacketError::Connection)?)?; } + #[cfg(feature = "trace-pkt")] trace!( "<-- {}", core::str::from_utf8(buf.as_slice()).unwrap_or("")