Expand description
Kernel logging API.
This module provides the logging facade for OSTD and all OSTD-based crates.
It uses eight log levels matching the severity levels described in syslog(2).
§Setup: defining __log_prefix
Every crate that uses the logging macros
must define a __log_prefix macro at its crate root (lib.rs),
before any mod declarations.
This prefix is prepended to every log message from the crate.
// Set this crate's log prefix for `ostd::log`.
macro_rules! __log_prefix {
() => {
"virtio: "
};
}
mod device; // all modules inherit the "virtio: " prefixIt is recommended is to follow Linux’s convention for log prefixes,
which uses the lowercase module name, followed by : .
For example: "virtio: ", "pci: ", "uart: ".
§Quick start
After defining __log_prefix, import the macros and use them:
use ostd::prelude::*;
info!("boot complete");
warn!("feature X is not supported");§Log levels
Eight severity levels are provided, matching syslog(2):
| Level | Value | Meaning |
|---|---|---|
| Emerg | 0 | System is unusable |
| Alert | 1 | Action must be taken |
| Crit | 2 | Critical conditions |
| Error | 3 | Error conditions |
| Warning | 4 | Warning conditions |
| Notice | 5 | Normal but significant |
| Info | 6 | Informational |
| Debug | 7 | Debug-level messages |
use ostd::prelude::*;
emerg!("system is going down");
alert!("action required immediately");
crit!("critical failure in subsystem");
error!("operation failed: {}", err);
warn!("deprecated feature used");
notice!("configuration change applied");
info!("boot complete");
debug!("variable x = {:?}", x);§log crate bridge
A bridge forwards messages from third-party crates
that use the log crate (e.g., smoltcp)
to the OSTD logger.
First-party code should use OSTD’s macros directly.
§Per-module prefix overrides
A subsystem module can override the crate-level prefix
by defining its own __log_prefix at the top of its mod.rs,
before any mod child; declarations.
Child modules inherit the override via textual scoping:
// Set this module's log prefix for `ostd::log`.
macro_rules! __log_prefix {
() => {
"iommu: "
};
}
mod fault; // inherits "iommu: " prefix
mod registers; // inherits "iommu: " prefix§Limitations
§No attributes on __log_prefix definitions
Do not put #[rustfmt::skip] or any other attribute
on __log_prefix definitions.
Rust treats attributed macro_rules! items as “macro-expanded,”
which triggers E0659 ambiguity with definitions at other scopes.
See the design doc in log/macros.rs for the full explanation.
§Backend
An OSTD-based kernel can register a custom Log implementation via inject_logger.
Before a backend is registered, messages are printed through the early-boot console.
Structs§
- Record
- A single log record carrying level, message, and source location.
Enums§
- Level
- Kernel log level, matching the severity levels described in
syslog(2). - Level
Filter - A filter for log levels.
Constants§
- STATIC_
MAX_ LEVEL - Compile-time maximum log level.
Traits§
- Log
- The logger backend trait.
Functions§
- inject_
logger - Registers the global logger backend.
- max_
level - Returns the current runtime maximum log level.
- set_
max_ level - Sets the runtime maximum log level.