#macro-derive #logging #proc-macro #function #log #derive #macro

macro log-derive

Procedural Macros for logging the result and inputs of a function

11 releases

0.4.1 Jul 15, 2020
0.4.0 Dec 20, 2019
0.3.2 Nov 27, 2019
0.3.1 Sep 15, 2019
0.1.0 Jan 31, 2019

#417 in Debugging

Download history 18575/week @ 2024-07-21 18937/week @ 2024-07-28 15751/week @ 2024-08-04 11670/week @ 2024-08-11 6303/week @ 2024-08-18 6154/week @ 2024-08-25 12981/week @ 2024-09-01 13394/week @ 2024-09-08 15730/week @ 2024-09-15 11466/week @ 2024-09-22 8659/week @ 2024-09-29 6826/week @ 2024-10-06 18416/week @ 2024-10-13 4622/week @ 2024-10-20 29042/week @ 2024-10-27 4110/week @ 2024-11-03

57,246 downloads per month
Used in 19 crates (9 directly)

MIT/Apache

19KB
324 lines

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.3"
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 according macro above a function that,
either returns an output or receive an input 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)]
#[logfn_inputs(Info)]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}

#[logfn(Trace, fmt = "testing the num: {:?}")]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}

Output

The output of the fibonacci example:

17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 5)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 4)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 3)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [ INFO] fibonacci() -> 2
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [ INFO] fibonacci() -> 3
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [ INFO] fibonacci() -> 2
17:15:24 [ INFO] fibonacci() -> 5
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 3)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [ INFO] fibonacci() -> 2
17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)
17:15:24 [ INFO] fibonacci() -> 1
17:15:24 [ INFO] fibonacci() -> 3
17:15:24 [ INFO] fibonacci() -> 8

If you expand the output of the #[logfn] 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
}

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

The expansion of the #[logfn_inputs] macro will look something like this:

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

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

Note

The log_ts feature will fail your compilation in a no-std enviroment. it can only be used where std is available. (as it uses std::time::Instant)

Dependencies

~2MB
~46K SLoC