xref: /DragonOS/kernel/src/libs/printk.rs (revision a381e482cbe742b2e4bbeaacae134a8131bf3f91)
1 use core::fmt::{self, Write};
2 
3 use alloc::string::ToString;
4 
5 use super::lib_ui::textui::{textui_putstr, FontColor};
6 
7 use crate::{
8     filesystem::procfs::{
9         kmsg::KMSG,
10         log::{LogLevel, LogMessage},
11     },
12     time::TimeSpec,
13 };
14 
15 #[macro_export]
16 macro_rules! print {
17     ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*)));
18 }
19 
20 #[macro_export]
21 macro_rules! println {
22     () => {
23         $crate::print!("\n");
24     };
25     ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
26 }
27 
28 /// 指定颜色,彩色输出
29 /// @param FRcolor 前景色
30 /// @param BKcolor 背景色
31 #[macro_export]
32 macro_rules! printk_color {
33 
34     ($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
35         use alloc;
36         $crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
37     };
38 }
39 
40 #[macro_export]
41 macro_rules! kdebug {
42     ($($arg:tt)*) => {
43         $crate::libs::printk::Logger.log(7,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
44         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
45     }
46 }
47 
48 #[macro_export]
49 macro_rules! kinfo {
50     ($($arg:tt)*) => {
51         $crate::libs::printk::Logger.log(6,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
52         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
53     }
54 }
55 
56 #[macro_export]
57 macro_rules! kwarn {
58     ($($arg:tt)*) => {
59         $crate::libs::printk::Logger.log(4,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
60         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::YELLOW, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ WARN ] ");
61         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
62     }
63 }
64 
65 #[macro_export]
66 macro_rules! kerror {
67     ($($arg:tt)*) => {
68         $crate::libs::printk::Logger.log(3,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
69         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ ERROR ] ");
70         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
71     }
72 }
73 
74 #[macro_export]
75 macro_rules! kBUG {
76     ($($arg:tt)*) => {
77         $crate::libs::printk::Logger.log(1,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
78         $crate::libs::printk::PrintkWriter.__write_string_color($crate::libs::lib_ui::textui::FontColor::RED, $crate::libs::lib_ui::textui::FontColor::BLACK, "[ BUG ] ");
79         $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
80     }
81 }
82 
83 pub struct PrintkWriter;
84 
85 impl PrintkWriter {
86     #[inline]
87     pub fn __write_fmt(&mut self, args: fmt::Arguments) {
88         self.write_fmt(args).ok();
89     }
90 
91     /// 并输出白底黑字
92     /// @param str: 要写入的字符
93     pub fn __write_string(&mut self, s: &str) {
94         textui_putstr(s, FontColor::WHITE, FontColor::BLACK).ok();
95     }
96 
97     pub fn __write_string_color(&self, fr_color: FontColor, bk_color: FontColor, s: &str) {
98         textui_putstr(s, fr_color, bk_color).ok();
99     }
100 }
101 
102 /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
103 impl fmt::Write for PrintkWriter {
104     fn write_str(&mut self, s: &str) -> fmt::Result {
105         self.__write_string(s);
106         Ok(())
107     }
108 }
109 
110 #[doc(hidden)]
111 pub fn __printk(args: fmt::Arguments) {
112     PrintkWriter.write_fmt(args).unwrap();
113 }
114 
115 pub struct Logger;
116 
117 impl Logger {
118     pub fn log(&self, log_level: usize, message: fmt::Arguments) {
119         if unsafe { !KMSG.is_none() } {
120             let timestamp: TimeSpec = TimeSpec::now();
121             let log_level = LogLevel::from(log_level.clone());
122             let log_message = LogMessage::new(timestamp, log_level, message.to_string());
123 
124             unsafe { KMSG.as_ref().unwrap().lock_irqsave().push(log_message) };
125         }
126     }
127 }
128