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