| Crates.io | funlog |
| lib.rs | funlog |
| version | 0.2.1 |
| created_at | 2024-11-28 14:29:07.199175+00 |
| updated_at | 2025-09-06 11:48:55.814489+00 |
| description | A procedural macro for tracing Rust function calls |
| homepage | https://github.com/koory1st/funlog |
| repository | https://github.com/koory1st/funlog |
| max_upload_size | |
| id | 1464541 |
| size | 144,345 |
English | 中文
一个用于跟踪 Rust 函数调用的过程宏库。
Funlog 是一个轻量级的 Rust 过程宏,用于自动记录函数的调用信息。它可以记录函数的参数、返回值,并支持多种日志级别和灵活的配置选项。
将以下内容添加到您的 Cargo.toml 文件中:
[dependencies]
funlog = "0.1.0"
# 如果使用日志级别(非 print),还需要添加日志库
log = "0.4"
env_logger = "0.10"
use funlog::funlog;
#[funlog(debug)]
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
env_logger::init();
let result = add(3, 5);
println!("Result: {}", result);
}
use funlog::funlog;
#[funlog(print)]
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("World");
}
print - 使用 println! 宏(无需日志库设置)trace - 使用 log::trace!debug - 使用 log::debug!info - 使用 log::info!warn - 使用 log::warn!error - 使用 log::error!all - 记录所有函数参数(默认)none - 不记录参数params(param1, param2, ...) - 记录指定参数onStart - 仅在函数开始时记录onEnd - 仅在函数结束时记录onStartEnd - 在开始和结束时都记录(默认)retVal - 在日志中包含返回值#[funlog(info, all, retVal)]
fn multiply(a: i32, b: i32) -> i32 {
a * b
}
#[funlog(debug, params(name, age))]
fn create_user(name: &str, age: u32, email: &str) -> String {
format!("User: {} ({})", name, age)
}
#[funlog(warn, onEnd, retVal)]
fn expensive_calculation() -> f64 {
// 复杂计算
std::thread::sleep(std::time::Duration::from_millis(100));
42.0
}
#[funlog(info, onStartEnd, params(input), retVal)]
fn process_data(input: &str, config: &Config) -> Result<String, Error> {
// 处理逻辑
Ok(input.to_uppercase())
}
项目包含了丰富的示例,展示了所有功能组合:
# 运行基本示例
cargo run --example raw_debug
# 运行参数记录示例
cargo run --example raw_all
cargo run --example raw_params
# 运行位置控制示例
cargo run --example raw_position_start
cargo run --example raw_position_end
# 运行返回值记录示例
cargo run --example raw_return_value
# 查看所有示例
ls examples/
详细的示例说明请参考 examples/README.md。
运行所有测试:
# 运行单元测试
cargo test
# 运行示例测试脚本
./run_tests.sh
Funlog 是一个过程宏,在编译时分析函数并生成相应的日志代码。它只在 debug 构建中生效,在 release 构建中会完全移除,确保零运行时开销。
本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。
欢迎提交 Issue 和 Pull Request!