zephyr/logging/
impl_printk.rs

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
//! Logging through printk
//!
//! This module implements a log handler (for the [`log`] crate) that logs messages through Zephyr's
//! printk mechanism.
//!
//! Currently, filtering is global, and set to Info.

use log::{Log, Metadata, Record, SetLoggerError};

use crate::printkln;

/// A simple log handler, built around printk.
struct PrintkLogger;

impl Log for PrintkLogger {
    // For now, everything is just available.
    fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
        true
    }

    // Print out the log message, using printkln.
    //
    // Normal caveats behind printkln apply, if `RUST_PRINTK_SYNC` is not defined, then all message
    // printing will be racy.  Otherwise, the message will be broken into small chunks that are each
    // printed atomically.
    fn log(&self, record: &Record<'_>) {
        printkln!("{}:{}: {}",
                  record.level(),
                  record.target(),
                  record.args());
    }

    // Flush is not needed.
    fn flush(&self) {
    }
}

static PRINTK_LOGGER: PrintkLogger = PrintkLogger;

/// Set the log handler to log messages through printk in Zephyr.
///
/// This is unsafe due to racy issues in the log framework on targets that do not support atomic
/// pointers.
pub unsafe fn set_logger() -> Result<(), SetLoggerError> {
    super::set_logger_internal(&PRINTK_LOGGER)
}