Macro that evaluates numeric variable for printing

I want to print messages in a utility based on specified verbosity.
The specified verbosity will be set as a variable defined as an integer value, such as 0: only necessary, 1: verbose, 2: more verbose.

I looked at the log create and all the crates that are based on it, but it all seems to take it too far for a simple single page utility.

In my mind, this should be very simple to realise by a macro that essentially takes the same arguments as println!, only with the verbosity number as the first argument.

I cannot get my head around creating a macro that does that. Anyone willing to help me?

I also don't think I am the first one to come up with this, but have not been able to find anything that does this. I do find it hard to perform my own things based on the macro documentation.

I found the solution myself:

    let verbosity = 1;

    macro_rules! eval_println {
        ($level:expr, $($arguments:expr),+) => {
            if $level >= verbosity {
                println!($($arguments),+);
            }
        }
    }

Ahh, it only took you an hour. Great. Thanks for sharing.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.