Macro trace_fn

Source
macro_rules! trace_fn {
    ($fn_name:expr) => { ... };
    ($fn_name:expr, $($key:expr => $value:expr),*) => { ... };
}
Expand description

A macro to automatically instrument functions with tracing spans.

This attribute macro can be applied to any function to automatically:

  • Create a TRACE-level span when the function is entered
  • Log the function’s parameters (if any)
  • Log the function’s return value (if any)
  • Automatically close the span when the function exits
  • Handle both Result and direct return types

§Usage

use rustic_net::trace_fn;

fn process_data(data: &[f32], scale: f32) -> Vec<f32> {
    trace_fn!("process_data");
    data.iter().map(|x| x * scale).collect()
}

fn load_model(path: &str) -> Result<(), String> {
    trace_fn!("load_model");
    // Implementation...
    Ok(())
}

§Generated Code

The macro expands to something like:

fn process_data(data: &[f32], scale: f32) -> Vec<f32> {
    trace_fn!("process_data", data_len => data.len(), scale => scale);
    let result = { /* original function body */ };
    result
}

§Performance

  • In release builds with tracing level above TRACE, the spans are optimized away
  • Function parameters are only evaluated if the corresponding log level is enabled

§Examples


// Basic usage
fn process() {
    trace_fn!("process");
    // ...
}

// With parameters
fn process_data(id: u64, data: &[u8]) {
    trace_fn!("process_data");
    // ...
}