Crates.io | log_macro |
lib.rs | log_macro |
version | 0.1.6 |
source | src |
created_at | 2023-08-20 02:40:36.181779 |
updated_at | 2023-08-21 12:06:25.904994 |
description | Macro to print variable name and value only (stripped from release builds) |
homepage | |
repository | https://github.com/nikitavoloboev/log_macro |
max_upload_size | |
id | 949085 |
size | 12,048 |
Macro to print variable(s) with values nicely (stripped from release builds)
cargo add log_macro
Add this to top of file:
#[macro_use]
extern crate log_macro;
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 this:
#[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 the tests in src/lib.rs.
cargo watch -q -- sh -c "tput reset && cargo test -q --lib"
The tasks to do are outlined in existing issues and in tasks below (sorted by priority).
If issue/idea you have is not there, open new issue or start discussion.
Any PR with code/doc improvements is welcome. ✨
Join Discord for more indepth discussions on this repo and others.
cargo test --doc --package log_macro -- log --nocapture
. fix them> print(f”operation: {2+1=}”)
-> operation: 2+1=3
dbg!
Support on GitHub or look into other projects.