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