Giter Site home page Giter Site logo

nikitavoloboev / log_macro Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 36 KB

Macro to print variable(s) with values nicely (stripped from release builds)

Home Page: https://docs.rs/log_macro/latest/log_macro/

License: MIT License

Rust 100.00%
rust rust-crate

log_macro's Introduction

log_macro crates.io docs.rs

Macro to print variable(s) with values nicely (stripped from release builds)

Install

cargo add log_macro

Use

Add this to top of file:

use log_macro::log;

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.

Implementation

Exported macro code is in src/lib.rs:

#[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.

cargo watch -q -- sh -c "tput reset && cargo test -q --lib"

Contribute

Always open to useful ideas or fixes in form of issues or PRs.

Can open new issue (search existing issues first) or start discussion.

It's okay to submit draft PR as you can get help along the way to make it merge ready.

Join Discord for more indepth discussions on this repo and others.

๐Ÿ–ค

Support on GitHub or look into other projects.

Discord X nikiv.dev

log_macro's People

Contributors

nikitavoloboev avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

log_macro's Issues

dbg!

You link to dbg! in your README so I guess you know about it. For me, it basically does all what this macro does and even plans to do. Am I missing something?

Plus, commit messages are there for a reason. It is not helpful when all messages are .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.