Skip to content

Commit 53b22bc

Browse files
committed
feat: v1.0.0
1 parent 92b2e41 commit 53b22bc

File tree

6 files changed

+118
-44
lines changed

6 files changed

+118
-44
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "instrument-level"
3-
version = "10.14.11"
3+
version = "1.0.0"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["root@ltpp.vip"]
77
license = "MIT"
8-
description = """"""
8+
description = "A Rust procedural macro collection providing convenient tracing instrumentation macros for different log levels (trace, debug, info, warn, error). This crate simplifies the process of adding tracing spans to functions with pre-configured log levels."
99
keywords = ["http", "request", "response", "tcp", "cross-platform"]
1010
repository = "https://github.com/crates-dev/instrument-level.git"
1111
categories = ["network-programming", "web-programming"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 尤雨东
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<center>
2+
3+
## instrument-level
4+
5+
[![](https://img.shields.io/crates/v/instrument-level.svg)](https://crates.io/crates/instrument-level)
6+
[![](https://img.shields.io/crates/d/instrument-level.svg)](https://img.shields.io/crates/d/instrument-level.svg)
7+
[![](https://docs.rs/instrument-level/badge.svg)](https://docs.rs/instrument-level)
8+
[![](https://github.com/crates-dev/instrument-level/workflows/Rust/badge.svg)](https://github.com/crates-dev/instrument-level/actions?query=workflow:Rust)
9+
[![](https://img.shields.io/crates/l/instrument-level.svg)](./LICENSE)
10+
11+
</center>
12+
13+
[Official Documentation](https://docs.ltpp.vip/instrument-level/)
14+
15+
[Api Docs](https://docs.rs/instrument-level/latest/instrument_level/)
16+
17+
> A Rust procedural macro collection providing convenient tracing instrumentation macros for different log levels (trace, debug, info, warn, error). This crate simplifies the process of adding tracing spans to functions with pre-configured log levels.
18+
19+
## Installation
20+
21+
To use this crate, you can run cmd:
22+
23+
```shell
24+
cargo add instrument-level
25+
```
26+
27+
## Usage
28+
29+
This crate provides five attribute macros for different tracing log levels:
30+
31+
### `#[instrument_trace]` - Trace Level Instrumentation
32+
33+
Use this macro to add trace-level logging instrumentation to functions. This is the most verbose log level and automatically excludes all function arguments from span fields using `skip_all`.
34+
35+
### `#[instrument_debug]` - Debug Level Instrumentation
36+
37+
Use this macro to add debug-level logging instrumentation to functions. Ideal for development and debugging purposes.
38+
39+
### `#[instrument_info]` - Info Level Instrumentation
40+
41+
Use this macro to add info-level logging instrumentation to functions. Suitable for general informational messages.
42+
43+
### `#[instrument_warn]` - Warning Level Instrumentation
44+
45+
Use this macro to add warning-level logging instrumentation to functions. Use for potentially harmful situations.
46+
47+
### `#[instrument_error]` - Error Level Instrumentation
48+
49+
Use this macro to add error-level logging instrumentation to functions. Use for error conditions that don't necessarily stop program execution.
50+
51+
Each macro accepts optional tracing parameters such as `target`, `name`, `skip`, `fields`, etc., which can be used to customize the span behavior according to your needs.
52+
53+
## License
54+
55+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
56+
57+
## Contributing
58+
59+
Contributions are welcome! Please open an issue or submit a pull request.
60+
61+
## Contact
62+
63+
For any inquiries, please reach out to the author at [root@ltpp.vip](mailto:root@ltpp.vip).

src/instrument/fn.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
use crate::*;
22

3-
/// Helper function to parse additional instrument parameters from attributes.
4-
///
5-
/// # Arguments
6-
///
7-
/// - `attr` - The attribute TokenStream containing additional parameters
8-
///
9-
/// # Returns
10-
///
11-
/// - `TokenStream2` - The parsed additional parameters as a TokenStream
12-
fn parse_instrument_params(attr: TokenStream) -> TokenStream2 {
13-
if attr.is_empty() {
14-
return quote! {};
15-
}
16-
let tokens: TokenStream2 = attr.into();
17-
quote! { #tokens }
18-
}
19-
203
/// Macro for adding trace-level instrumentation to functions.
214
///
5+
/// This macro instruments functions with trace-level logging and automatically includes `skip_all`
6+
/// to exclude all function arguments from the span fields.
7+
///
228
/// # Arguments
239
///
24-
/// - `_attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
10+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
2511
/// - `item` - The function to instrument
2612
///
2713
/// # Returns
2814
///
29-
/// - `TokenStream` - The instrumented function with trace-level logging
30-
pub fn instrument_trace_macro(_attr: TokenStream, item: TokenStream) -> TokenStream {
15+
/// - `TokenStream` - The instrumented function with trace-level logging and skip_all enabled
16+
pub(crate) fn instrument_trace_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
3117
let input_fn: ItemFn = parse_macro_input!(item as ItemFn);
32-
let params: TokenStream2 = parse_instrument_params(_attr);
18+
let params: TokenStream2 = attr.into();
3319
let expanded: TokenStream2 = quote! {
34-
#[::tracing::instrument(level = "trace", #params)]
20+
#[::tracing::instrument(level = "trace", skip_all, #params)]
3521
#input_fn
3622
};
3723
TokenStream::from(expanded)
@@ -41,15 +27,15 @@ pub fn instrument_trace_macro(_attr: TokenStream, item: TokenStream) -> TokenStr
4127
///
4228
/// # Arguments
4329
///
44-
/// - `_attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
30+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
4531
/// - `item` - The function to instrument
4632
///
4733
/// # Returns
4834
///
4935
/// - `TokenStream` - The instrumented function with debug-level logging
50-
pub fn instrument_debug_macro(_attr: TokenStream, item: TokenStream) -> TokenStream {
36+
pub(crate) fn instrument_debug_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
5137
let input_fn: ItemFn = parse_macro_input!(item as ItemFn);
52-
let params: TokenStream2 = parse_instrument_params(_attr);
38+
let params: TokenStream2 = attr.into();
5339
let expanded: TokenStream2 = quote! {
5440
#[::tracing::instrument(level = "debug", #params)]
5541
#input_fn
@@ -61,15 +47,15 @@ pub fn instrument_debug_macro(_attr: TokenStream, item: TokenStream) -> TokenStr
6147
///
6248
/// # Arguments
6349
///
64-
/// - `_attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
50+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
6551
/// - `item` - The function to instrument
6652
///
6753
/// # Returns
6854
///
6955
/// - `TokenStream` - The instrumented function with info-level logging
70-
pub fn instrument_info_macro(_attr: TokenStream, item: TokenStream) -> TokenStream {
56+
pub(crate) fn instrument_info_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
7157
let input_fn: ItemFn = parse_macro_input!(item as ItemFn);
72-
let params: TokenStream2 = parse_instrument_params(_attr);
58+
let params: TokenStream2 = attr.into();
7359
let expanded: TokenStream2 = quote! {
7460
#[::tracing::instrument(level = "info", #params)]
7561
#input_fn
@@ -81,15 +67,15 @@ pub fn instrument_info_macro(_attr: TokenStream, item: TokenStream) -> TokenStre
8167
///
8268
/// # Arguments
8369
///
84-
/// - `_attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
70+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
8571
/// - `item` - The function to instrument
8672
///
8773
/// # Returns
8874
///
8975
/// - `TokenStream` - The instrumented function with warn-level logging
90-
pub fn instrument_warn_macro(_attr: TokenStream, item: TokenStream) -> TokenStream {
76+
pub(crate) fn instrument_warn_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
9177
let input_fn: ItemFn = parse_macro_input!(item as ItemFn);
92-
let params: TokenStream2 = parse_instrument_params(_attr);
78+
let params: TokenStream2 = attr.into();
9379
let expanded: TokenStream2 = quote! {
9480
#[::tracing::instrument(level = "warn", #params)]
9581
#input_fn
@@ -101,15 +87,15 @@ pub fn instrument_warn_macro(_attr: TokenStream, item: TokenStream) -> TokenStre
10187
///
10288
/// # Arguments
10389
///
104-
/// - `_attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
90+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
10591
/// - `item` - The function to instrument
10692
///
10793
/// # Returns
10894
///
10995
/// - `TokenStream` - The instrumented function with error-level logging
110-
pub fn instrument_error_macro(_attr: TokenStream, item: TokenStream) -> TokenStream {
96+
pub(crate) fn instrument_error_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
11197
let input_fn: ItemFn = parse_macro_input!(item as ItemFn);
112-
let params: TokenStream2 = parse_instrument_params(_attr);
98+
let params: TokenStream2 = attr.into();
11399
let expanded: TokenStream2 = quote! {
114100
#[::tracing::instrument(level = "error", #params)]
115101
#input_fn

src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! A Rust procedural macro collection providing convenient tracing instrumentation macros
2+
//! for different log levels (trace, debug, info, warn, error). This crate simplifies
3+
//! the process of adding tracing spans to functions with pre-configured log levels.
4+
15
mod instrument;
26

37
pub(crate) use instrument::*;
@@ -9,12 +13,12 @@ pub(crate) use syn::*;
913

1014
/// Enables trace-level instrumentation for the decorated function.
1115
///
12-
/// This attribute macro wraps the function with `#[::tracing::instrument(level = "trace")]`,
13-
/// enabling automatic tracing instrumentation at the trace level.
16+
/// This attribute macro wraps the function with `#[::tracing::instrument(level = "trace", skip_all)]`,
17+
/// enabling automatic tracing instrumentation at the trace level with all arguments excluded from span fields.
1418
///
1519
/// # Arguments
1620
///
17-
/// - `attr` - The attribute TokenStream (currently unused).
21+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
1822
/// - `item` - The TokenStream representing the function to be instrumented.
1923
///
2024
/// # Returns
@@ -49,7 +53,7 @@ pub fn instrument_trace(attr: TokenStream, item: TokenStream) -> TokenStream {
4953
///
5054
/// # Arguments
5155
///
52-
/// - `attr` - The attribute TokenStream (currently unused).
56+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
5357
/// - `item` - The TokenStream representing the function to be instrumented.
5458
///
5559
/// # Returns
@@ -84,7 +88,7 @@ pub fn instrument_debug(attr: TokenStream, item: TokenStream) -> TokenStream {
8488
///
8589
/// # Arguments
8690
///
87-
/// - `attr` - The attribute TokenStream (currently unused).
91+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
8892
/// - `item` - The TokenStream representing the function to be instrumented.
8993
///
9094
/// # Returns
@@ -119,7 +123,7 @@ pub fn instrument_info(attr: TokenStream, item: TokenStream) -> TokenStream {
119123
///
120124
/// # Arguments
121125
///
122-
/// - `attr` - The attribute TokenStream (currently unused).
126+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
123127
/// - `item` - The TokenStream representing the function to be instrumented.
124128
///
125129
/// # Returns
@@ -154,7 +158,7 @@ pub fn instrument_warn(attr: TokenStream, item: TokenStream) -> TokenStream {
154158
///
155159
/// # Arguments
156160
///
157-
/// - `attr` - The attribute TokenStream (currently unused).
161+
/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
158162
/// - `item` - The TokenStream representing the function to be instrumented.
159163
///
160164
/// # Returns

0 commit comments

Comments
 (0)