Skip to content

Commit

Permalink
Add 'std' feature to RustAdvancedLoggerDxe which enables use of std::…
Browse files Browse the repository at this point in the history
…println instead of AdvLoggerProtocol - useful for test environments.
  • Loading branch information
joschock committed Nov 29, 2023
1 parent 94459fb commit 8cf24c1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
4 changes: 3 additions & 1 deletion AdvLoggerPkg/Crates/RustAdvancedLoggerDxe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ path = "src/lib.rs"

[dependencies]
r-efi = {workspace=true}
spin = {workspace=true}

[features]
std = []
100 changes: 70 additions & 30 deletions AdvLoggerPkg/Crates/RustAdvancedLoggerDxe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,78 @@ pub fn _log(level: usize, args: fmt::Arguments) {
LOGGER.log(level, args)
}

/// Prints to the AdvancedLogger log at the specified level.
///
/// This macro uses the same syntax as rust std [`std::println!`] macro, with the addition of a level argument that
/// indicates what debug level the output is to be written at.
///
/// See [`std::fmt`] for details on format strings.
///
/// ```no_run
/// use rust_advanced_logger_dxe::{init_debug, debug, DEBUG_INFO};
/// use r_efi::efi::Status;
/// pub extern "efiapi" fn efi_main(
/// _image_handle: *const core::ffi::c_void,
/// _system_table: *const r_efi::system::SystemTable,
/// ) -> u64 {
///
/// //Initialize debug logging - no output without this.
/// init_debug(unsafe { (*_system_table).boot_services});
///
/// debug!(DEBUG_INFO, "Hello, World. This is {:} in {:}. ", "rust", "UEFI");
/// debug!(DEBUG_INFO, "Better add our own newline.\n");
///
/// Status::SUCCESS.as_usize() as u64
/// }
/// ```
#[macro_export]
macro_rules! debug {
($level:expr, $($arg:tt)*) => {
$crate::_log($level, format_args!($($arg)*))
}
#[cfg(not(feature = "std"))]
mod no_std_debug {
/// Prints to the AdvancedLogger log at the specified level.
///
/// This macro uses the same syntax as rust std [`std::println!`] macro, with the addition of a level argument that
/// indicates what debug level the output is to be written at.
///
/// See [`std::fmt`] for details on format strings.
///
/// ```no_run
/// use rust_advanced_logger_dxe::{init_debug, debug, DEBUG_INFO};
/// use r_efi::efi::Status;
/// pub extern "efiapi" fn efi_main(
/// _image_handle: *const core::ffi::c_void,
/// _system_table: *const r_efi::system::SystemTable,
/// ) -> u64 {
///
/// //Initialize debug logging - no output without this.
/// init_debug(unsafe { (*_system_table).boot_services});
///
/// debug!(DEBUG_INFO, "Hello, World. This is {:} in {:}. ", "rust", "UEFI");
/// debug!(DEBUG_INFO, "Better add our own newline.\n");
///
/// Status::SUCCESS.as_usize() as u64
/// }
/// ```
#[macro_export]
macro_rules! debug {
($level:expr, $($arg:tt)*) => {
$crate::_log($level, format_args!($($arg)*))
}
}
}

#[cfg(feature = "std")]
mod std_debug {
extern crate std;

/// Prints to the console log.
///
/// This macro uses the same syntax as rust std [`std::println!`] macro, with the addition of a level argument that
/// indicates what debug level the output is to be written at.
///
/// See [`std::fmt`] for details on format strings.
///
/// ```no_run
/// use rust_advanced_logger_dxe::{init_debug, debug, DEBUG_INFO};
/// use r_efi::efi::Status;
/// pub extern "efiapi" fn efi_main(
/// _image_handle: *const core::ffi::c_void,
/// _system_table: *const r_efi::system::SystemTable,
/// ) -> u64 {
///
/// //Initialize debug logging - no output without this.
/// init_debug(unsafe { (*_system_table).boot_services});
///
/// debug!(DEBUG_INFO, "Hello, World. This is {:} in {:}. ", "rust", "UEFI");
/// debug!(DEBUG_INFO, "Better add our own newline.\n");
///
/// Status::SUCCESS.as_usize() as u64
/// }
/// ```
#[macro_export]
macro_rules! debug {
($level:expr, $($arg:tt)*) => {
let _ = $level;
print!($($arg)*)
}
}
}

/// Prints to the AdvancedLogger log at the specified level with a newline.
/// Prints to the log with a newline.
///
/// Equivalent to the [`debug!`] macro except that a newline is appended to the format string.
///
Expand Down

0 comments on commit 8cf24c1

Please sign in to comment.