rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.16k stars 250 forks source link

Add a `ldbg!` macro that mirrors `std::dbg` #546

Open tgross35 opened 1 year ago

tgross35 commented 1 year ago

The std::dbg!() macro is quite useful for quick debugging. I have stumbled across at least a handful of cases where whatever the logger is is more useful than stderr output - for example, when the logger is mapped to RTT or UART on embedded and there is no stderr available.

So, my suggestion is to add ldbg that acts similarly to the debug macro, but uses the default logger.

use log::{ldbg, Level};

let v = vec![0usize, 1, 2, 3, 4];

// default to debug level (possibly warn or info so it's more likely to show up)
let m = ldbg!(&v[2..]);

// allow for optional level override
let n = ldbg!(level: Level::Warn, &v[..3]);
Thomasdezeeuw commented 5 months ago

I'm on the fence on this.

On the one hand I see the usefulness of the dbg! macro for quick debugging. However I think these statement shouldn't be committed (i.e. they should be removed before sending the code upstream).

If you need more information in a way that the log crate can provide I think it would be more beneficial to add proper debug! message and use the release_max_level_info (or similar) feature to remove them from release binary. (this will also safe you time the next time you're debugging a similar issue, speaking from experience)