百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

log-derive

> 编程语言
开源

用于自动记录函数输出的程序宏

194 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于自动记录函数输出的程序宏

log-derive

A Rust macro to part of the log facade that auto generates loggings for functions output.

  • Documentation

Usage

Add this to your Cargo.toml:

toml
[dependencies]
log-derive = "0.3"
log = "0.4"

and for Rust Edition 2015 add this to your crate root:

rust
#[macro_use]
extern crate log_derive;
extern crate log;

In Rust Edition 2018 you can simply do:

rust
use log_derive::logfn;

After that all you need is to add the according macro above a function that,

either returns an output or receive an input that implements the Debug trait.

Examples

rust
 #[logfn(Err = "Error", fmt = "Failed Sending Packet: {:?}")]
 fn send_hi(addr: SocketAddr) -> Result {
     let mut stream = TcpStream::connect(addr)?;
     stream.write(b"Hi!")?;
     Ok( () )
 }
rust
#[logfn(Trace)]
#[logfn_inputs(Info)]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}
rust
#[logfn(Trace, fmt = "testing the num: {:?}")]
fn test_log(a: u8) -> String {
  (a*2).to_string()
}

Output

The output of the fibonacci example:

…

If you expand the output of the #[logfn] macro the resulting code will look something like this:

rust
fn fibonacci(n: u32) -> u32 {
    let result = (move || match n {
        0 => 1,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    })();
    log::log!(log::Level::Info, "fibonacci() -> {}", result);
    result
}

If the function returns a Result it will match through it to split between the Ok LogLevel and the Err LogLevel

The expansion of the #[logfn_inputs] macro will look something like this:

rust
fn fibonacci(n: u32) -> u32 {
    log::log!(log::Level::Info, "fibonacci(n: {:?})", n);
    match n {
        0 => 1,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

Of course the log! macro will be expanded too and it will be a bit more messy.

Note

The log_ts feature will fail your compilation in a no-std enviroment. it can only be used where std is available. (as it uses std::time::Instant)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustloggingmetaprogrammingproc-macrorust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言