#trace #log #events #proc-macro #attributes #debugging #instrument

log-instrument

Offers an attribute procedural macro that adds log::trace! events at the start and end of attributed functions

7 releases

0.3.0 Oct 12, 2023
0.2.0 Sep 1, 2023
0.1.4 Aug 20, 2023

#2249 in Rust patterns

Download history 13/week @ 2024-07-27 10/week @ 2024-09-21

1,722 downloads per month

Apache-2.0

6KB

log-instrument

Crates.io docs codecov

Offers an attribute procedural macro that adds log::trace! events at the start and end of attributed functions.

Example

use log::*;

fn main() {
    env_logger::builder()
        .filter_level(LevelFilter::Trace)
        .init();
    info!("{}", one(2));
    info!("{}", one(3));
    info!("{}", one(4));
}
#[log_instrument::instrument]
fn one(x: u32) -> u32 {
    let cmp = x == 2;
    debug!("cmp: {cmp}");
    if cmp {
        return 4;
    }
    two(x + 3)
}
#[log_instrument::instrument]
fn two(x: u32) -> u32 {
    let res = x % 2;
    debug!("res: {res}");
    res
}

Outputs:

[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: true
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO  six] 4
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: false
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one>>two
[2023-10-12T16:38:00Z DEBUG six] res: 0
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one<<two
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO  six] 0
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: false
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one>>two
[2023-10-12T16:38:00Z DEBUG six] res: 1
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one<<two
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO  six] 1

Dependencies

~300–800KB
~19K SLoC