chiark / gitweb /
provide dbgc!, version of dbg! which prints compactly
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 4 Mar 2021 21:02:05 +0000 (21:02 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 4 Mar 2021 21:02:24 +0000 (21:02 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/prelude.rs
src/utils.rs

index 17d4d232ad9c187a77298edec5bb80d657328b20..7baa8932fcba31d1b0db3380a034531592844172 100644 (file)
@@ -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;
index a062eb97cf6bc52ac1f5ac3c56f24df6d93b9e8f..f08b4425e45494de8cf0f4cb71be0284c766893f 100644 (file)
@@ -352,3 +352,28 @@ impl<T,U,E,F> IteratorExt<U,E,F> 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)),+,)
+    };
+}