Skip to content

Added macros print! and println! #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 1, 2022
46 changes: 46 additions & 0 deletions uefi-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern crate log;
extern crate uefi;

use core::ffi::c_void;
use core::fmt::Write;
use core::ptr::NonNull;

use cfg_if::cfg_if;
Expand Down Expand Up @@ -95,6 +96,51 @@ pub fn init(st: &mut SystemTable<Boot>) -> Result {
}
}

// Interal function for print macros
#[doc(hidden)]
pub fn _print(args: core::fmt::Arguments) {
unsafe {
if let Some(st) = &mut SYSTEM_TABLE {
st.stdout().write_fmt(args).expect("Failed to write to stdout");
} else {
panic!("SYSTEM_TABLE is None");
}
}
}

/// Prints to the standard output
///
/// # Panics
/// Will panic if SYSTEM_TABLE is None (Before [init] and after [exit_boot_services])
///
/// # Examples
/// ```
/// print!("");
/// print!("Hello World\n");
/// print!("Hello {}", "World");
/// ```
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::_print(core::format_args!($($arg)*)));
}

/// Prints to the standard output, with a newline
///
/// # Panics
/// Will panic if SYSTEM_TABLE is None (Before [init] and after [exit_boot_services])
///
/// # Examples
/// ```
/// println!();
/// println!("Hello World");
/// println!("Hello {}", "World");
/// ```
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::_print(core::format_args!("{}{}", core::format_args!($($arg)*), "\n")));
}

/// Set up logging
///
/// This is unsafe because you must arrange for the logger to be reset with
Expand Down