zephyr/logging/
impl_printk.rs

1//! Logging through printk
2//!
3//! This module implements a log handler (for the [`log`] crate) that logs messages through Zephyr's
4//! printk mechanism.
5//!
6//! Currently, filtering is global, and set to Info.
7
8use log::{Log, Metadata, Record, SetLoggerError};
9
10use crate::printkln;
11
12/// A simple log handler, built around printk.
13struct PrintkLogger;
14
15impl Log for PrintkLogger {
16    // For now, everything is just available.
17    fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
18        true
19    }
20
21    // Print out the log message, using printkln.
22    //
23    // Normal caveats behind printkln apply, if `RUST_PRINTK_SYNC` is not defined, then all message
24    // printing will be racy.  Otherwise, the message will be broken into small chunks that are each
25    // printed atomically.
26    fn log(&self, record: &Record<'_>) {
27        printkln!("{}:{}: {}", record.level(), record.target(), record.args());
28    }
29
30    // Flush is not needed.
31    fn flush(&self) {}
32}
33
34static PRINTK_LOGGER: PrintkLogger = PrintkLogger;
35
36/// Set the log handler to log messages through printk in Zephyr.
37///
38/// # Safety
39///
40/// This is unsafe due to racy issues in the log framework on targets that do not support atomic
41/// pointers.  As long as this is called ever by a single thread, it is safe to use.
42pub unsafe fn set_logger() -> Result<(), SetLoggerError> {
43    super::set_logger_internal(&PRINTK_LOGGER)
44}