Module log

Module log 

Source
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: " prefix

It 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):

LevelValueMeaning
Emerg0System is unusable
Alert1Action must be taken
Crit2Critical conditions
Error3Error conditions
Warning4Warning conditions
Notice5Normal but significant
Info6Informational
Debug7Debug-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).
LevelFilter
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.