xref: /DragonOS/kernel/src/libs/printk.rs (revision f3b05a97ec061e766247b18dc12e2a413b977b14)
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::{
11*f3b05a97SGnoCiYeH     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 /// 指定颜色,彩色输出
366cb769c4Slogin /// @param FRcolor 前景色
376cb769c4Slogin /// @param BKcolor 背景色
386cb769c4Slogin #[macro_export]
396cb769c4Slogin macro_rules! printk_color {
406cb769c4Slogin 
416cb769c4Slogin     ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
426cb769c4Slogin         use alloc;
436cb769c4Slogin         $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
446cb769c4Slogin     };
456cb769c4Slogin }
466cb769c4Slogin 
476cb769c4Slogin #[macro_export]
486cb769c4Slogin macro_rules! kdebug {
496cb769c4Slogin     ($($arg:tt)*) => {
508d72b68dSJomo         $crate::libs::printk::Logger.log(7,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
5140fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
526cb769c4Slogin     }
536cb769c4Slogin }
546cb769c4Slogin 
556cb769c4Slogin #[macro_export]
566cb769c4Slogin macro_rules! kinfo {
576cb769c4Slogin     ($($arg:tt)*) => {
588d72b68dSJomo         $crate::libs::printk::Logger.log(6,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
5940fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
606cb769c4Slogin     }
616cb769c4Slogin }
626cb769c4Slogin 
636cb769c4Slogin #[macro_export]
646cb769c4Slogin macro_rules! kwarn {
656cb769c4Slogin     ($($arg:tt)*) => {
668d72b68dSJomo         $crate::libs::printk::Logger.log(4,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
6752da9a59SGnoCiYeH         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[1;33m[ WARN ] \x1B[0m"));
6840fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
696cb769c4Slogin     }
706cb769c4Slogin }
716cb769c4Slogin 
726cb769c4Slogin #[macro_export]
736cb769c4Slogin macro_rules! kerror {
746cb769c4Slogin     ($($arg:tt)*) => {
758d72b68dSJomo         $crate::libs::printk::Logger.log(3,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
7652da9a59SGnoCiYeH         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ ERROR ] \x1B[0m"));
7740fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
786cb769c4Slogin     }
796cb769c4Slogin }
806cb769c4Slogin 
816cb769c4Slogin #[macro_export]
826cb769c4Slogin macro_rules! kBUG {
836cb769c4Slogin     ($($arg:tt)*) => {
848d72b68dSJomo         $crate::libs::printk::Logger.log(1,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
8552da9a59SGnoCiYeH         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ BUG ] \x1B[0m"));
8640fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
876cb769c4Slogin     }
886cb769c4Slogin }
896cb769c4Slogin 
90fb6c29d0Slogin pub struct PrintkWriter;
91fb6c29d0Slogin 
92fb6c29d0Slogin impl PrintkWriter {
9340fe15e0SLoGin     #[inline]
9440fe15e0SLoGin     pub fn __write_fmt(&mut self, args: fmt::Arguments) {
95abe3a6eaShanjiezhou         self.write_fmt(args).ok();
9640fe15e0SLoGin     }
9740fe15e0SLoGin 
98abe3a6eaShanjiezhou     /// 并输出白底黑字
99fb6c29d0Slogin     /// @param str: 要写入的字符
100fb6c29d0Slogin     pub fn __write_string(&mut self, s: &str) {
10152da9a59SGnoCiYeH         let current_vcnum = CURRENT_VCNUM.load(Ordering::SeqCst);
10252da9a59SGnoCiYeH         if current_vcnum != -1 {
10352da9a59SGnoCiYeH             // tty已经初始化了之后才输出到屏幕
10452da9a59SGnoCiYeH             let port = TTY_PORTS[current_vcnum as usize].clone();
10552da9a59SGnoCiYeH             let tty = port.port_data().tty();
10652da9a59SGnoCiYeH             if tty.is_some() {
10752da9a59SGnoCiYeH                 let tty = tty.unwrap();
10852da9a59SGnoCiYeH                 let _ = tty.write(tty.core(), s.as_bytes(), s.len());
10952da9a59SGnoCiYeH             } else {
11052da9a59SGnoCiYeH                 let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK);
111fb6c29d0Slogin             }
11252da9a59SGnoCiYeH         } else {
11352da9a59SGnoCiYeH             let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK);
11452da9a59SGnoCiYeH         }
11540fe15e0SLoGin     }
116fb6c29d0Slogin }
117fb6c29d0Slogin 
118fb6c29d0Slogin /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
119fb6c29d0Slogin impl fmt::Write for PrintkWriter {
120fb6c29d0Slogin     fn write_str(&mut self, s: &str) -> fmt::Result {
121fb6c29d0Slogin         self.__write_string(s);
122fb6c29d0Slogin         Ok(())
123fb6c29d0Slogin     }
124fb6c29d0Slogin }
125fb6c29d0Slogin 
126fb6c29d0Slogin #[doc(hidden)]
127fb6c29d0Slogin pub fn __printk(args: fmt::Arguments) {
128fb6c29d0Slogin     PrintkWriter.write_fmt(args).unwrap();
129fb6c29d0Slogin }
1308d72b68dSJomo 
1318d72b68dSJomo pub struct Logger;
1328d72b68dSJomo 
1338d72b68dSJomo impl Logger {
1348d72b68dSJomo     pub fn log(&self, log_level: usize, message: fmt::Arguments) {
135453452ccSLoGin         if unsafe { KMSG.is_some() } {
1368d72b68dSJomo             let timestamp: TimeSpec = TimeSpec::now();
1378d72b68dSJomo             let log_level = LogLevel::from(log_level.clone());
138453452ccSLoGin 
1398d72b68dSJomo             let log_message = LogMessage::new(timestamp, log_level, message.to_string());
1408d72b68dSJomo 
1418d72b68dSJomo             unsafe { KMSG.as_ref().unwrap().lock_irqsave().push(log_message) };
1428d72b68dSJomo         }
1438d72b68dSJomo     }
1448d72b68dSJomo }
145