lib.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #![no_std]
  2. use core::fmt::{self, Write};
  3. use alloc::sync::Arc;
  4. use eonix_sync::Spin;
  5. extern crate alloc;
  6. pub trait ConsoleWrite: Send + Sync {
  7. fn write(&self, s: &str);
  8. }
  9. struct Console {
  10. console: Option<Arc<dyn ConsoleWrite>>,
  11. }
  12. // TODO!!!: We should use a `RwLock` here for better performance.
  13. static CONSOLE: Spin<Console> = Spin::new(Console::new());
  14. impl Console {
  15. const fn new() -> Self {
  16. Self { console: None }
  17. }
  18. }
  19. impl Write for Console {
  20. fn write_str(&mut self, s: &str) -> fmt::Result {
  21. if let Some(console) = self.console.as_ref() {
  22. console.write(s);
  23. }
  24. Ok(())
  25. }
  26. }
  27. pub fn set_console(console: Arc<dyn ConsoleWrite>) {
  28. CONSOLE.lock_irq().console.replace(console);
  29. }
  30. #[doc(hidden)]
  31. pub fn do_print(args: fmt::Arguments) {
  32. let _ = CONSOLE.lock_irq().write_fmt(args);
  33. }
  34. #[macro_export]
  35. macro_rules! print {
  36. ($($arg:tt)*) => {
  37. $crate::do_print(format_args!($($arg)*))
  38. };
  39. }
  40. #[macro_export]
  41. macro_rules! println {
  42. () => {
  43. $crate::print!("\n")
  44. };
  45. ($($arg:tt)*) => {
  46. $crate::print!("{}\n", format_args!($($arg)*))
  47. };
  48. }
  49. #[macro_export]
  50. macro_rules! println_warn {
  51. ($($arg:tt)*) => {
  52. $crate::println!("[kernel: warn] {}", format_args!($($arg)*))
  53. };
  54. }
  55. #[macro_export]
  56. macro_rules! println_debug {
  57. ($($arg:tt)*) => {
  58. $crate::println!("[kernel:debug] {}", format_args!($($arg)*))
  59. };
  60. }
  61. #[macro_export]
  62. macro_rules! println_info {
  63. ($($arg:tt)*) => {
  64. $crate::println!("[kernel: info] {}", format_args!($($arg)*))
  65. };
  66. }
  67. #[macro_export]
  68. macro_rules! println_fatal {
  69. () => {
  70. $crate::println!("[kernel:fatal] ")
  71. };
  72. ($($arg:tt)*) => {
  73. $crate::println!("[kernel:fatal] {}", format_args!($($arg)*))
  74. };
  75. }
  76. #[macro_export]
  77. macro_rules! println_trace {
  78. ($feat:literal) => {
  79. #[deny(unexpected_cfgs)]
  80. {
  81. #[cfg(feature = $feat)]
  82. $crate::println!("[kernel:trace] ")
  83. }
  84. };
  85. ($feat:literal, $($arg:tt)*) => {{
  86. #[deny(unexpected_cfgs)]
  87. {
  88. #[cfg(feature = $feat)]
  89. $crate::println!("[kernel:trace] {}", format_args!($($arg)*))
  90. }
  91. }};
  92. }