Skip to content

Commit

Permalink
Improve logging macros to work with IDEs better
Browse files Browse the repository at this point in the history
  • Loading branch information
lrubasze committed Dec 19, 2024
1 parent 07f86bb commit ee40f4b
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions scrypto/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ macro_rules! error {
#[cfg(not(feature = "log-error"))]
#[macro_export]
macro_rules! error {
($($args: expr),+) => {{}};
($($args: expr),+) => {{
// The operation below does nothing at runtime but consumes the expressions,
// allowing tools (e.g., rust-analyzer) to parse them correctly to identify
// syntactical errors or provide hints and completions.
// Using `false` ensures the expression is optimized out in case of expressions
// that mutate data or have side effects.
if false {
let _ = ($($args,)+);
}
}};
}

/// Logs a `WARN` message.
Expand All @@ -39,7 +48,12 @@ macro_rules! warn {
#[cfg(not(feature = "log-warn"))]
#[macro_export]
macro_rules! warn {
($($args: expr),+) => {{}};
($($args: expr),+) => {{
// See `error` macro comment
if false {
let _ = ($($args,)+);
}
}};
}

/// Logs an `INFO` message.
Expand All @@ -61,7 +75,12 @@ macro_rules! info {
#[cfg(not(feature = "log-info"))]
#[macro_export]
macro_rules! info {
($($args: expr),+) => {{}};
($($args: expr),+) => {{
// See `error` macro comment
if false {
let _ = ($($args,)+);
}
}};
}

/// Logs a `DEBUG` message.
Expand All @@ -83,7 +102,12 @@ macro_rules! debug {
#[cfg(not(feature = "log-debug"))]
#[macro_export]
macro_rules! debug {
($($args: expr),+) => {{}};
($($args: expr),+) => {{
// See `error` macro comment
if false {
let _ = ($($args,)+);
}
}};
}

/// Logs a `TRACE` message.
Expand All @@ -105,7 +129,12 @@ macro_rules! trace {
#[cfg(not(feature = "log-trace"))]
#[macro_export]
macro_rules! trace {
($($args: expr),+) => {{}};
($($args: expr),+) => {{
// See `error` macro comment
if false {
let _ = ($($args,)+);
}
}};
}

// This is a TT-Muncher, a useful guide for this type of use case is here: https://adventures.michaelfbryan.com/posts/non-trivial-macros/
Expand Down

0 comments on commit ee40f4b

Please sign in to comment.