lib.rs 2.2 KB

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