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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! Tracing compatibility layer for structured logging and spans.
//!
//! This module provides a unified interface for tracing that works whether or not
//! the `tracing-integration` feature is enabled:
//!
//! - **With feature enabled**: Re-exports from the `tracing` crate for full functionality.
//! - **Without feature**: No-op macros that compile to nothing for zero runtime overhead.
//!
//! # Usage
//!
//! ```rust,ignore
//! use asupersync::tracing_compat::{info, debug, trace, span, Level};
//!
//! // These compile to no-ops when tracing-integration is disabled
//! info!("Starting operation");
//! debug!(task_id = ?id, "Task spawned");
//!
//! let _span = span!(Level::INFO, "my_operation");
//! ```
//!
//! # Feature Flag
//!
//! Enable tracing by adding the feature to your `Cargo.toml`:
//!
//! ```toml
//! asupersync = { version = "0.1", features = ["tracing-integration"] }
//! ```
#[cfg(feature = "tracing-integration")]
pub use tracing::{
Instrument, Level, Span, debug, debug_span, error, error_span, event, info, info_span,
instrument, span, trace, trace_span, warn, warn_span,
};
// When tracing is disabled, provide no-op macros
#[cfg(not(feature = "tracing-integration"))]
mod noop {
//! No-op implementations when tracing is disabled.
//!
//! These macros expand to nothing, ensuring zero compile-time and runtime cost.
/// No-op trace-level logging macro.
#[macro_export]
macro_rules! trace {
($($arg:tt)*) => {};
}
/// No-op debug-level logging macro.
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {};
}
/// No-op info-level logging macro.
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {};
}
/// No-op warn-level logging macro.
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {};
}
/// No-op error-level logging macro.
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {};
}
/// No-op event macro.
#[macro_export]
macro_rules! event {
($($arg:tt)*) => {};
}
/// No-op span macro that returns a `NoopSpan`.
#[macro_export]
macro_rules! span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op trace_span macro.
#[macro_export]
macro_rules! trace_span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op debug_span macro.
#[macro_export]
macro_rules! debug_span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op info_span macro.
#[macro_export]
macro_rules! info_span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op warn_span macro.
#[macro_export]
macro_rules! warn_span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op error_span macro.
#[macro_export]
macro_rules! error_span {
($($arg:tt)*) => {
$crate::tracing_compat::NoopSpan
};
}
/// No-op instrument attribute (does nothing).
/// This is a placeholder - the actual `#[instrument]` attribute requires proc-macro.
#[macro_export]
macro_rules! instrument {
($($arg:tt)*) => {};
}
// Re-export the macros at module level
pub use crate::{
debug, debug_span, error, error_span, event, info, info_span, instrument, span, trace,
trace_span, warn, warn_span,
};
}
#[cfg(not(feature = "tracing-integration"))]
pub use noop::*;
/// A no-op span that does nothing.
///
/// When tracing is disabled, span macros return this type. It implements
/// the necessary methods to allow code like `span.enter()` to compile
/// without the tracing feature.
#[cfg(not(feature = "tracing-integration"))]
#[derive(Debug, Clone, Copy)]
pub struct NoopSpan;
#[cfg(not(feature = "tracing-integration"))]
impl NoopSpan {
/// Returns a no-op guard that does nothing on drop.
#[inline]
#[must_use]
pub fn enter(&self) -> NoopGuard {
NoopGuard
}
/// Returns self (no-op).
#[inline]
#[must_use]
pub fn entered(self) -> Self {
self
}
/// Returns self (no-op).
#[inline]
#[must_use]
pub fn or_current(self) -> Self {
self
}
/// Returns self (no-op).
#[inline]
pub fn follows_from(&self, _span: &Self) {}
/// Returns true (always "enabled" to avoid branch differences).
#[inline]
#[must_use]
pub fn is_disabled(&self) -> bool {
true
}
/// Records a value (no-op).
#[inline]
pub fn record<V>(&self, _field: &str, _value: V) {}
/// Returns a no-op span (current span is always a no-op when disabled).
#[inline]
#[must_use]
pub fn current() -> Self {
Self
}
/// Returns a no-op span (none is always a no-op when disabled).
#[inline]
#[must_use]
pub fn none() -> Self {
Self
}
}
/// A no-op span guard that does nothing on drop.
#[cfg(not(feature = "tracing-integration"))]
#[derive(Debug)]
pub struct NoopGuard;
/// No-op level type for when tracing is disabled.
#[cfg(not(feature = "tracing-integration"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Level;
#[cfg(not(feature = "tracing-integration"))]
impl Level {
/// Trace level (most verbose).
pub const TRACE: Self = Self;
/// Debug level.
pub const DEBUG: Self = Self;
/// Info level.
pub const INFO: Self = Self;
/// Warn level.
pub const WARN: Self = Self;
/// Error level (least verbose).
pub const ERROR: Self = Self;
}
/// Alias for `NoopSpan` when tracing is disabled.
#[cfg(not(feature = "tracing-integration"))]
pub type Span = NoopSpan;
/// No-op `Instrument` trait when tracing is disabled.
///
/// This trait is implemented for all `Future` types and does nothing,
/// allowing code using `.instrument(span)` to compile without the feature.
#[cfg(not(feature = "tracing-integration"))]
pub trait Instrument: Sized {
/// Instruments this future with a span (no-op when disabled).
#[must_use]
fn instrument(self, _span: NoopSpan) -> Self {
self
}
/// Instruments this future with the current span (no-op when disabled).
#[must_use]
fn in_current_span(self) -> Self {
self
}
}
#[cfg(not(feature = "tracing-integration"))]
impl<T> Instrument for T {}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::init_test_logging;
fn init_test(test_name: &str) {
init_test_logging();
crate::test_phase!(test_name);
}
#[test]
fn test_noop_macros_compile() {
init_test("test_noop_macros_compile");
// These should all compile and do nothing
trace!("trace message");
debug!("debug message");
info!("info message");
warn!("warn message");
error!("error message");
trace!(field = "value", "trace with field");
debug!(count = 42, "debug with field");
info!(name = "test", "info with field");
crate::test_complete!("test_noop_macros_compile");
}
#[test]
fn test_noop_span_compile() {
init_test("test_noop_span_compile");
let span = span!(Level::INFO, "test_span");
let _guard = span.enter();
let span2 = info_span!("info_span");
let _entered = span2.entered();
let span3 = debug_span!("debug_span", task_id = 42);
span3.record("field", "value");
crate::test_complete!("test_noop_span_compile");
}
#[test]
fn test_noop_level_constants() {
init_test("test_noop_level_constants");
// Verify that Level constants are accessible — tested further in
// `level_equality_and_ordering`.
crate::test_complete!("test_noop_level_constants");
}
#[test]
fn test_noop_span_methods() {
init_test("test_noop_span_methods");
#[cfg(not(feature = "tracing-integration"))]
{
let span = NoopSpan;
let disabled = span.is_disabled();
crate::assert_with_log!(disabled, "noop span should be disabled", true, disabled);
let current = NoopSpan::current();
let none = NoopSpan::none();
let _ = current.or_current();
none.follows_from(&span);
}
crate::test_complete!("test_noop_span_methods");
}
#[test]
fn noop_guard_debug() {
#[cfg(not(feature = "tracing-integration"))]
{
let guard = NoopGuard;
let dbg = format!("{guard:?}");
assert!(dbg.contains("NoopGuard"), "{dbg}");
}
}
#[test]
fn noop_span_debug_clone_copy() {
#[cfg(not(feature = "tracing-integration"))]
{
let span = NoopSpan;
let dbg = format!("{span:?}");
assert!(dbg.contains("NoopSpan"), "{dbg}");
let copied = span;
let cloned = span;
assert!(copied.is_disabled());
assert!(cloned.is_disabled());
}
}
#[test]
fn level_equality_and_ordering() {
#[cfg(not(feature = "tracing-integration"))]
{
// All noop levels are the same unit struct
assert_eq!(Level::TRACE, Level::DEBUG);
assert_eq!(Level::INFO, Level::WARN);
assert_eq!(Level::WARN, Level::ERROR);
// Ordering is consistent
assert!(Level::TRACE <= Level::ERROR);
// Debug
let dbg = format!("{:?}", Level::INFO);
assert!(dbg.contains("Level"), "{dbg}");
// Clone/Copy
let l = Level::INFO;
let copied = l;
let cloned = l;
assert_eq!(copied, cloned);
}
}
#[test]
fn span_type_alias_works() {
#[cfg(not(feature = "tracing-integration"))]
{
let span: Span = NoopSpan::current();
let _guard = span.enter();
}
}
#[test]
fn all_span_macros_compile() {
#[cfg(not(feature = "tracing-integration"))]
{
let _ = trace_span!("t");
let _ = debug_span!("d");
let _ = info_span!("i");
let _ = warn_span!("w");
let _ = error_span!("e");
}
}
#[test]
fn instrument_trait_noop() {
#[cfg(not(feature = "tracing-integration"))]
{
let fut = async { 42 };
let instrumented = fut.instrument(NoopSpan);
// Can also use in_current_span
drop(async { 1 }.in_current_span());
drop(instrumented);
}
}
}