1use core::fmt;
13use lazy_static::lazy_static;
14use spin::Mutex;
15
16lazy_static! {
17 pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer::new());
19}
20
21pub struct Writer {
23 column_position: usize,
24}
25
26impl Writer {
27 pub const fn new() -> Self {
29 Writer { column_position: 0 }
30 }
31
32 pub fn write_byte(&mut self, byte: u8) {
34 match byte {
35 b'\n' => {
36 self.new_line();
37 }
38 byte => {
39 self.write_to_serial(byte);
41 self.column_position += 1;
42 }
43 }
44 }
45
46 pub fn write_string(&mut self, s: &str) {
48 for byte in s.bytes() {
49 match byte {
50 0x20..=0x7e | b'\n' => self.write_byte(byte),
52 _ => self.write_byte(0xfe),
54 }
55 }
56 }
57
58 fn new_line(&mut self) {
60 self.write_to_serial(b'\n');
61 self.column_position = 0;
62 }
63
64 fn write_to_serial(&mut self, byte: u8) {
66 use crate::serial::SERIAL1;
67 use core::fmt::Write;
68
69 let mut serial = SERIAL1.lock();
71 let _ = serial.write_char(byte as char);
72 }
73}
74
75impl Default for Writer {
76 fn default() -> Self {
77 Self::new()
78 }
79}
80
81impl fmt::Write for Writer {
82 fn write_str(&mut self, s: &str) -> fmt::Result {
83 self.write_string(s);
84 Ok(())
85 }
86}
87
88#[doc(hidden)]
90pub fn _print(args: fmt::Arguments) {
91 use crate::interrupts;
92 use core::fmt::Write;
93
94 interrupts::without_interrupts(|| {
96 WRITER.lock().write_fmt(args).unwrap();
97 });
98}
99
100#[macro_export]
108macro_rules! print {
109 ($($arg:tt)*) => ($crate::console::_print(format_args!($($arg)*)));
110}
111
112#[macro_export]
121macro_rules! println {
122 () => ($crate::print!("\n"));
123 ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
124}