Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add logging feature#271

Merged
sharksforarms merged 1 commit into
sharksforarms:masterfrom
wcampbell0x2a:add-logging
Jul 4, 2022
Merged

Add logging feature#271
sharksforarms merged 1 commit into
sharksforarms:masterfrom
wcampbell0x2a:add-logging

Conversation

@wcampbell0x2a
Copy link
Copy Markdown
Collaborator

  • 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.

Even cooler would be something like this:

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

See #168

* 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
@sharksforarms
Copy link
Copy Markdown
Owner

sharksforarms commented Jul 4, 2022

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 sharksforarms merged commit d4f7343 into sharksforarms:master Jul 4, 2022
@wcampbell0x2a wcampbell0x2a deleted the add-logging branch July 4, 2022 17:26
@sharksforarms
Copy link
Copy Markdown
Owner

sharksforarms commented Jul 16, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants