instrument_level/lib.rs
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
5mod instrument;
6
7pub(crate) use instrument::*;
8
9pub(crate) use proc_macro::TokenStream;
10pub(crate) use proc_macro2::TokenStream as TokenStream2;
11pub(crate) use quote::quote;
12pub(crate) use syn::*;
13
14/// Enables trace-level instrumentation for the decorated function.
15///
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.
18///
19/// # Arguments
20///
21/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
22/// - `item` - The TokenStream representing the function to be instrumented.
23///
24/// # Returns
25///
26/// Returns the expanded TokenStream with tracing instrumentation applied.
27///
28/// # Examples
29///
30/// ```
31/// use tracing;
32/// use instrument_level::*;
33///
34/// #[instrument_trace]
35/// async fn test(x: i32, y: i32) -> i32 {
36/// x + y
37/// }
38/// ```
39#[proc_macro_attribute]
40pub fn instrument_trace(attr: TokenStream, item: TokenStream) -> TokenStream {
41 instrument_trace_macro(attr, item)
42}
43
44/// Enables debug-level instrumentation for the decorated function.
45///
46/// This attribute macro wraps the function with `#[::tracing::instrument(level = "debug")]`,
47/// enabling automatic tracing instrumentation at the debug level.
48///
49/// # Arguments
50///
51/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
52/// - `item` - The TokenStream representing the function to be instrumented.
53///
54/// # Returns
55///
56/// Returns the expanded TokenStream with tracing instrumentation applied.
57///
58/// # Examples
59///
60/// ```
61/// use tracing;
62/// use instrument_level::*;
63///
64/// #[instrument_debug]
65/// async fn test(x: i32, y: i32) -> i32 {
66/// x + y
67/// }
68///
69/// #[instrument_debug(skip_all)]
70/// async fn test_skip_all(x: i32, y: i32) -> i32 {
71/// x + y
72/// }
73/// ```
74#[proc_macro_attribute]
75pub fn instrument_debug(attr: TokenStream, item: TokenStream) -> TokenStream {
76 instrument_debug_macro(attr, item)
77}
78
79/// Enables info-level instrumentation for the decorated function.
80///
81/// This attribute macro wraps the function with `#[::tracing::instrument(level = "info")]`,
82/// enabling automatic tracing instrumentation at the info level.
83///
84/// # Arguments
85///
86/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
87/// - `item` - The TokenStream representing the function to be instrumented.
88///
89/// # Returns
90///
91/// Returns the expanded TokenStream with tracing instrumentation applied.
92///
93/// # Examples
94///
95/// ```
96/// use tracing;
97/// use instrument_level::*;
98///
99/// #[instrument_info]
100/// async fn test(x: i32, y: i32) -> i32 {
101/// x + y
102/// }
103///
104/// #[instrument_info(skip_all)]
105/// async fn test_skip_all(x: i32, y: i32) -> i32 {
106/// x + y
107/// }
108/// ```
109#[proc_macro_attribute]
110pub fn instrument_info(attr: TokenStream, item: TokenStream) -> TokenStream {
111 instrument_info_macro(attr, item)
112}
113
114/// Enables warn-level instrumentation for the decorated function.
115///
116/// This attribute macro wraps the function with `#[::tracing::instrument(level = "warn")]`,
117/// enabling automatic tracing instrumentation at the warn level.
118///
119/// # Arguments
120///
121/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
122/// - `item` - The TokenStream representing the function to be instrumented.
123///
124/// # Returns
125///
126/// Returns the expanded TokenStream with tracing instrumentation applied.
127///
128/// # Examples
129///
130/// ```
131/// use tracing;
132/// use instrument_level::*;
133///
134/// #[instrument_warn]
135/// async fn test(x: i32, y: i32) -> i32 {
136/// x + y
137/// }
138///
139/// #[instrument_warn(skip_all)]
140/// async fn test_skip_all(x: i32, y: i32) -> i32 {
141/// x + y
142/// }
143/// ```
144#[proc_macro_attribute]
145pub fn instrument_warn(attr: TokenStream, item: TokenStream) -> TokenStream {
146 instrument_warn_macro(attr, item)
147}
148
149/// Enables error-level instrumentation for the decorated function.
150///
151/// This attribute macro wraps the function with `#[::tracing::instrument(level = "error")]`,
152/// enabling automatic tracing instrumentation at the error level.
153///
154/// # Arguments
155///
156/// - `attr` - Additional tracing instrument parameters (optional): target, name, skip, fields, etc.
157/// - `item` - The TokenStream representing the function to be instrumented.
158///
159/// # Returns
160///
161/// Returns the expanded TokenStream with tracing instrumentation applied.
162///
163/// # Examples
164///
165/// ```
166/// use tracing;
167/// use instrument_level::*;
168///
169/// #[instrument_error]
170/// async fn test(x: i32, y: i32) -> i32 {
171/// x + y
172/// }
173///
174/// #[instrument_error(skip_all)]
175/// async fn test_skip_all(x: i32, y: i32) -> i32 {
176/// x + y
177/// }
178/// ```
179#[proc_macro_attribute]
180pub fn instrument_error(attr: TokenStream, item: TokenStream) -> TokenStream {
181 instrument_error_macro(attr, item)
182}