printk.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. use core::{
  2. fmt::{self, Write},
  3. sync::atomic::Ordering,
  4. };
  5. use alloc::string::ToString;
  6. use super::lib_ui::textui::{textui_putstr, FontColor};
  7. use crate::{
  8. driver::tty::{
  9. tty_driver::TtyOperation, tty_port::TTY_PORTS,
  10. virtual_terminal::virtual_console::CURRENT_VCNUM,
  11. },
  12. filesystem::procfs::{
  13. kmsg::KMSG,
  14. log::{LogLevel, LogMessage},
  15. },
  16. time::TimeSpec,
  17. };
  18. #[macro_export]
  19. macro_rules! print {
  20. ($($arg:tt)*) => ($crate::libs::printk::__printk(format_args!($($arg)*)));
  21. }
  22. #[macro_export]
  23. macro_rules! println {
  24. () => {
  25. $crate::print!("\n");
  26. };
  27. ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
  28. }
  29. #[macro_export]
  30. macro_rules! kdebug {
  31. ($($arg:tt)*) => {
  32. $crate::libs::printk::Logger.log(7,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  33. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ DEBUG ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
  34. }
  35. }
  36. #[macro_export]
  37. macro_rules! kinfo {
  38. ($($arg:tt)*) => {
  39. $crate::libs::printk::Logger.log(6,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  40. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("[ INFO ] ({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)))
  41. }
  42. }
  43. #[macro_export]
  44. macro_rules! kwarn {
  45. ($($arg:tt)*) => {
  46. $crate::libs::printk::Logger.log(4,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  47. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[1;33m[ WARN ] \x1B[0m"));
  48. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  49. }
  50. }
  51. #[macro_export]
  52. macro_rules! kerror {
  53. ($($arg:tt)*) => {
  54. $crate::libs::printk::Logger.log(3,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  55. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ ERROR ] \x1B[0m"));
  56. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  57. }
  58. }
  59. #[macro_export]
  60. macro_rules! kBUG {
  61. ($($arg:tt)*) => {
  62. $crate::libs::printk::Logger.log(1,format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  63. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("\x1B[41m[ BUG ] \x1B[0m"));
  64. $crate::libs::printk::PrintkWriter.__write_fmt(format_args!("({}:{})\t {}\n", file!(), line!(),format_args!($($arg)*)));
  65. }
  66. }
  67. pub struct PrintkWriter;
  68. impl PrintkWriter {
  69. #[inline]
  70. pub fn __write_fmt(&mut self, args: fmt::Arguments) {
  71. self.write_fmt(args).ok();
  72. }
  73. /// 并输出白底黑字
  74. /// @param str: 要写入的字符
  75. pub fn __write_string(&mut self, s: &str) {
  76. let current_vcnum = CURRENT_VCNUM.load(Ordering::SeqCst);
  77. if current_vcnum != -1 {
  78. // tty已经初始化了之后才输出到屏幕
  79. let port = TTY_PORTS[current_vcnum as usize].clone();
  80. let tty = port.port_data().tty();
  81. if tty.is_some() {
  82. let tty = tty.unwrap();
  83. let _ = tty.write(tty.core(), s.as_bytes(), s.len());
  84. } else {
  85. let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK);
  86. }
  87. } else {
  88. let _ = textui_putstr(s, FontColor::WHITE, FontColor::BLACK);
  89. }
  90. }
  91. }
  92. /// 为Printk Writer实现core::fmt::Write, 使得能够借助Rust自带的格式化组件,格式化字符并输出
  93. impl fmt::Write for PrintkWriter {
  94. fn write_str(&mut self, s: &str) -> fmt::Result {
  95. self.__write_string(s);
  96. Ok(())
  97. }
  98. }
  99. #[doc(hidden)]
  100. pub fn __printk(args: fmt::Arguments) {
  101. PrintkWriter.write_fmt(args).unwrap();
  102. }
  103. pub struct Logger;
  104. impl Logger {
  105. pub fn log(&self, log_level: usize, message: fmt::Arguments) {
  106. if unsafe { KMSG.is_some() } {
  107. let timestamp: TimeSpec = TimeSpec::now();
  108. let log_level = LogLevel::from(log_level.clone());
  109. let log_message = LogMessage::new(timestamp, log_level, message.to_string());
  110. unsafe { KMSG.as_ref().unwrap().lock_irqsave().push(log_message) };
  111. }
  112. }
  113. }