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