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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Global logger management for the logwise logging system.
//!
//! This module provides thread-safe management of global loggers that receive all log records
//! generated throughout the application. The global logger system supports multiple simultaneous
//! loggers, allowing logs to be sent to multiple destinations (e.g., stderr, files, remote servers).
//!
//! # Architecture
//!
//! The global logger system uses a spinlock-protected vector of `Arc<dyn Logger>` instances.
//! This design ensures:
//! - Thread-safe access from any thread
//! - Multiple loggers can be active simultaneously
//! - Loggers remain alive during logging operations
//! - Compatible with WASM environments where traditional mutexes may not work
//!
//! # Default Behavior
//!
//! By default, the system initializes with a single stderr logger that writes colored output
//! to stderr. This ensures logging works out-of-the-box without configuration.
//!
//! # Thread Safety
//!
//! All functions in this module are thread-safe and can be called from any thread. The underlying
//! spinlock ensures atomic operations while keeping lock hold times minimal. The spinlock is
//! particularly important for WASM compatibility where blocking mutexes may not be available.
//!
//! # Examples
//!
//! ## Using the default logger
//!
//! ```
//! use logwise::global_logger::global_loggers;
//!
//! // Get the current loggers (initializes with StdErrorLogger if needed)
//! let loggers = global_loggers();
//! assert!(!loggers.is_empty());
//! ```
//!
//! ## Adding a custom logger
//!
//! ```
//! logwise::declare_logging_domain!();
//! # fn main() {
//! use logwise::global_logger::add_global_logger;
//! use logwise::InMemoryLogger;
//! use std::sync::Arc;
//!
//! // Add an in-memory logger alongside existing loggers
//! let logger = Arc::new(InMemoryLogger::new());
//! add_global_logger(logger.clone());
//!
//! // Now logs go to both stderr and the in-memory logger
//! logwise::info_sync!("This goes to multiple loggers");
//! # }
//! ```
//!
//! ## Replacing all loggers
//!
//! ```
//! logwise::declare_logging_domain!();
//! # fn main() {
//! use logwise::global_logger::set_global_loggers;
//! use logwise::InMemoryLogger;
//! use std::sync::Arc;
//!
//! // Replace all loggers with just an in-memory logger
//! let logger = Arc::new(InMemoryLogger::new());
//! set_global_loggers(vec![logger.clone()]);
//!
//! // Now logs only go to the in-memory logger
//! logwise::warn_sync!("Only captured in memory");
//! # }
//! ```
//!
//! # Implementation Notes
//!
//! ## Spinlock vs Mutex
//!
//! This module uses a custom spinlock implementation rather than `std::sync::Mutex` for
//! compatibility with WASM environments where blocking mutexes may not be available. The
//! spinlock ensures very short critical sections - only cloning Arc references or updating
//! the logger vector.
//!
//! ## Logger Lifecycle
//!
//! Loggers are reference-counted using `Arc`. When a logger is removed (via `set_global_loggers`),
//! it continues to exist until all outstanding references are dropped. This ensures that
//! in-flight logging operations complete successfully even if the logger configuration changes.
//!
//! ## Performance Considerations
//!
//! - Getting loggers clones the `Arc` vector, which is cheap (only reference count increments)
//! - Adding loggers requires a write lock but is typically infrequent
//! - The spinlock may cause CPU usage spikes under high contention, but this is rare in practice
//! since logger configuration typically happens during initialization
//!
//! ## Best Practices
//!
//! 1. Configure loggers early in your application's lifecycle
//! 2. Avoid frequently changing logger configuration in production
//! 3. Use `add_global_logger` to add supplementary loggers without disrupting existing ones
//! 4. Use `set_global_loggers` when you need complete control over the logging pipeline
//! 5. Always keep at least one logger active to avoid losing important diagnostic information
use crateLogger;
use crateStdErrorLogger;
use Spinlock;
use ;
/// Static storage for the global logger collection.
///
/// Uses `OnceLock` for one-time initialization and `Spinlock` for thread-safe access.
/// The spinlock is necessary for WASM compatibility where traditional mutexes may block.
static GLOBAL_LOGGERS_PTR: = new;
/// Retrieves the current set of global loggers.
///
/// Returns a vector of `Arc<dyn Logger>` references to ensure loggers remain alive
/// during logging operations. If no loggers have been configured, automatically
/// initializes with a default stderr logger.
///
/// This function is thread-safe and can be called from any thread.
///
/// # Returns
///
/// A vector containing `Arc` references to all currently active global loggers.
/// The vector is never empty - it always contains at least the default logger.
///
/// # Performance
///
/// This function clones the vector of `Arc`s, which is relatively cheap since
/// `Arc::clone` only increments a reference count. The spinlock is held only
/// for the duration of the clone operation.
///
/// # Examples
///
/// ```
/// use logwise::global_logger::global_loggers;
///
/// let loggers = global_loggers();
/// println!("Number of active loggers: {}", loggers.len());
///
/// // Loggers can be inspected if needed
/// for logger in &loggers {
/// println!("Logger: {:?}", logger);
/// }
/// ```
/// Adds a logger to the global logger collection.
///
/// The new logger is appended to the existing list of loggers, allowing multiple
/// loggers to receive all log records. This is useful for sending logs to multiple
/// destinations simultaneously.
///
/// This function is thread-safe and can be called from any thread.
///
/// # Arguments
///
/// * `logger` - An `Arc`-wrapped logger implementation to add to the global collection
///
/// # Thread Safety
///
/// The function uses a spinlock to ensure thread-safe modification of the logger list.
/// The lock is held only for the duration of the push operation.
///
/// # Examples
///
/// ```
/// use logwise::global_logger::{add_global_logger, global_loggers};
/// use logwise::InMemoryLogger;
/// use std::sync::Arc;
///
/// let initial_count = global_loggers().len();
///
/// // Add a new logger
/// let logger = Arc::new(InMemoryLogger::new());
/// add_global_logger(logger);
///
/// // Verify it was added
/// assert_eq!(global_loggers().len(), initial_count + 1);
/// ```
///
/// ## Multiple logger types
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::global_logger::add_global_logger;
/// use logwise::InMemoryLogger;
/// use std::sync::Arc;
///
/// // Add multiple in-memory loggers (for demonstration)
/// let logger1 = Arc::new(InMemoryLogger::new());
/// let logger2 = Arc::new(InMemoryLogger::new());
/// add_global_logger(logger1.clone());
/// add_global_logger(logger2.clone());
///
/// // Now logs go to all registered loggers
/// logwise::info_sync!("This appears in all loggers");
/// # }
/// ```
/// Replaces all global loggers with a new set.
///
/// This function completely replaces the existing logger collection. Previous loggers
/// are properly dropped when they are no longer referenced. This is useful when you
/// want complete control over where logs are sent.
///
/// This function is thread-safe and can be called from any thread.
///
/// # Arguments
///
/// * `new_loggers` - A vector of `Arc`-wrapped logger implementations to use as the new global collection
///
/// # Thread Safety
///
/// The function uses a spinlock to ensure thread-safe replacement of the logger list.
/// The lock is held only for the duration of the assignment operation. Previous loggers
/// are dropped after the lock is released when their reference counts reach zero.
///
/// # Examples
///
/// ## Replace with a single logger
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::global_logger::set_global_loggers;
/// use logwise::InMemoryLogger;
/// use std::sync::Arc;
///
/// // Replace all loggers with just one
/// let logger = Arc::new(InMemoryLogger::new());
/// set_global_loggers(vec![logger.clone()]);
///
/// // Now only the in-memory logger receives logs
/// logwise::info_sync!("Only in memory");
/// # }
/// ```
///
/// ## Replace with multiple loggers
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::global_logger::set_global_loggers;
/// use logwise::InMemoryLogger;
/// use std::sync::Arc;
///
/// // Set up multiple loggers at once
/// let logger1 = Arc::new(InMemoryLogger::new());
/// let logger2 = Arc::new(InMemoryLogger::new());
/// let loggers: Vec<Arc<dyn logwise::Logger>> = vec![
/// logger1.clone() as Arc<dyn logwise::Logger>,
/// logger2.clone() as Arc<dyn logwise::Logger>,
/// ];
/// set_global_loggers(loggers);
///
/// logwise::warn_sync!("This goes to both loggers");
/// # }
/// ```
///
/// ## Clear all loggers (not recommended)
///
/// ```
/// use logwise::global_logger::set_global_loggers;
///
/// // This removes all loggers - logs will be silently dropped
/// // Generally not recommended in production code
/// set_global_loggers(vec![]);
/// ```