I've found several useful macros in Rust, namely: file!(), line!(), stringify!()
I've also found that Rust allows macros with variable arguments, as stated here:
macro_rules! print_all {
($($args:expr),*) => {{
$(
println!("{}", $args);
)*
}}
}
My goal is to somehow combine all those macros inside one which I will use during troubleshooting/debugging. So calling trace!
macro on the following example:
let a: i32 = 1;
let b: i32 = 2;
trace!(a,b)
should expand to something like this:
println!("TRACE: file: {}, line: {}, a: {}, b: {}", file!(), line!(), a, b);
Is it possible? If yes, how would such a macro work?
You could do something like this:
There is likely a small overhead to calling
print!
multiple times, since each call will result in a system call and will also check for IO errors. However, constructing a single formatting string for arbitrary arguments would need a procedural macro, which I think is beyond the scope of the question.You could also use a
BufWriter
to limit it to a single system call, but it might not be worth the effort.