kernel/library/std/print.rs
1//! # Print Macros and UART Handling
2//!
3//! This module provides functionality for formatted printing through a UART device.
4//! It defines the core printing macros (`print!` and `println!`) used throughout the kernel,
5//! along with the necessary infrastructure to handle UART output.
6//!
7//! ## Examples
8//!
9//! ```
10//! println!("Hello, world!");
11//! println!("Value: {}", 42);
12//! print!("No newline here");
13//! ```
14//!
15//! ## Implementation Details
16//!
17//! The module initializes a UART writer lazily when first used and provides the
18//! core implementation of the `Write` trait for the UART device. It automatically
19//! handles CR+LF conversion for newlines.
20
21/// Implements core printing functionality by writing formatted text to the UART.
22/// This function is called by the `print!` macro and handles lazy initialization
23/// of the UART writer if it doesn't exist.
24///
25/// # Arguments
26///
27/// * `args` - Formatted arguments to print
28///
29/// # Note
30///
31/// This function is not meant to be called directly. Use the `print!` or
32/// `println!` macros instead.
33
34/// Wraps a UART device to implement the `core::fmt::Write` trait.
35///
36/// This allows the UART to be used with the standard formatting macros.
37use core::fmt;
38
39use crate::device::manager::DeviceManager;
40use crate::early_println;
41
42#[macro_export]
43macro_rules! print {
44 ($($arg:tt)*) => ($crate::library::std::print::_print(format_args!($($arg)*)));
45}
46
47#[macro_export]
48macro_rules! println {
49 ($fmt:expr) => ($crate::print!(concat!($fmt, "\n")));
50 ($fmt:expr, $($arg:tt)*) => ($crate::print!(concat!($fmt, "\n"), $($arg)*));
51}
52
53pub fn _print(args: fmt::Arguments) {
54 let manager = DeviceManager::get_mut_manager();
55 let serial = manager.basic.borrow_mut_serial(0);
56 match serial {
57 Some(serial) => {
58 serial.write_fmt(args).unwrap();
59 }
60 None => {
61 early_println!("[print] No serial device found!");
62 }
63 }
64}