abyss/
kprint.rs

1//! Kernel print utilities.
2
3use crate::dev::x86_64::serial::Com1Sink;
4use crate::spinlock::SpinLock;
5use core::fmt::Write;
6
7// Only mutated when force unlocking is required (i.e. panicking)
8static mut SERIAL: SpinLock<Com1Sink> = SpinLock::new(Com1Sink::new());
9
10#[doc(hidden)]
11#[unsafe(no_mangle)]
12/// Safety: Serial only mutated when force unlocking is required (i.e.
13/// panicking)
14pub fn _print(fmt: core::fmt::Arguments<'_>) {
15    let mut guard = unsafe { SERIAL.lock() };
16    let _ = write!(&mut *guard, "{fmt}");
17    guard.unlock();
18}
19
20/// Force Unlocking Serial.
21///
22/// Do NOT use this API.
23#[doc(hidden)]
24pub unsafe fn force_unlock_serial() {
25    unsafe {
26        SERIAL = SpinLock::new(Com1Sink::new());
27    }
28}
29
30/// Prints out the message.
31///
32/// Use the format! syntax to write data to the standard output.
33/// This first holds the lock for console device.
34#[macro_export]
35macro_rules! print {
36    ($($arg:tt)*) => ($crate::kprint::_print(format_args!($($arg)*)));
37}
38
39/// Prints out the message with a newline.
40///
41/// Use the format! syntax to write data to the standard output.
42/// This first holds the lock for console device.
43#[macro_export]
44macro_rules! println {
45    () => ($crate::print!("\n"));
46    ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
47}
48
49/// Display an information message.
50///
51/// Use the format! syntax to write data to the standard output.
52/// This first holds the lock for console device.
53#[macro_export]
54macro_rules! info {
55    () => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[INFO]\n") });
56    ($($arg:tt)*) => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[INFO] {}\n", format_args!($($arg)*)) });
57}
58
59/// Display a warning message.
60///
61/// Use the format! syntax to write data to the standard output.
62/// This first holds the lock for console device.
63#[macro_export]
64macro_rules! warning {
65    () => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[WARN]\n") });
66    ($($arg:tt)*) => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[WARN] {}\n", format_args!($($arg)*)) });
67}
68
69/// Display a debug message.
70///
71/// Use the format! syntax to write data to the standard output.
72/// This first holds the lock for console device.
73#[macro_export]
74macro_rules! debug {
75    () => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[DEBUG]\n") });
76    ($($arg:tt)*) => (if !$crate::QUITE.load(core::sync::atomic::Ordering::SeqCst) { $crate::print!("[DEBUG] {}\n", format_args!($($arg)*))} );
77}