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
// SPDX-License-Identifier: MPL-2.0
//! Logging macros.
//!
//! Contains the core [`log!`](crate::log!) macro and
//! level-specific wrappers (`info!`, `warn!`, etc.).
//! All are `#[macro_export]` so downstream crates can access them
//! via `use ostd::info;` etc.
/// Logs a message at the [`Emerg`] level.
///
/// [`Emerg`]: crate::log::Level::Emerg
/// Logs a message at the [`Alert`] level.
///
/// [`Alert`]: crate::log::Level::Alert
/// Logs a message at the [`Crit`] level.
///
/// [`Crit`]: crate::log::Level::Crit
/// Logs a message at the [`Error`] level.
///
/// [`Error`]: crate::log::Level::Error
/// Logs a message at the [`Warning`] level.
///
/// [`Warning`]: crate::log::Level::Warning
/// Logs a message at the [`Notice`] level.
///
/// [`Notice`]: crate::log::Level::Notice
/// Logs a message at the [`Info`] level.
///
/// [`Info`]: crate::log::Level::Info
/// Logs a message at the [`Debug`] level.
///
/// [`Debug`]: crate::log::Level::Debug
/// Returns `true` if a message at the given level would be logged.
///
/// Checks both the compile-time [`STATIC_MAX_LEVEL`] and the runtime
/// [`max_level`] filters.
///
/// [`STATIC_MAX_LEVEL`]: crate::log::STATIC_MAX_LEVEL
/// [`max_level`]: crate::log::max_level
/// Logs a message at the given level.
///
/// This is the core logging macro.
/// All level-specific macros delegate to it.
///
/// The macro references a bare `__log_prefix!()` which resolves at
/// the call site via Rust's textual macro scoping.
/// The prefix is passed as a separate field on the [`Record`]
/// rather than concatenated into the format string,
/// so implicit format captures (`info!("{var}")`) work normally.
///
/// [`Record`]: crate::log::Record
///
/// # Examples
///
/// ```rust,ignore
/// use ostd::log::Level;
/// ostd::log!(Level::Warning, "unsupported");
/// ostd::log!(Level::Info, "value = {}", x);
/// ostd::log!(Level::Debug, "{name} started");
/// ```
/// # How the `__log_prefix` mechanism works
///
/// The per-module prefix mechanism relies on Rust's textual
/// `macro_rules!` scoping rules. This document explains these rules,
/// the key constraints on `__log_prefix` definitions,
/// and why certain seemingly-reasonable alternatives don't work.
///
/// ## How the prefix reaches the output
///
/// The `log!` macro passes `__log_prefix!()` as a separate
/// `&'static str` field on [`Record`](crate::log::Record),
/// not as part of the `format_args!()` string.
/// The logger backend prepends it when formatting the message.
/// This avoids using `concat!()` inside `format_args!()`,
/// which would disable Rust's implicit format captures
/// (e.g., `info!("{var}")`).
///
/// ## Textual scoping of `macro_rules!`
///
/// A `macro_rules!` definition is visible to all items that appear
/// **after** it in the same file,
/// including file-backed child modules declared via `mod child;`:
///
/// ```rust,ignore
/// // lib.rs
/// macro_rules! __log_prefix { () => { "" }; }
/// mod sub; // sub.rs can use __log_prefix!()
/// ```
///
/// A `macro_rules!` in an inner scope **shadows** one from an outer
/// scope. This enables per-module overrides:
///
/// ```rust,ignore
/// // lib.rs
/// macro_rules! __log_prefix { () => { "" }; }
/// mod sub;
///
/// // sub/mod.rs
/// macro_rules! __log_prefix { () => { "sub: " }; }
/// mod child; // child.rs sees "sub: ", not ""
/// ```
///
/// ## How `__log_prefix!()` resolves through the macro chain
///
/// The `log!` macro contains a bare `__log_prefix!()` reference
/// (without `$crate::`). Bare names in `macro_rules!` expansions
/// resolve at the **call site**, not the definition site.
/// So when `info!("msg")` expands to
/// `$crate::log!(Level::Info, "msg")`,
/// the `__log_prefix!()` inside `log!` resolves at the site where
/// `info!()` was written — finding whichever `__log_prefix` is in
/// scope there (either the crate-root default or a module override).
///
/// ## Why `__log_prefix` must be at the crate root
///
/// The `__log_prefix` default must be defined at the crate root
/// (`lib.rs`), before any `mod` declarations, so that every module
/// in the crate inherits it via textual scoping.
/// Without it, any `info!()` call would fail with
/// "cannot find macro `__log_prefix`."
///
/// ## Why `__log_prefix` cannot be made more user-friendly
///
/// The raw `macro_rules! __log_prefix { ... }` boilerplate is admittedly
/// ugly. Two natural approaches to beautify it were explored and both
/// fail due to the same underlying Rust limitation.
///
/// **The underlying rule:** Rust's macro name resolution tracks whether
/// a `macro_rules!` item was directly written in source code or produced
/// by some form of macro expansion (including attribute processing).
/// Two definitions of the same name at different scopes are only allowed
/// to shadow each other if they are at the same "expansion level."
/// If one is directly written and the other is "macro-expanded,"
/// Rust reports E0659 (ambiguous).
///
/// ### Attempt 1: keep it one-line with `#[rustfmt::skip]`
///
/// Without `#[rustfmt::skip]`, `rustfmt` expands the definition to
/// multiple lines. It would be nice to write:
///
/// ```rust,ignore
/// #[rustfmt::skip]
/// macro_rules! __log_prefix { () => { "sub: " }; }
/// ```
///
/// But `#[rustfmt::skip]` (or any attribute) on a `macro_rules!` item
/// causes Rust to treat the item as "macro-expanded."
/// When the crate root has a directly-written default and a submodule
/// has an attributed override, the two are at different expansion levels
/// and Rust reports E0659:
///
/// ```rust,ignore
/// // lib.rs — directly written
/// macro_rules! __log_prefix { () => { "" }; }
///
/// // sub/mod.rs — attributed → "macro-expanded" → AMBIGUOUS
/// #[rustfmt::skip]
/// macro_rules! __log_prefix { () => { "sub: " }; }
/// ```
///
/// ### Attempt 2: hide behind a `set_log_prefix!` wrapper macro
///
/// A wrapper like `ostd::set_log_prefix!("iommu")` that expands to
/// `macro_rules! __log_prefix { ... }` would be much more ergonomic.
/// However, the expanded `macro_rules!` item is "macro-expanded"
/// (it was produced by expanding `set_log_prefix!`).
/// Two such definitions at different scopes — one from the crate root's
/// `set_log_prefix!` and one from a module's `set_log_prefix!` — are
/// always ambiguous (E0659), even though both originate from the same
/// wrapper macro.
///
/// ### Conclusion
///
/// The only way to get clean shadowing between scopes is for both the
/// default and the override to be **directly written** `macro_rules!`
/// items with no attributes and no wrapper macros.