Macro to print variable(s) with values nicely (stripped from release builds)
cargo add log_macro
Add this to top of file:
use log_macro::log;
Possible uses and outputs:
// print string only
log!("hello"); // -> hello
// print variable
let animals = vec!["cat", "dog"];
log!(animals); // -> animals: ["cat", "dog"]
// print multiple variables
let animals = vec!["cat", "dog"];
let fish = vec!["salmon", "tuna"];
log!(animals, fish);
// each variable logged on new line
// -> animals: ["cat", "dog"]
// -> fish: ["salmon", "tuna"]
Variables will be in green color to stand out.
Exported macro code is in src/lib.rs:
#[macro_export]
macro_rules! log {
// Single literal string case
( $val:expr $(,)? ) => {{
#[cfg(debug_assertions)]
{
if ::std::stringify!($val).starts_with("\"") {
// Remove quotes for string literals
::std::eprintln!("{}", ::std::stringify!($val).trim_matches('\"'));
} else {
// Print using a reference to avoid moving the value
::std::eprintln!("\x1B[32m{}\x1B[0m: {:?}", ::std::stringify!($val), &$val);
}
}
}};
// Multiple variables case
( $($val:expr),+ $(,)? ) => {{
#[cfg(debug_assertions)]
{
$(
$crate::log!($val);
)+
}
}};
}
Will run tests in src/lib.rs.
cargo watch -q -- sh -c "tput reset && cargo test -q --lib"
Always open to useful ideas or fixes in form of issues or PRs.
Can open new issue (search existing ones for duplicates first) or start discussion on GitHub or Discord.
Can submit draft PRs with good ideas/fixes. You will get help along the way to make it merge ready.
Ask for help if you're stuck. You will be unblocked fast.