lib.rs 2.2 KB

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