xref: /DragonOS/kernel/src/libs/printk.rs (revision 453452cc02e2766a28d87dd47bdee37caddc4c44)
1abe3a6eaShanjiezhou use core::fmt::{self, Write};
26cb769c4Slogin 
38d72b68dSJomo use alloc::string::ToString;
48d72b68dSJomo 
5fbe6becdSLoGin use super::lib_ui::textui::{textui_putstr, FontColor};
66cb769c4Slogin 
78d72b68dSJomo use crate::{
88d72b68dSJomo     filesystem::procfs::{
98d72b68dSJomo         kmsg::KMSG,
108d72b68dSJomo         log::{LogLevel, LogMessage},
118d72b68dSJomo     },
128d72b68dSJomo     time::TimeSpec,
138d72b68dSJomo };
148d72b68dSJomo 
156cb769c4Slogin #[macro_export]
166cb769c4Slogin macro_rules! print {
176cb769c4Slogin     ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*)));
186cb769c4Slogin }
196cb769c4Slogin 
206cb769c4Slogin #[macro_export]
216cb769c4Slogin macro_rules! println {
226cb769c4Slogin     () => {
236cb769c4Slogin         $crate::print!("\n");
246cb769c4Slogin     };
256cb769c4Slogin     ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
266cb769c4Slogin }
276cb769c4Slogin 
286cb769c4Slogin /// 指定颜色,彩色输出
296cb769c4Slogin /// @param FRcolor 前景色
306cb769c4Slogin /// @param BKcolor 背景色
316cb769c4Slogin #[macro_export]
326cb769c4Slogin macro_rules! printk_color {
336cb769c4Slogin 
346cb769c4Slogin     ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
356cb769c4Slogin         use alloc;
366cb769c4Slogin         $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
376cb769c4Slogin     };
386cb769c4Slogin }
396cb769c4Slogin 
406cb769c4Slogin #[macro_export]
416cb769c4Slogin macro_rules! kdebug {
426cb769c4Slogin     ($($arg:tt)*) => {
438d72b68dSJomo         $crate::libs::printk::Logger.log(7,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
4440fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
456cb769c4Slogin     }
466cb769c4Slogin }
476cb769c4Slogin 
486cb769c4Slogin #[macro_export]
496cb769c4Slogin macro_rules! kinfo {
506cb769c4Slogin     ($($arg:tt)*) => {
518d72b68dSJomo         $crate::libs::printk::Logger.log(6,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
5240fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
536cb769c4Slogin     }
546cb769c4Slogin }
556cb769c4Slogin 
566cb769c4Slogin #[macro_export]
576cb769c4Slogin macro_rules! kwarn {
586cb769c4Slogin     ($($arg:tt)*) => {
598d72b68dSJomo         $crate::libs::printk::Logger.log(4,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
60abe3a6eaShanjiezhou         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::YELLOW, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ WARN ] ");
6140fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
626cb769c4Slogin     }
636cb769c4Slogin }
646cb769c4Slogin 
656cb769c4Slogin #[macro_export]
666cb769c4Slogin macro_rules! kerror {
676cb769c4Slogin     ($($arg:tt)*) => {
688d72b68dSJomo         $crate::libs::printk::Logger.log(3,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
69abe3a6eaShanjiezhou         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ ERROR ] ");
7040fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
716cb769c4Slogin     }
726cb769c4Slogin }
736cb769c4Slogin 
746cb769c4Slogin #[macro_export]
756cb769c4Slogin macro_rules! kBUG {
766cb769c4Slogin     ($($arg:tt)*) => {
778d72b68dSJomo         $crate::libs::printk::Logger.log(1,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
78abe3a6eaShanjiezhou         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ BUG ] ");
7940fe15e0SLoGin         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
806cb769c4Slogin     }
816cb769c4Slogin }
826cb769c4Slogin 
83fb6c29d0Slogin pub struct PrintkWriter;
84fb6c29d0Slogin 
85fb6c29d0Slogin impl PrintkWriter {
8640fe15e0SLoGin     #[inline]
8740fe15e0SLoGin     pub fn __write_fmt(&mut self, args: fmt::Arguments) {
88abe3a6eaShanjiezhou         self.write_fmt(args).ok();
8940fe15e0SLoGin     }
9040fe15e0SLoGin 
91abe3a6eaShanjiezhou     /// 并输出白底黑字
92fb6c29d0Slogin     /// @param str: 要写入的字符
93fb6c29d0Slogin     pub fn __write_string(&mut self, s: &str) {
94fbe6becdSLoGin         textui_putstr(s, FontColor::WHITE, FontColor::BLACK).ok();
95fb6c29d0Slogin     }
96fb6c29d0Slogin 
97abe3a6eaShanjiezhou     pub fn __write_string_color(&self, fr_color: FontColor, bk_color: FontColor, s: &str) {
98fbe6becdSLoGin         textui_putstr(s, fr_color, bk_color).ok();
9940fe15e0SLoGin     }
100fb6c29d0Slogin }
101fb6c29d0Slogin 
102fb6c29d0Slogin /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
103fb6c29d0Slogin impl fmt::Write for PrintkWriter {
104fb6c29d0Slogin     fn write_str(&mut self, s: &str) -> fmt::Result {
105fb6c29d0Slogin         self.__write_string(s);
106fb6c29d0Slogin         Ok(())
107fb6c29d0Slogin     }
108fb6c29d0Slogin }
109fb6c29d0Slogin 
110fb6c29d0Slogin #[doc(hidden)]
111fb6c29d0Slogin pub fn __printk(args: fmt::Arguments) {
112fb6c29d0Slogin     PrintkWriter.write_fmt(args).unwrap();
113fb6c29d0Slogin }
1148d72b68dSJomo 
1158d72b68dSJomo pub struct Logger;
1168d72b68dSJomo 
1178d72b68dSJomo impl Logger {
1188d72b68dSJomo     pub fn log(&self, log_level: usize, message: fmt::Arguments) {
119*453452ccSLoGin         if unsafe { KMSG.is_some() } {
1208d72b68dSJomo             let timestamp: TimeSpec = TimeSpec::now();
1218d72b68dSJomo             let log_level = LogLevel::from(log_level.clone());
122*453452ccSLoGin 
1238d72b68dSJomo             let log_message = LogMessage::new(timestamp, log_level, message.to_string());
1248d72b68dSJomo 
1258d72b68dSJomo             unsafe { KMSG.as_ref().unwrap().lock_irqsave().push(log_message) };
1268d72b68dSJomo         }
1278d72b68dSJomo     }
1288d72b68dSJomo }
129