log-execution-time 0.1.0

A Rust procedural macro to log the execution time of functions.
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 6.71 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 274.74 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • satishbabariya/log-execution-time-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • satishbabariya

log_execution_time

Overview

log_execution_time is a Rust procedural macro that logs the execution time of functions. It leverages the log crate to provide detailed timing information, making it a useful tool for performance monitoring and debugging.

Features

  • Measures and logs the execution time of functions.
  • Compatible with synchronous and asynchronous functions.
  • Lightweight and easy to integrate into existing projects.

Installation

Add the following to your Cargo.toml file:

[dependencies]
log_execution_time = "0.1.0"
log = "0.4" # Required for logging

Ensure you have a logger initialized in your project, such as env_logger or any other compatible logger.

Usage

Annotate your functions with #[log_execution_time]:

use log_execution_time::log_execution_time;

#[log_execution_time]
fn compute() {
    // Perform some computation
    let sum: u32 = (1..=1000).sum();
    println!("Sum is: {}", sum);
}

fn main() {
    env_logger::init();
    compute();
}

Async Functions

The macro also works with asynchronous functions:

use log_execution_time::log_execution_time;

#[log_execution_time]
async fn fetch_data() {
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    println!("Data fetched!");
}

#[tokio::main]
async fn main() {
    env_logger::init();
    fetch_data().await;
}

Example

Create an example file in your project to test the macro:

mkdir examples

Add the following to examples/main.rs:

use log_execution_time::log_execution_time;

#[log_execution_time]
fn example_function() {
    let product: u32 = (1..=10).product();
    println!("Product is: {}", product);
}

fn main() {
    env_logger::init();
    example_function();
}

Run the example:

cargo run --example main

License

This project is licensed under the MIT License. See the LICENSE file for details.