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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Kernel print utilities.

use crate::dev::x86_64::serial::Serial;
use crate::spin_lock::SpinLock;
use core::fmt::Write;

static SERIAL: SpinLock<Serial> = SpinLock::new(Serial::new());

#[doc(hidden)]
#[no_mangle]
pub fn _print(fmt: core::fmt::Arguments<'_>) {
    let _ = write!(&mut *SERIAL.lock(), "{}", fmt);
}

/// Prints out the message.
///
/// Use the format! syntax to write data to the standard output.
/// This first holds the lock for console device.
#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => ($crate::kprint::_print(format_args!($($arg)*)));
}

/// Prints out the message with a newline.
///
/// Use the format! syntax to write data to the standard output.
/// This first holds the lock for console device.
#[macro_export]
macro_rules! println {
    () => ($crate::print!("\n"));
    ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}

/// Display an information message.
///
/// Use the format! syntax to write data to the standard output.
/// This first holds the lock for console device.
#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => ($crate::kprint::_print(
            format_args!(
                "[INFO] {}\n",
                format_args!($($arg)*)
            )
        )
    );
}

/// Display a warning message.
///
/// Use the format! syntax to write data to the standard output.
/// This first holds the lock for console device.
#[macro_export]
macro_rules! warning {
    ($($arg:tt)*) => ($crate::kprint::_print(
            format_args!(
                "[WARNING] {}\n",
                format_args!($($arg)*)
            )
        )
    );
}

/// Print msg if debug build
#[macro_export]
macro_rules! debug {
    ($($e:tt)*) => {
        if cfg!(debug_assertions) {
            $crate::kprint::_print(
                format_args!(
                    "[DEBUG] {}\n",
                    format_args!($($arg)*)
                )
            )
        }
    }
}