Skip to content

Commit

Permalink
Add logging feature
Browse files Browse the repository at this point in the history
* Improve ease of debugging an incorrect parser implementation by adding
  context to the Err values from deku parsers by adding logging from the
  log crate.
* This is feature-gated under the new "logging" feature.

From the following sample program, you can easily see the improved error
message and debugging since we known the field that was incorrect
parsed. Much easier than gdb.
```
[dependencies]
deku = { path = "../deku", features = ["logging"]}
log = "0.4.17"
env_logger = "0.9.0"
```
```
use deku::prelude::*;

pub struct TestStruct {
    pub a: u16,
    pub b: u16,
}

fn main() {
    env_logger::init();
    TestStruct::from_bytes((&[0x01, 0x02, 0x03], 0)).unwrap();
    println!("Hello, world!");
}
```
```
> RUST_LOG=trace cargo r
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/deku-testing`
[2022-07-04T16:11:54Z TRACE deku_testing] Reading: TestStruct::a from [00000001, 00000010, 00000011]
[2022-07-04T16:11:54Z TRACE deku_testing] Reading: TestStruct::b from [00000011]
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Incomplete(NeedSize { bits: 16 })', src/main.rs:11:54
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

This could be expanded in the future to enums and/or other internal deku
structs.

See sharksforarms#168
  • Loading branch information
wcampbell0x2a authored and sharksforarms committed Jul 4, 2022
1 parent 7defa4d commit d4f7343
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
default = ["std", "const_generics"]
std = ["deku_derive/std", "bitvec/std", "alloc"]
alloc = ["bitvec/alloc"]
logging = ["deku_derive/log"]
const_generics = []

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions deku-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ proc-macro = true

[features]
std = ["proc-macro-crate"]
logging = ["log"]

[dependencies]
quote = "1.0"
Expand All @@ -22,6 +23,7 @@ syn = "1.0"
proc-macro2 = "1.0"
darling = "0.14"
proc-macro-crate = { version = "1.0.0", optional = true }
log = { version = "0.4.17", optional = true }

[dev-dependencies]
rstest = "0.13"
9 changes: 9 additions & 0 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ fn emit_field_read(
}
});

let trace_field_log = if cfg!(feature = "log") {
quote! {
log::trace!("Reading: {}::{} from {}", #ident, #field_ident_str, __deku_rest);
}
} else {
quote! {}
};

let field_read_func = if field_reader.is_some() {
quote! { #field_reader }
} else {
Expand Down Expand Up @@ -668,6 +676,7 @@ fn emit_field_read(
#bit_offset
#byte_offset

#trace_field_log
let #internal_field_ident = {
#field_read_tokens
};
Expand Down

0 comments on commit d4f7343

Please sign in to comment.