Skip to content

Commit

Permalink
Enable logging for responses with only alloc. (#78)
Browse files Browse the repository at this point in the history
* Enable logging for responses with only alloc.

* Introduce trace-pkt feature.

Also feature gate imports.

* Make trace-pkt a default feature.

* Feature gate trace! for received packets.
  • Loading branch information
gz committed Sep 24, 2021
1 parent 4344c85 commit 52ae3c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ pretty_env_logger = "0.4"
goblin = "0.2"

[features]
default = ["std"]
default = ["std", "trace-pkt"]
alloc = ["managed/alloc"]
std = ["alloc"]
trace-pkt = ["alloc"]
paranoid_unsafe = []

# INTERNAL: enables the `__dead_code_marker!` macro.
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<invalid packet>")
Expand Down Expand Up @@ -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("<invalid packet>")
Expand Down
13 changes: 9 additions & 4 deletions src/protocol/response_writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#[cfg(feature = "trace-pkt")]
use alloc::string::String;
#[cfg(feature = "trace-pkt")]
use alloc::vec::Vec;

use num_traits::PrimInt;

use crate::internal::BeBytes;
Expand All @@ -24,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 = "std")]
#[cfg(feature = "trace-pkt")]
msg: Vec<u8>,
}

Expand All @@ -37,7 +42,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> {
checksum: 0,
rle_char: 0,
rle_repeat: 0,
#[cfg(feature = "std")]
#[cfg(feature = "trace-pkt")]
msg: Vec::new(),
}
}
Expand All @@ -51,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 = "std")]
#[cfg(feature = "trace-pkt")]
trace!(
"--> ${}#{:02x?}",
String::from_utf8_lossy(&self.msg),
Expand All @@ -73,7 +78,7 @@ impl<'a, C: Connection + 'a> ResponseWriter<'a, C> {
}

fn inner_write(&mut self, byte: u8) -> Result<(), Error<C::Error>> {
#[cfg(feature = "std")]
#[cfg(feature = "trace-pkt")]
if log_enabled!(log::Level::Trace) {
match self.msg.as_slice() {
[.., c, b'*'] => {
Expand Down

0 comments on commit 52ae3c7

Please sign in to comment.