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
//! Logger trait and global logger storage.
//!
//! Linux equivalent: `struct console` and `register_console()` in `kernel/printk/`
//!
//! This module defines the `Logger` trait that drivers implement to provide
//! actual log output. The kernel only defines the interface (mechanism),
//! while drivers implement the policy (where and how to log).
use ;
use ;
/// Logger trait - the kernel mechanism for logging.
///
/// Drivers implement this trait to provide actual log output. The kernel
/// only defines the interface, not the policy (where logs go, formatting, etc.).
///
/// # Thread Safety
///
/// Implementations must be `Send + Sync` as the logger is accessed from
/// multiple threads concurrently. Implementations should be lock-free
/// or use minimal locking to avoid blocking the caller.
///
/// # Example
///
/// ```
/// use reovim_kernel::api::v1::*;
///
/// struct StderrLogger;
///
/// impl Logger for StderrLogger {
/// fn log(&self, record: &Record) {
/// eprintln!("[{}] {}: {}",
/// record.level(),
/// record.file(),
/// record.message());
/// }
///
/// fn flush(&self) {
/// // stderr is unbuffered by default
/// }
///
/// fn enabled(&self, level: Level) -> bool {
/// level <= Level::Debug // Log everything except Trace
/// }
/// }
/// ```
/// No-op logger used when no logger is set.
///
/// All operations are no-ops. `enabled()` returns `false` for all levels,
/// causing the logging macros to skip message formatting entirely.
///
/// This is the default logger if `set_logger()` is never called.
;
/// Error returned when attempting to set the logger more than once.
///
/// The global logger can only be set once. This error is returned
/// if `set_logger()` is called after a logger has already been set.
;
// LLVM coverage artifact: unit struct Display impl closing brace marked DA:0
// despite being exercised by test_set_logger_error_display.
// =============================================================================
// Global Logger State
// =============================================================================
/// Global logger storage.
///
/// Uses `OnceLock` for thread-safe one-time initialization.
/// This is the only global state in the printk module.
static LOGGER: = new;
/// Static no-op logger instance used as default.
static NOP_LOGGER: NopLogger = NopLogger;
/// Sets the global logger.
///
/// This function can only be called once. Subsequent calls will
/// return `Err(SetLoggerError)`.
///
/// # Errors
///
/// Returns `Err(SetLoggerError)` if a logger has already been set.
/// The global logger can only be set once.
///
/// # Thread Safety
///
/// This function is thread-safe. If multiple threads call `set_logger()`
/// concurrently, only one will succeed (and return `Ok`), while the
/// others will return `Err(SetLoggerError)`.
///
/// # Example
///
/// ```
/// use reovim_kernel::api::v1::*;
///
/// struct MyLogger;
///
/// impl Logger for MyLogger {
/// fn log(&self, record: &Record) {
/// eprintln!("{}", record.message());
/// }
/// fn flush(&self) {}
/// fn enabled(&self, _level: Level) -> bool { true }
/// }
///
/// static MY_LOGGER: MyLogger = MyLogger;
///
/// // First call succeeds
/// // Note: This would succeed, but we can't actually run it in doctests
/// // because other tests might have already set the logger.
/// // assert!(set_logger(&MY_LOGGER).is_ok());
/// ```
/// Returns the global logger.
///
/// If no logger has been set via `set_logger()`, returns the no-op logger
/// which silently discards all log messages.
///
/// # Example
///
/// ```
/// use reovim_kernel::api::v1::*;
///
/// // Before set_logger() is called, returns NopLogger
/// assert!(!logger().enabled(Level::Error));
/// ```
/// Internal helper for logging macros.
///
/// This function is called by the `pr_*` macros after the level check passes.
/// It formats the message and passes it to the logger.
///
/// # Note
///
/// This function is marked `#[doc(hidden)]` because it's an implementation
/// detail of the logging macros. Users should use the macros instead.
/// Flushes the global logger.
///
/// Ensures all buffered log messages are written out. Useful before
/// program exit or when immediate output is required.