xref: /DragonOS/kernel/src/libs/printk.rs (revision 4fda81ce81939d83b74c8042d6fb4223deff3685)
1 use core::fmt::{self, Write};
2 
3 use super::lib_ui::textui::{textui_putstr, 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         textui_putstr(s, FontColor::WHITE, FontColor::BLACK).ok();
81     }
82 
83     pub fn __write_string_color(&self, fr_color: FontColor, bk_color: FontColor, s: &str) {
84         textui_putstr(s, fr_color, bk_color).ok();
85     }
86 }
87 
88 /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
89 impl fmt::Write for PrintkWriter {
90     fn write_str(&mut self, s: &str) -> fmt::Result {
91         self.__write_string(s);
92         Ok(())
93     }
94 }
95 
96 #[doc(hidden)]
97 pub fn __printk(args: fmt::Arguments) {
98     PrintkWriter.write_fmt(args).unwrap();
99 }
100