2025-11-23 22:40:06 -07:00
|
|
|
// Choose if the macros are enabled based on the feature setting
|
|
|
|
|
#[cfg(feature = "log")]
|
2026-02-20 14:34:32 -07:00
|
|
|
mod log_enabled;
|
2025-11-23 22:40:06 -07:00
|
|
|
|
|
|
|
|
#[cfg(not(feature = "log"))]
|
2026-02-20 14:34:32 -07:00
|
|
|
mod log_disabled;
|
2025-11-23 22:40:06 -07:00
|
|
|
|
2025-11-09 12:34:52 -07:00
|
|
|
mod pretty_logger;
|
|
|
|
|
|
2026-03-17 16:40:05 -06:00
|
|
|
use alloc::boxed::Box;
|
|
|
|
|
use alloc::string::String;
|
2025-11-09 12:34:52 -07:00
|
|
|
pub use pretty_logger::PrettyLogger;
|
2026-02-09 10:27:15 -07:00
|
|
|
pub use pretty_logger::log;
|
2025-11-09 12:34:52 -07:00
|
|
|
|
2026-02-20 15:55:21 -07:00
|
|
|
pub static mut IS_DEFAULT_LOGGER: bool = true;
|
2025-11-09 12:34:52 -07:00
|
|
|
static mut LOGGER: &dyn Logger = &DefaultLogger;
|
|
|
|
|
|
2026-03-17 16:40:05 -06:00
|
|
|
#[derive(Debug)]
|
2025-11-09 12:34:52 -07:00
|
|
|
pub enum LogLevel {
|
|
|
|
|
Debug,
|
|
|
|
|
Info,
|
|
|
|
|
Warn,
|
|
|
|
|
Error,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:40:05 -06:00
|
|
|
#[derive(Debug)]
|
2025-11-09 12:34:52 -07:00
|
|
|
pub struct Record {
|
|
|
|
|
log_level: LogLevel,
|
2025-11-11 11:00:28 -07:00
|
|
|
location: Option<String>,
|
2025-11-09 12:34:52 -07:00
|
|
|
// line: u32,
|
2026-03-17 16:40:05 -06:00
|
|
|
time: Option<u64>,
|
2025-11-09 12:34:52 -07:00
|
|
|
message: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait Logger {
|
|
|
|
|
fn log(&self, log: Record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DefaultLogger;
|
|
|
|
|
|
|
|
|
|
impl Logger for DefaultLogger {
|
|
|
|
|
fn log(&self, _: Record) {}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 14:27:06 -07:00
|
|
|
#[allow(unused_variables)]
|
2025-11-09 12:34:52 -07:00
|
|
|
pub fn set_logger_box(logger: Box<dyn Logger>) {
|
2025-11-24 08:45:44 -07:00
|
|
|
#[cfg(feature = "log")]
|
2025-11-09 12:34:52 -07:00
|
|
|
unsafe {
|
|
|
|
|
LOGGER = Box::leak(logger);
|
2026-02-20 15:55:21 -07:00
|
|
|
IS_DEFAULT_LOGGER = false;
|
2025-11-09 12:34:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_logger(logger: &'static dyn Logger) {
|
|
|
|
|
unsafe {
|
|
|
|
|
LOGGER = logger;
|
2026-02-20 15:55:21 -07:00
|
|
|
IS_DEFAULT_LOGGER = false;
|
2025-11-09 12:34:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 11:00:28 -07:00
|
|
|
pub fn add_record(
|
|
|
|
|
log_level: LogLevel,
|
|
|
|
|
location: Option<String>,
|
2026-03-17 16:40:05 -06:00
|
|
|
time: Option<u64>,
|
2025-11-11 11:00:28 -07:00
|
|
|
message: String,
|
|
|
|
|
) {
|
2025-11-09 12:34:52 -07:00
|
|
|
logger().log(Record {
|
|
|
|
|
log_level,
|
|
|
|
|
location,
|
|
|
|
|
time,
|
|
|
|
|
message,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 09:13:46 -07:00
|
|
|
pub fn logger() -> &'static dyn Logger {
|
2025-11-09 12:34:52 -07:00
|
|
|
unsafe { LOGGER }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code, improper_ctypes_definitions)]
|
|
|
|
|
pub type SetupLogger = extern "C" fn(logger: &'static dyn Logger);
|
|
|
|
|
|
|
|
|
|
#[unsafe(no_mangle)]
|
|
|
|
|
#[allow(improper_ctypes_definitions)]
|
|
|
|
|
pub extern "C" fn setup_logger(logger: &'static dyn Logger) {
|
|
|
|
|
set_logger(logger);
|
|
|
|
|
}
|
2026-02-20 14:34:32 -07:00
|
|
|
|
|
|
|
|
// Macro Definitions
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! debug {
|
|
|
|
|
($($arg:tt)*) => {
|
|
|
|
|
$crate::log!($crate::logger::LogLevel::Debug, $($arg)*)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! info {
|
|
|
|
|
($($arg:tt)*) => {
|
|
|
|
|
$crate::log!($crate::logger::LogLevel::Info, $($arg)*)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! warn {
|
|
|
|
|
($($arg:tt)*) => {
|
|
|
|
|
$crate::log!($crate::logger::LogLevel::Warn, $($arg)*)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! error {
|
|
|
|
|
($($arg:tt)*) => {
|
|
|
|
|
$crate::log!($crate::logger::LogLevel::Error, $($arg)*)
|
|
|
|
|
};
|
|
|
|
|
}
|