1 use crate::{boot, system};
2 use core::fmt::Write;
3
4 /// INTERNAL API! Helper for print macros.
5 #[doc(hidden)]
_print(args: core::fmt::Arguments)6 pub fn _print(args: core::fmt::Arguments) {
7 if boot::are_boot_services_active() {
8 system::with_stdout(|stdout| {
9 stdout.write_fmt(args).expect("Failed to write to stdout");
10 });
11 } else {
12 // Ease debugging: Depending on logger, this might write to serial or
13 // debugcon.
14 log::debug!("You are using `print!` after the boot services have been exited.");
15 }
16 }
17
18 /// Prints to the standard output of the UEFI boot service console.
19 ///
20 /// # Usage
21 /// Use this similar to `print!` from the Rust standard library, but only
22 /// as long as boot services have not been exited.
23 ///
24 /// You should never use this macro in a custom Logger ([`log::Log`] impl) to
25 /// prevent a circular runtime dependency.
26 ///
27 /// # Panics
28 /// Will panic if the system table's `stdout` is not set, or if writing fails.
29 ///
30 /// # Examples
31 /// ```
32 /// print!("");
33 /// print!("Hello World\n");
34 /// print!("Hello {}", "World");
35 /// ```
36 #[macro_export]
37 macro_rules! print {
38 ($($arg:tt)*) => ($crate::helpers::_print(core::format_args!($($arg)*)));
39 }
40
41 /// Prints to the standard output of the UEFI boot service console, but with a
42 /// newline.
43 ///
44 /// # Usage
45 /// Use this similar to `println!` from the Rust standard library, but only
46 /// as long as boot services have not been exited.
47 ///
48 /// You should never use this macro in a custom Logger ([`log::Log`] impl) to
49 /// prevent a circular runtime dependency.
50 ///
51 /// # Panics
52 /// Will panic if the system table's `stdout` is not set, or if writing fails.
53 ///
54 /// # Examples
55 /// ```
56 /// println!();
57 /// println!("Hello World");
58 /// println!("Hello {}", "World");
59 /// ```
60 #[macro_export]
61 macro_rules! println {
62 () => ($crate::print!("\n"));
63 ($($arg:tt)*) => ($crate::helpers::_print(core::format_args!("{}{}", core::format_args!($($arg)*), "\n")));
64 }
65