sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.14k stars 55 forks source link

Add logging feature #271

Closed wcampbell0x2a closed 2 years ago

wcampbell0x2a commented 2 years ago

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.

Even cooler would be something like this:

                                                                      |-----------------|
[2022-07-04T16:08:16Z TRACE deku_testing] Reading: TestStruct::a from [00000001, 00000010, 00000011]

See #168

sharksforarms commented 2 years ago

LGTM thanks!

I think this format from @hawkw could be really cool for debugging: https://twitter.com/mycoliza/status/1529987746269233152/photo/2

https://github.com/hawkw/mycelium/blob/4d86039bf166bfddd92864b1c35003ef0226ba96/util/src/bits.rs#L224-L323

sharksforarms commented 2 years ago

@wcampbell0x2a https://crates.io/crates/mycelium-bitfield just dropped đź‘€

https://docs.rs/mycelium-bitfield/0.1.1/mycelium_bitfield/macro.bitfield.html#example-display-output