From: Ian Jackson Date: Thu, 4 Mar 2021 21:02:05 +0000 (+0000) Subject: provide dbgc!, version of dbg! which prints compactly X-Git-Tag: otter-0.4.0~246 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e2f7cf3e649b94eb39e1ee0ad54e0317040d12b9;p=otter.git provide dbgc!, version of dbg! which prints compactly Signed-off-by: Ian Jackson --- diff --git a/src/prelude.rs b/src/prelude.rs index 17d4d232..7baa8932 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -95,6 +95,7 @@ pub use otter_base::zcoord::{self, ZCoord}; pub use otter_base::misc as base_misc; pub use base_misc::default; +pub use crate::dbgc; pub use crate::{deref_to_field, deref_to_field_mut}; pub use crate::ensure_eq; pub use crate::from_instance_lock_error; diff --git a/src/utils.rs b/src/utils.rs index a062eb97..f08b4425 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -352,3 +352,28 @@ impl IteratorExt for T where }) } } + +#[macro_export] +macro_rules! dbgc { + // NOTE: We cannot use `concat!` to make a static string as a format argument + // of `eprintln!` because `file!` could contain a `{` or + // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!` + // will be malformed. + () => { + std::eprintln!("[{}:{}]", std::file!(), std::line!()); + }; + ($val:expr $(,)?) => { + // Use of `match` here is intentional because it affects the lifetimes + // of temporaries - https://stackoverflow.com/a/48732525/1063961 + match $val { + tmp => { + std::eprintln!("[{}:{}] {} = {:?}", + std::file!(), std::line!(), std::stringify!($val), &tmp); + tmp + } + } + }; + ($($val:expr),+ $(,)?) => { + ($(std::dbg!($val)),+,) + }; +}