1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// SPDX-License-Identifier: MIT OR Apache-2.0
//SPDX-License-Identifier: MIT OR Apache-2.0
//! # Logwise Procedural Macros
//!
//! This crate provides procedural macros for the logwise logging library, generating efficient
//! structured logging code at compile time. The macros transform format strings with key-value
//! pairs into optimized logging calls that use the `PrivateFormatter` system.
//!
//! ## Architecture
//!
//! Each logging macro follows a consistent three-phase pattern:
//! 1. **Pre-phase**: Creates a `LogRecord` using `*_pre()` functions with location metadata
//! 2. **Format phase**: Uses `PrivateFormatter` to write structured data via `lformat_impl`
//! 3. **Post-phase**: Completes logging using `*_post()` functions (sync or async variants)
//!
//! ## Log Levels and Build Configuration
//!
//! The macros respect logwise's opinionated logging levels:
//! - `trace_*`: Debug builds only, per-thread activation via `Context::currently_tracing()`
//! - `debuginternal_*`: Debug builds only, requires `declare_logging_domain!()` at crate root
//! - `info_*`: Debug builds only, for supporting downstream crates
//! - `warn_*`, `error_*`, `perfwarn_*`: Available in all builds
//!
//! ## Usage Example
//!
//! ```rust
//! use logwise_proc::*;
//!
//! // This macro call:
//! // logwise::info_sync!("User {name} has {count} items", name="alice", count=42);
//!
//! // Expands to approximately:
//! // {
//! // let mut record = logwise::hidden::info_sync_pre(file!(), line!(), column!());
//! // let mut formatter = logwise::hidden::PrivateFormatter::new(&mut record);
//! // formatter.write_literal("User ");
//! // formatter.write_val("alice");
//! // formatter.write_literal(" has ");
//! // formatter.write_val(42);
//! // formatter.write_literal(" items");
//! // logwise::hidden::info_sync_post(record);
//! // }
//! ```
//!
//! ## Key-Value Parsing
//!
//! Format strings support embedded key-value pairs:
//! - Keys are extracted from `{key}` placeholders in the format string
//! - Values are provided as `key=value` parameters after the format string
//! - The parser handles complex Rust expressions as values, including method calls and literals
//!
//! ## Privacy Integration
//!
//! These macros integrate with logwise's privacy system via the `Loggable` trait.
//! Values are processed through `formatter.write_val()` which respects privacy constraints.
use TokenStream;
pub
/// Low-level macro for generating formatter calls from format strings.
///
/// See the internal `lformat` module implementation for details.
/// Synchronous trace-level logging (debug builds only, per-thread activation).
///
/// Active when `Context::currently_tracing()` is true. Compiled out in release builds.
///
/// ```
/// logwise::trace_sync!("Processing {value}", value=42);
/// ```
/// Asynchronous trace-level logging (debug builds only, per-thread activation).
///
/// Async variant of `trace_sync!`. Compiled out in release builds.
///
/// ```
/// async fn example() {
/// logwise::trace_async!("Processing {size} bytes", size=42);
/// }
/// ```
/// Synchronous debug-internal logging (debug builds only).
///
/// Requires `declare_logging_domain!()` at crate root. Compiled out in release builds.
///
/// ```
/// logwise::declare_logging_domain!();
/// fn main() {
/// logwise::debuginternal_sync!("Debug: {val}", val=42);
/// }
/// ```
/// Asynchronous debug-internal logging (debug builds only).
///
/// Async variant of `debuginternal_sync!`. Requires `declare_logging_domain!()` at crate root.
///
/// ```
/// logwise::declare_logging_domain!();
/// async fn example() {
/// logwise::debuginternal_async!("Starting {id}", id="task_123");
/// }
/// ```
/// Synchronous info-level logging (debug builds only).
///
/// For important operational information. Compiled out in release builds.
///
/// ```
/// logwise::info_sync!("Processing {count} items", count=42);
/// ```
/// Asynchronous info-level logging (debug builds only).
///
/// Async variant of `info_sync!`. Compiled out in release builds.
///
/// ```
/// async fn example() {
/// logwise::info_async!("Connected to {host}", host="localhost");
/// }
/// ```
/// Synchronous warning-level logging (all builds).
///
/// For suspicious conditions that warrant attention. Active in release builds.
///
/// ```
/// logwise::warn_sync!("Large payload: {size} bytes", size=1024);
/// ```
/// Begin a performance warning interval (all builds).
///
/// Returns a guard that warns on drop if operation takes too long. Active in release builds.
///
/// ```
/// let interval = logwise::perfwarn_begin!("Database query");
/// // ... operation ...
/// drop(interval);
/// ```
/// Block-scoped performance warning interval (all builds).
///
/// Wraps a code block with performance monitoring. Preserves block's return value.
///
/// ```
/// # fn expensive() -> i32 { 42 }
/// let result = logwise::perfwarn!("Loading users", {
/// expensive()
/// });
/// ```
/// Conditional performance warning interval.
///
/// Only logs if duration exceeds the specified threshold.
///
/// ```
/// # use std::time::Duration;
/// let threshold = Duration::from_millis(100);
/// let interval = logwise::perfwarn_begin_if!(threshold, "operation {p}", p=42);
/// drop(interval);
/// ```
/// Synchronous error-level logging (all builds).
///
/// For actual error conditions in Result error paths. Active in release builds.
///
/// ```
/// logwise::error_sync!("Failed to read file: {error}", error="not found");
/// ```
/// Asynchronous error-level logging (all builds).
///
/// Async variant of `error_sync!`. Active in release builds.
///
/// ```
/// async fn example() {
/// logwise::error_async!("API failed: {error}", error="timeout");
/// }
/// ```
/// Synchronous mandatory-level logging (all builds).
///
/// Always enabled for temporary printf-style debugging. Remove before committing.
///
/// ```
/// logwise::mandatory_sync!("Debug value: {val}", val=42);
/// ```
/// Asynchronous mandatory-level logging (all builds).
///
/// Async variant of `mandatory_sync!`. Remove before committing.
///
/// ```
/// async fn example() {
/// logwise::mandatory_async!("Debug: {val}", val=42);
/// }
/// ```
/// Synchronous profile-level logging (all builds).
///
/// For temporary profiling and performance investigation. Remove before committing.
///
/// ```
/// logwise::profile_sync!("Operation took {ms} ms", ms=100);
/// ```
/// Asynchronous profile-level logging (all builds).
///
/// Async variant of `profile_sync!`. Remove before committing.
///
/// ```
/// async fn example() {
/// logwise::profile_async!("Async timing: {ms} ms", ms=50);
/// }
/// ```
/// Begin a profile interval (all builds).
///
/// Logs BEGIN when created and END with duration when dropped. Each interval has a unique ID.
///
/// ```
/// let interval = logwise::profile_begin!("database_query");
/// // ... operation ...
/// drop(interval);
/// ```
/// Attribute macro to automatically profile a function's execution time.
///
/// Wraps function body to log BEGIN on entry and END with duration on return.
///
/// ```rust
/// #[logwise::profile]
/// fn expensive_operation() -> i32 {
/// (0..1000).sum()
/// }
/// ```