https://github.com/nikitavoloboev/log_macro
Macro to print variable(s) with values nicely (stripped from release builds)
https://github.com/nikitavoloboev/log_macro
rust rust-crate
Last synced: 3 months ago
JSON representation
Macro to print variable(s) with values nicely (stripped from release builds)
- Host: GitHub
- URL: https://github.com/nikitavoloboev/log_macro
- Owner: nikitavoloboev
- License: mit
- Created: 2023-08-20T02:20:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-22T16:41:30.000Z (about 1 year ago)
- Last Synced: 2025-10-11T11:07:49.743Z (3 months ago)
- Topics: rust, rust-crate
- Language: Rust
- Homepage: https://docs.rs/log_macro/latest/log_macro/
- Size: 38.1 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# log_macro [
](https://crates.io/crates/log_macro) [
](https://docs.rs/log_macro)
> Macro to print variable(s) with values nicely (stripped from release builds)
## Install
```
cargo add log_macro
```
## Use
Add this to top of file:
```rust
use log_macro::log;
```
Possible uses and outputs:
```rust
// 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.
## Implementation
Exported macro code is in [src/lib.rs](src/lib.rs):
```rust
#[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);
)+
}
}};
}
```
## Run
Will run tests in [src/lib.rs](src/lib.rs).
```
cargo watch -q -- sh -c "tput reset && cargo test -q --lib"
```
## Contributing
Always open to useful ideas or fixes in form of issues or PRs.
Can [open new issue](../../issues/new/choose) (search [existing ones](../../issues) for duplicates first) or start discussion on [GitHub](../../discussions) or [Discord](https://discord.com/invite/TVafwaD23d).
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.
[](https://go.nikiv.dev/discord) [](https://x.com/nikitavoloboev) [](https://nikiv.dev)