testlog
Yes, One macro
A micro-crate that provides test_log! – a macro that prints to stderr only when tests are running, and only for the crate where it's used.
Why?
You need to debug test failures. println! gets captured by the test harness.
eprintln! clutters production logs. Adding print statements of any kind
pollute the logs of the consumers of your library. What you want is something
in your library that only prints for your tests, and ideally is only shown
during testing on failure.
This does exactly what you want: debug output that only appears during testing of your crate, and since output from a test is captured on success, you only see the log when the test fails. Perfect!.
Usage
Add to Cargo.toml:
[]
= "0.1"
Use in your code:
use test_log;
How it works
The test_log! macro checks cfg!(test) at compile time:
- In test builds: Expands to
eprintln!(...) - In production builds: Expands to nothing (zero runtime cost)
- Crate-local: Only activates when the current crate is in test mode
That's it