1 #![allow(unused)]
2 use crate::include::bindings::bindings::{printk_color, BLACK, WHITE};
3 use ::core::ffi::c_char;
4 use alloc::vec::Vec;
5 use core::fmt;
6 
7 // ====== 定义颜色 ======
8 /// 白色
9 pub const COLOR_WHITE: u32 = 0x00ffffff;
10 /// 黑色
11 pub const COLOR_BLACK: u32 = 0x00000000;
12 /// 红色
13 pub const COLOR_RED: u32 = 0x00ff0000;
14 /// 橙色
15 pub const COLOR_ORANGE: u32 = 0x00ff8000;
16 /// 黄色
17 pub const COLOR_YELLOW: u32 = 0x00ffff00;
18 /// 绿色
19 pub const COLOR_GREEN: u32 = 0x0000ff00;
20 /// 蓝色
21 pub const COLOR_BLUE: u32 = 0x000000ff;
22 /// 靛色
23 pub const COLOR_INDIGO: u32 = 0x0000ffff;
24 /// 紫色
25 pub const COLOR_PURPLE: u32 = 0x008000ff;
26 
27 #[macro_export]
28 macro_rules! print {
29     ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*)));
30 }
31 
32 #[macro_export]
33 macro_rules! println {
34     () => {
35         $crate::print!("\n");
36     };
37     ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
38 }
39 
40 /// 指定颜色,彩色输出
41 /// @param FRcolor 前景色
42 /// @param BKcolor 背景色
43 #[macro_export]
44 macro_rules! printk_color {
45 
46     ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
47         use alloc;
48         $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
49     };
50 }
51 
52 #[macro_export]
53 macro_rules! kdebug {
54     ($($arg:tt)*) => {
55         $crate::libs::printk::PrintkWriter.__write_string((alloc::fmt::format(format_args!("[ DEBUG ] ({}:{})\t", file!(), line!()))+
56                                                                 alloc::fmt::format(format_args!($($arg)*)).as_str() + "\n").as_str())
57     }
58 }
59 
60 #[macro_export]
61 macro_rules! kinfo {
62     ($($arg:tt)*) => {
63         $crate::libs::printk::PrintkWriter.__write_string((alloc::string::String::from("[ INFO ] ")+ alloc::fmt::format(format_args!($($arg)*)).as_str() + "\n").as_str())
64     }
65 }
66 
67 #[macro_export]
68 macro_rules! kwarn {
69     ($($arg:tt)*) => {
70         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::printk::COLOR_YELLOW, $crate::libs::printk::COLOR_BLACK, "[ WARN ] ");
71         $crate::libs::printk::PrintkWriter.__write_string((alloc::fmt::format(format_args!($($arg)*)) + "\n").as_str())
72     }
73 }
74 
75 #[macro_export]
76 macro_rules! kerror {
77     ($($arg:tt)*) => {
78         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::printk::COLOR_RED, $crate::libs::printk::COLOR_BLACK, "[ ERROR ] ");
79         $crate::libs::printk::PrintkWriter.__write_string((alloc::fmt::format(format_args!("({}:{})\t", file!(), line!())) +
80                                                                 alloc::fmt::format(format_args!($($arg)*)).as_str() + "\n").as_str())
81     }
82 }
83 
84 #[macro_export]
85 macro_rules! kBUG {
86     ($($arg:tt)*) => {
87         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::printk::COLOR_RED, $crate::libs::printk::COLOR_BLACK, "[ BUG ] ");
88         $crate::libs::printk::PrintkWriter.__write_string((alloc::fmt::format(format_args!("({}:{})\t", file!(), line!())) +
89                                                                 alloc::fmt::format(format_args!($($arg)*)).as_str() + "\n").as_str())
90     }
91 }
92 
93 pub struct PrintkWriter;
94 
95 impl PrintkWriter {
96     /// 调用C语言编写的printk_color,并输出白底黑字(暂时只支持ascii字符)
97     /// @param str: 要写入的字符
__write_string(&mut self, s: &str)98     pub fn __write_string(&mut self, s: &str) {
99         let str_to_print = self.__utf8_to_ascii(s);
100         unsafe {
101             printk_color(WHITE, BLACK, str_to_print.as_ptr() as *const c_char);
102         }
103     }
104 
__write_string_color(&self, fr_color: u32, bk_color: u32, s: &str)105     pub fn __write_string_color(&self, fr_color: u32, bk_color: u32, s: &str) {
106         let str_to_print = self.__utf8_to_ascii(s);
107         unsafe {
108             printk_color(fr_color, bk_color, str_to_print.as_ptr() as *const c_char);
109         }
110     }
111 
112     /// 将s这个utf8字符串,转换为ascii字符串
113     /// @param s 待转换的utf8字符串
114     /// @return Vec<u8> 转换结束后的Ascii字符串
__utf8_to_ascii(&self, s: &str) -> Vec<u8>115     pub fn __utf8_to_ascii(&self, s: &str) -> Vec<u8> {
116         let mut ascii_str: Vec<u8> = Vec::with_capacity(s.len() + 1);
117         for byte in s.bytes() {
118             match byte {
119                 0..=127 => {
120                     ascii_str.push(byte);
121                 }
122                 _ => {}
123             }
124         }
125         ascii_str.push(b'\0');
126         return ascii_str;
127     }
128 }
129 
130 /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
131 impl fmt::Write for PrintkWriter {
write_str(&mut self, s: &str) -> fmt::Result132     fn write_str(&mut self, s: &str) -> fmt::Result {
133         self.__write_string(s);
134         Ok(())
135     }
136 }
137 
138 #[doc(hidden)]
__printk(args: fmt::Arguments)139 pub fn __printk(args: fmt::Arguments) {
140     use fmt::Write;
141     PrintkWriter.write_fmt(args).unwrap();
142 }
143