xref: /DragonOS/kernel/src/libs/printk.rs (revision c757940bd61b0125e037a59eb77565e42470201b)
1 use core::fmt::{self, Write};
2 
3 use super::lib_ui::textui::{textui_putchar, FontColor};
4 
5 #[macro_export]
6 macro_rules! print {
7     ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*)));
8 }
9 
10 #[macro_export]
11 macro_rules! println {
12     () => {
13         $crate::print!("\n");
14     };
15     ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
16 }
17 
18 /// 指定颜色,彩色输出
19 /// @param FRcolor 前景色
20 /// @param BKcolor 背景色
21 #[macro_export]
22 macro_rules! printk_color {
23 
24     ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
25         use alloc;
26         $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
27     };
28 }
29 
30 #[macro_export]
31 macro_rules! kdebug {
32     ($($arg:tt)*) => {
33         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t{}\n", file!(), line!(),format_args!($($arg)*)))
34 
35     }
36 }
37 
38 #[macro_export]
39 macro_rules! kinfo {
40     ($($arg:tt)*) => {
41         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t{}\n", file!(), line!(),format_args!($($arg)*)))
42     }
43 }
44 
45 #[macro_export]
46 macro_rules! kwarn {
47     ($($arg:tt)*) => {
48         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::YELLOW, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ WARN ] ");
49         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t{}\n", file!(), line!(),format_args!($($arg)*)));
50     }
51 }
52 
53 #[macro_export]
54 macro_rules! kerror {
55     ($($arg:tt)*) => {
56         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ ERROR ] ");
57         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t{}\n", file!(), line!(),format_args!($($arg)*)));
58     }
59 }
60 
61 #[macro_export]
62 macro_rules! kBUG {
63     ($($arg:tt)*) => {
64         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ BUG ] ");
65         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t{}\n", file!(), line!(),format_args!($($arg)*)));
66     }
67 }
68 
69 pub struct PrintkWriter;
70 
71 impl PrintkWriter {
72     #[inline]
73     pub fn __write_fmt(&mut self, args: fmt::Arguments) {
74         self.write_fmt(args).ok();
75     }
76 
77     /// 并输出白底黑字
78     /// @param str: 要写入的字符
79     pub fn __write_string(&mut self, s: &str) {
80         for c in s.chars() {
81             textui_putchar(c, FontColor::WHITE, FontColor::BLACK).ok();
82         }
83     }
84 
85     pub fn __write_string_color(&self, fr_color: FontColor, bk_color: FontColor, s: &str) {
86         for c in s.chars() {
87             textui_putchar(c, fr_color, bk_color).ok();
88         }
89     }
90 }
91 
92 /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
93 impl fmt::Write for PrintkWriter {
94     fn write_str(&mut self, s: &str) -> fmt::Result {
95         self.__write_string(s);
96         Ok(())
97     }
98 }
99 
100 #[doc(hidden)]
101 pub fn __printk(args: fmt::Arguments) {
102     PrintkWriter.write_fmt(args).unwrap();
103 }
104