152da9a59SGnoCiYeH use core::{ 252da9a59SGnoCiYeH fmt::{self, Write}, 352da9a59SGnoCiYeH sync::atomic::Ordering, 452da9a59SGnoCiYeH }; 56cb769c4Slogin 68d72b68dSJomo use alloc::string::ToString; 78d72b68dSJomo 8fbe6becdSLoGin use super::lib_ui::textui::{textui_putstr, FontColor}; 96cb769c4Slogin 108d72b68dSJomo use crate::{ 11f3b05a97SGnoCiYeH driver::tty::{ 1252da9a59SGnoCiYeH tty_driver::TtyOperation, tty_port::TTY_PORTS, 1352da9a59SGnoCiYeH virtual_terminal::virtual_console::CURRENT_VCNUM, 1452da9a59SGnoCiYeH }, 158d72b68dSJomo filesystem::procfs::{ 168d72b68dSJomo kmsg::KMSG, 178d72b68dSJomo log::{LogLevel, LogMessage}, 188d72b68dSJomo }, 198d72b68dSJomo time::TimeSpec, 208d72b68dSJomo }; 218d72b68dSJomo 226cb769c4Slogin #[macro_export] 236cb769c4Slogin macro_rules! print { 246cb769c4Slogin ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*))); 256cb769c4Slogin } 266cb769c4Slogin 276cb769c4Slogin #[macro_export] 286cb769c4Slogin macro_rules! println { 296cb769c4Slogin () => { 306cb769c4Slogin $crate::print!("\n"); 316cb769c4Slogin }; 326cb769c4Slogin ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); 336cb769c4Slogin } 346cb769c4Slogin 356cb769c4Slogin #[macro_export] 366cb769c4Slogin macro_rules! kdebug { 376cb769c4Slogin ($($arg:tt)*) => { 388d72b68dSJomo $crate::libs::printk::Logger.log(7,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 3940fe15e0SLoGin $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))) 406cb769c4Slogin } 416cb769c4Slogin } 426cb769c4Slogin 436cb769c4Slogin #[macro_export] 446cb769c4Slogin macro_rules! kinfo { 456cb769c4Slogin ($($arg:tt)*) => { 468d72b68dSJomo $crate::libs::printk::Logger.log(6,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 4740fe15e0SLoGin $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))) 486cb769c4Slogin } 496cb769c4Slogin } 506cb769c4Slogin 516cb769c4Slogin #[macro_export] 526cb769c4Slogin macro_rules! kwarn { 536cb769c4Slogin ($($arg:tt)*) => { 548d72b68dSJomo $crate::libs::printk::Logger.log(4,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 5552da9a59SGnoCiYeH $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[1;33m[ WARN ] \x1B[0m")); 5640fe15e0SLoGin $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 576cb769c4Slogin } 586cb769c4Slogin } 596cb769c4Slogin 606cb769c4Slogin #[macro_export] 616cb769c4Slogin macro_rules! kerror { 626cb769c4Slogin ($($arg:tt)*) => { 638d72b68dSJomo $crate::libs::printk::Logger.log(3,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 6452da9a59SGnoCiYeH $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ ERROR ] \x1B[0m")); 6540fe15e0SLoGin $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 666cb769c4Slogin } 676cb769c4Slogin } 686cb769c4Slogin 696cb769c4Slogin #[macro_export] 706cb769c4Slogin macro_rules! kBUG { 716cb769c4Slogin ($($arg:tt)*) => { 728d72b68dSJomo $crate::libs::printk::Logger.log(1,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 7352da9a59SGnoCiYeH $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ BUG ] \x1B[0m")); 7440fe15e0SLoGin $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*))); 756cb769c4Slogin } 766cb769c4Slogin } 776cb769c4Slogin 78fb6c29d0Slogin pub struct PrintkWriter; 79fb6c29d0Slogin 80fb6c29d0Slogin impl PrintkWriter { 8140fe15e0SLoGin #[inline] 8240fe15e0SLoGin pub fn __write_fmt(&mut self, args: fmt::Arguments) { 83abe3a6eaShanjiezhou self.write_fmt(args).ok(); 8440fe15e0SLoGin } 8540fe15e0SLoGin 86abe3a6eaShanjiezhou /// 并输出白底黑字 87fb6c29d0Slogin /// @param str: 要写入的字符 88fb6c29d0Slogin pub fn __write_string(&mut self, s: &str) { 8952da9a59SGnoCiYeH let current_vcnum = CURRENT_VCNUM.load(Ordering::SeqCst); 9052da9a59SGnoCiYeH if current_vcnum != -1 { 9152da9a59SGnoCiYeH // tty已经初始化了之后才输出到屏幕 9252da9a59SGnoCiYeH let port = TTY_PORTS[current_vcnum as usize].clone(); 9352da9a59SGnoCiYeH let tty = port.port_data().tty(); 94*b5b571e0SLoGin if let Some(tty) = tty { 9552da9a59SGnoCiYeH let _ = tty.write(tty.core(), s.as_bytes(), s.len()); 9652da9a59SGnoCiYeH } else { 9752da9a59SGnoCiYeH let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK); 98fb6c29d0Slogin } 9952da9a59SGnoCiYeH } else { 10052da9a59SGnoCiYeH let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK); 10152da9a59SGnoCiYeH } 10240fe15e0SLoGin } 103fb6c29d0Slogin } 104fb6c29d0Slogin 105fb6c29d0Slogin /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出 106fb6c29d0Slogin impl fmt::Write for PrintkWriter { 107fb6c29d0Slogin fn write_str(&mut self, s: &str) -> fmt::Result { 108fb6c29d0Slogin self.__write_string(s); 109fb6c29d0Slogin Ok(()) 110fb6c29d0Slogin } 111fb6c29d0Slogin } 112fb6c29d0Slogin 113fb6c29d0Slogin #[doc(hidden)] 114fb6c29d0Slogin pub fn __printk(args: fmt::Arguments) { 115fb6c29d0Slogin PrintkWriter.write_fmt(args).unwrap(); 116fb6c29d0Slogin } 1178d72b68dSJomo 1188d72b68dSJomo pub struct Logger; 1198d72b68dSJomo 1208d72b68dSJomo impl Logger { 1218d72b68dSJomo pub fn log(&self, log_level: usize, message: fmt::Arguments) { 122453452ccSLoGin if unsafe { KMSG.is_some() } { 1238d72b68dSJomo let timestamp: TimeSpec = TimeSpec::now(); 124*b5b571e0SLoGin let log_level = LogLevel::from(log_level); 125453452ccSLoGin 1268d72b68dSJomo let log_message = LogMessage::new(timestamp, log_level, message.to_string()); 1278d72b68dSJomo 1288d72b68dSJomo unsafe { KMSG.as_ref().unwrap().lock_irqsave().push(log_message) }; 1298d72b68dSJomo } 1308d72b68dSJomo } 1318d72b68dSJomo } 132