Skip to main content

kernel/kprint/
macros.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Kernel print macros ([`kinfo!`](crate::kinfo), [`kdebug`](crate::kdebug), ...)
3//!
4//! Authors: MarioS271
5
6/// Internal macro to avoid code duplication
7macro_rules! __kprint {
8    ($level:expr, $($arg:tt)*) => {
9        {
10            use $crate::kprint::kernel_writer::KernelWriter;
11            use $crate::kprint::log_entry::LogFlags;
12            use $crate::state::kstate::KSTATE;
13            if KSTATE.kprint.config().max_log_level() >= $level {
14                let mut w = KernelWriter::lock(
15                    $level,
16                    LogFlags::NEWLINE
17                );
18                let _ = core::fmt::write(&mut w, format_args!($($arg)*));
19            }
20        }
21    };
22}
23pub(crate) use __kprint;
24
25/// Log a line at emergency severity (system is unusable).
26#[macro_export]
27macro_rules! kemerg {
28    ($($arg:tt)*) => {
29        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Emergency, $($arg)*);
30    };
31}
32
33/// Log a line at alert severity (action must be taken immediately).
34#[macro_export]
35macro_rules! kalert {
36    ($($arg:tt)*) => {
37        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Alert, $($arg)*);
38    };
39}
40
41/// Log a line at critical severity.
42#[macro_export]
43macro_rules! kcrit {
44    ($($arg:tt)*) => {
45        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Critical, $($arg)*);
46    };
47}
48
49/// Log a line at error severity.
50#[macro_export]
51macro_rules! kerror {
52    ($($arg:tt)*) => {
53        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Error, $($arg)*);
54    };
55}
56
57/// Log a line at warning severity.
58#[macro_export]
59macro_rules! kwarn {
60    ($($arg:tt)*) => {
61        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Warn, $($arg)*);
62    };
63}
64
65/// Log a line at notice severity.
66#[macro_export]
67macro_rules! knotice {
68    ($($arg:tt)*) => {
69        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Notice, $($arg)*);
70    };
71}
72
73/// Log a line at info severity.
74#[macro_export]
75macro_rules! kinfo {
76    ($($arg:tt)*) => {
77        $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Info, $($arg)*);
78    };
79}
80
81/// Log a line at debug severity; compiled out unless the `debug-logging` feature is enabled.
82#[macro_export]
83macro_rules! kdebug {
84    ($($arg:tt)*) => {
85        {
86            #[cfg(feature = "debug-logging")]
87            $crate::kprint::macros::__kprint!($crate::kprint::log_entry::LogLevel::Debug, $($arg)*);
88        };
89    };
90}