GithubHelp home page GithubHelp logo

isgasho / log-derive Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elichai/log-derive

0.0 1.0 0.0 51 KB

A procedural macro for auto logging output of functions

Home Page: https://docs.rs/log-derive

License: Apache License 2.0

Rust 100.00%

log-derive's Introduction

log-derive

Build Status Latest version Documentation License dependency status

A Rust macro to part of the log facade that auto generates loggings for functions output.

Usage

Add this to your Cargo.toml:

[dependencies]
log-derive = "0.2"
log = "0.4"

and for Rust Edition 2015 add this to your crate root:

#[macro_use]
extern crate log_derive;
extern crate log;

In Rust Edition 2018 you can simply do:

use log_derive::logfn;

After that all you need is to add the macro above a function that returns an output that implements the Debug trait.

Examples

 #[logfn(Err = "Error", fmt = "Failed Sending Packet: {:?}")]
 fn send_hi(addr: SocketAddr) -> Result<(), io::Error> {
     let mut stream = TcpStream::connect(addr)?;
     stream.write(b"Hi!")?;
     Ok( () )
 }
#[logfn(Trace)]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}

Output

The output of the fibonacci example:

14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 2
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 3
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 2
14:55:41 [INFO] fibonacci() -> 5
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 2
14:55:41 [INFO] fibonacci() -> 1
14:55:41 [INFO] fibonacci() -> 3
14:55:41 [INFO] fibonacci() -> 8

If you expand the output of the macro the resulting code will look something like this:

fn fibonacci(n: u32) -> u32 {
    let result = (move || match n {
        0 => 1,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    })();
    log::log!(log::Level::Info, "fibonacci() -> {}", result);
    result
}

Of course the log! macro will be expanded too and it will be a bit more messy.

If the function returns a Result it will match through it to split between the Ok LogLevel and the Err LogLevel

log-derive's People

Contributors

elichai avatar teddriggs avatar

Watchers

 avatar

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.