Skip to content

show-t/rust-logkit-macros

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

logkit-macro

logkit-macros is a proc-macro attribute that automatically expands ? operators in functions and logs errors individually in Rust.
logkit-macros ใฏ Rust ใง้–ขๆ•ฐๅ†…ใฎ ? ๆผ”็ฎ—ๅญใ‚’่‡ชๅ‹•็š„ใซๅฑ•้–‹ใ—ใ€ใ‚จใƒฉใƒผๆ™‚ใซๅ€‹ๅˆฅใซใƒญใ‚ฐๅ‡บๅŠ›ใ™ใ‚‹ ๅฑžๆ€งใƒžใ‚ฏใƒญ (proc-macro) ใงใ™ใ€‚

๐Ÿ“ฆ Features

  • Attach #[try_with_log] to a function and all ? operators inside it are transformed
    #[try_with_log] ใ‚’้–ขๆ•ฐใซใคใ‘ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€้–ขๆ•ฐๅ†…ใฎใ™ในใฆใฎ ? ๆผ”็ฎ—ๅญใ‚’ๅค‰ๆ›
  • When an error occurs, logs are output per function using tracing
    ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใ€tracing ใ‚’ไฝฟใฃใฆ้–ขๆ•ฐๅ˜ไฝใงใƒญใ‚ฐใ‚’ๅ‡บๅŠ›ใ™ใ‚‹

Example

#[try_with_log]
fn multi_try_fn() -> Result<(), anyhow::Error> {
    step1()?;  // logs if an error occurs
    step2()?;
    Ok(())
}

๐Ÿ“š Requirements

  • Rust 1.70+ (with Cargo)

๐Ÿš€ Getting Started

  1. Add to Cargo.toml:
[dependencies]
logkit-macros = { git = "https://github.com/show-t/rust-logkit-macros" }

๐Ÿ“– Usage

  1. Initialize tracing_subscriber:
tracing_subscriber::fmt::init();
  1. Add #[try_with_log] to the function:
use logkit_macros::try_with_log;
use anyhow::Result;

#[try_with_log]
fn example() -> Result<()> {
  do_something()?; // logs on error
  Ok(())
}

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ src/             # Rust source files
โ”‚   โ”œโ”€โ”€ macros_impl/ # Macro implementation
โ”‚   โ””โ”€โ”€ lib.rs       # Crate public interface
โ”œโ”€โ”€ target/          # Compiled Rust output
โ”œโ”€โ”€ cargo.toml       # Cargo configuration
โ”œโ”€โ”€ .gitignore   
โ”œโ”€โ”€ LICENSE   
โ””โ”€โ”€ README.md        # Project documentation

๐Ÿ“„ License

This project is licensed under the MIT License.
ๆœฌใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใฏ MIT ใƒฉใ‚คใ‚ปใƒณใ‚นใฎใ‚‚ใจใงๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใพใ™ใ€‚

See the LICENSE file for more details.
่ฉณ็ดฐใฏ LICENSE ใƒ•ใ‚กใ‚คใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages