Crate verbose_macros

Source
Expand description

A dependency-free macro library for verbose and debug printing in Rust.

§Introduction

I usually use log with all log levels for printing, but some people prefer to use just the --verbose and --debug flags.

For this reason, I created this library to provide a simple way to print verbose and debug messages without the need for any dependencies or setup to the log crate.

The library just provides the verbose! and debug! macros, which have the same syntax as println! and prints only if the flags for them are set.

§Getting Started

Add the following to your Cargo.toml:

[dependencies]
verbose-macros = "0.1"

Then, you can use the macros in your code:

use verbose_macros::{debug, verbose};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let verbose = std::env::args().any(|arg| arg == "--verbose" || arg == "-v");
    let debug = std::env::args().any(|arg| arg == "--debug" || arg == "-d");

    // Set the debug and verbose flags
    verbose_macros::set_debug(verbose);
    verbose_macros::set_verbose(debug);

    // Use the debug macro
    debug!("This is a debug message.");
    debug!("This is a debug message with a value: {}", 42);

    // Use the verbose macro
    verbose!("This is a verbose message.");
    verbose!("This is a verbose message with a value: {}", 42);

    Ok(())
}

Macros§

debug
A macro for debug printing.
verbose
A macro for verbose printing.

Functions§

is_debug
Check if the debug flag is set.
is_verbose
Check if the verbose flag is set.
set_debug
Set the debug flag.
set_verbose
Set the verbose flag.
try_set_debug
Safe version of set_debug that returns an error if the debug flag is already set.
try_set_verbose
Safe version of set_verbose that returns an error if the verbose flag is already set.