Skip to content

Commit

Permalink
Enhance/Fix logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Aug 9, 2023
1 parent 939975d commit 218bdca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
3 changes: 1 addition & 2 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,7 @@ fn emit_field_read(

let trace_field_log = if cfg!(feature = "logging") {
quote! {
// TODO: fix logging feature
//log::trace!("Reading: {}::{} from {}", #ident, #field_ident_str, __deku_rest);
log::trace!("Reading: {}::{}", #ident, #field_ident_str);
}
} else {
quote! {}
Expand Down
15 changes: 8 additions & 7 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! To test out the "logging" feature:
//! ```
//! $ RUST_LOG=trace cargo run --example example --features logging
//! ```
#![allow(clippy::unusual_byte_groupings)]

use std::convert::{TryFrom, TryInto};
use std::convert::TryInto;

use deku::{
container::Container,
ctx::{BitSize, ByteSize, Endian},
prelude::*,
};
use deku::{container::Container, prelude::*};

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct FieldF {
Expand Down Expand Up @@ -40,6 +41,7 @@ struct DekuTest {
}

fn main() {
env_logger::init();
let test_data: &[u8] = [
0xab,
0b1010010_1,
Expand All @@ -57,7 +59,6 @@ fn main() {
let mut container = Container::new(std::io::Cursor::new(test_data));
let test_deku = DekuTest::from_reader(&mut container, ()).unwrap();

let mut container = Container::new(std::io::Cursor::new(test_data));
let test_deku_from_bytes = DekuTest::from_bytes((test_data, 0)).unwrap();
assert_eq!(test_deku, test_deku_from_bytes.1);

Expand Down
17 changes: 17 additions & 0 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use bitvec::prelude::*;

use crate::{prelude::NeedSize, DekuError};

#[cfg(feature = "logging")]
use log;

/// Return from `read_bytes`
pub enum ContainerRet {
/// Successfully read bytes
Expand Down Expand Up @@ -35,6 +38,8 @@ impl<R: Read> Container<R> {
/// not increase bits_read.
#[inline]
pub fn skip_bits(&mut self, amt: usize) -> Result<(), DekuError> {
#[cfg(feature = "logging")]
log::trace!("skip_bits: {amt}");
// Save, and keep the leftover bits since the read will most likely be less than a byte
self.read_bits(amt)?;
self.bits_read = 0;
Expand All @@ -53,7 +58,11 @@ impl<R: Read> Container<R> {
/// `amt` - Amount of bits that will be read
#[inline]
pub fn read_bits(&mut self, amt: usize) -> Result<Option<BitVec<u8, Msb0>>, DekuError> {
#[cfg(feature = "logging")]
log::trace!("read_bits: requesting {amt} bits");
if amt == 0 {
#[cfg(feature = "logging")]
log::trace!("read_bits: returned None");
return Ok(None);
}
let mut ret = BitVec::with_capacity(amt);
Expand Down Expand Up @@ -89,6 +98,8 @@ impl<R: Read> Container<R> {

// TODO: other errors?
}
#[cfg(feature = "logging")]
log::trace!("read_bits: read() {buf:02x?}");

// create bitslice and remove unused bits
let mut rest: BitVec<u8, Msb0> = BitVec::try_from_slice(&buf).unwrap();
Expand All @@ -100,6 +111,8 @@ impl<R: Read> Container<R> {
}

self.bits_read += ret.len();
#[cfg(feature = "logging")]
log::trace!("read_bits: returning {ret}");
Ok(Some(ret))
}

Expand All @@ -111,6 +124,8 @@ impl<R: Read> Container<R> {
/// `amt` - Amount of bytes that will be read
#[inline]
pub fn read_bytes(&mut self, amt: usize, buf: &mut [u8]) -> Result<ContainerRet, DekuError> {
#[cfg(feature = "logging")]
log::trace!("read_bytes: requesting {amt} bytes");
if self.leftover.is_empty() {
if buf.len() < amt {
return Err(DekuError::Incomplete(NeedSize::new(amt * 8)));
Expand All @@ -123,6 +138,8 @@ impl<R: Read> Container<R> {
// TODO: other errors?
}
self.bits_read += amt * 8;
#[cfg(feature = "logging")]
log::trace!("read_bytes: returning {buf:02x?}");
Ok(ContainerRet::Bytes)
} else {
Ok(ContainerRet::Bits(self.read_bits(amt * 8)?))
Expand Down

0 comments on commit 218bdca

Please sign in to comment.