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