tracing-fn

Crates.iotracing-fn
lib.rstracing-fn
version0.1.0
created_at2025-09-19 01:15:47.573792+00
updated_at2025-09-19 01:15:47.573792+00
description该库提供了一个派生宏, 用于追踪函数的调用参数和返回值
homepage
repositoryhttps://github.com/mengyou1024/tracing-fn.git
max_upload_size
id1845632
size17,255
梦幽 (mengyou1024)

documentation

README

tracing-fn

一个 Rust 过程宏,用于为函数添加 tracing 功能。

功能

  1. 在函数调用前输出函数调用参数
  2. 在函数调用后输出函数的返回值和执行耗时
  3. 使用 tracing 库进行输出
  4. 可以指定日志输出的等级(默认为 trace)
  5. 参数输出可以跳过某些参数
  6. 如果使用该库的项目为 Release 模式,则默认不添加输出功能,但可以通过 force 参数强制使 Release 模式也输出

使用方法

Cargo.toml 中添加依赖:

[dependencies]
tracing-fn = "..." # 根据实际情况调整路径
tracing = "0.1"
tracing_subscriber = "0.3"

基本使用

use tracing_fn::tracing_fn;

#[tracing_fn]
fn hello_world(name: &str) -> String {
    format!("Hello, {}!", name)
}

指定日志等级

#[tracing_fn(level = "info")]
fn add(a: i32, b: i32) -> i32 {
    a + b
}

跳过特定参数

#[tracing_fn(skip = "password")]
fn login(username: &str, password: &str) -> bool {
    // password 参数不会被记录
    !username.is_empty() && !password.is_empty()
}

在 Release 模式下强制启用 tracing

在 Release 模式下,默认不启用 tracing 功能。如果需要强制启用,可以使用 force 参数:

#[tracing_fn(force = true)]
fn important_function(a: i32) -> i32 {
    a * 2
}

初始化 tracing

为了使 tracing 正常工作,需要在程序开始时初始化 tracing:

fn main() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .init();
    
    // 你的代码
}

运行示例

# 在调试模式下运行(默认启用 tracing)
cargo run --example example

# 在 Release 模式下运行(默认不启用 tracing)
cargo run --example example --release
Commit count: 0

cargo fmt