I refactoring my app to make it also available as a crate. So I want to disable println! by default and allow it only for debugging. For this case I did next steps:
The problem is that this works only for the main.rs and if I want to use my modified println! in another files, I should use crate::println! instead. So, for main.rs I still can use:
println!("TEST");
but this macro will be my modified macro. For other files I should add crate:: prefix, otherwise other files will use std::println!.
The question: is there a way to avoid adding extra code and just use println! as usual ? I want to modify println! without a lot of edits in every file.
and instead of #[macro_export] I use pub(crate) export. For println name this not works, because it is a default macro name. So I am forced to use custom name.
Second, in my main.rs I removed #[macro_use] attr and instead I use pub(crate) import:
// for mod primary I removed #[macro_use]
mod primary;
pub(crate) use primary::debug;
And finally for each place where I need to call debug! I use it with crate:: prefix.
If I need to use colored messages, but all of them should be hidden if debug is disabled, will this work ? From what I got the conditional messaging only works for log::debug!, but not for log::info! etc
In my case, I use colored messages and I want to disable all of them, if debug flag is disabled.
Sure, it all depends on how you configure your logger.[1] I usually use tracing-subscriber with the RUST_LOG environment variable. The equivalent setup can be achieved with log and env_logger. Then I can run RUST_LOG=debug and all logs higher and equal to the debug log level will get printed. If you don't need to configure the log level during run time with an environment variable, but instead based on the features your crate is compiled with, you can specify different loggers based on the used feature set. Or no logger at all if your debug feature isn't set, for example.
And whether it has ANSI support for coloured terminal output. âŠī¸