2025-12-31 01:08:12 +01:00
|
|
|
use core::cell::RefCell;
|
2025-12-29 19:36:00 +01:00
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
2025-12-31 00:54:48 +01:00
|
|
|
use critical_section::{CriticalSection, Mutex};
|
2025-12-31 01:08:12 +01:00
|
|
|
use esp_hal::Blocking;
|
2026-01-06 22:52:41 +01:00
|
|
|
use esp_hal::uart::UartTx;
|
2026-01-11 00:53:08 +01:00
|
|
|
use log::{LevelFilter, Log};
|
|
|
|
|
|
|
|
|
|
pub const LOG_LEVEL_FILTER: LevelFilter = {
|
|
|
|
|
if let Some(string) = option_env!("ESP_LOG") {
|
|
|
|
|
if string.eq_ignore_ascii_case("ERROR") {
|
|
|
|
|
LevelFilter::Error
|
|
|
|
|
} else if string.eq_ignore_ascii_case("WARN") {
|
|
|
|
|
LevelFilter::Warn
|
|
|
|
|
} else if string.eq_ignore_ascii_case("INFO") {
|
|
|
|
|
LevelFilter::Info
|
|
|
|
|
} else if string.eq_ignore_ascii_case("DEBUG") {
|
|
|
|
|
LevelFilter::Debug
|
|
|
|
|
} else if string.eq_ignore_ascii_case("TRACE") {
|
|
|
|
|
LevelFilter::Trace
|
|
|
|
|
} else {
|
|
|
|
|
panic!("Unknown `ESP_LOG` value. Only `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`, or `OFF` may be used.");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LevelFilter::Off
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-12-29 19:36:00 +01:00
|
|
|
|
2026-01-06 22:52:41 +01:00
|
|
|
static ALT_LOGGER_UART: Mutex<RefCell<Option<UartTx<'static, Blocking>>>> =
|
|
|
|
|
Mutex::new(RefCell::new(None));
|
2025-12-29 19:36:00 +01:00
|
|
|
|
2026-01-06 22:52:41 +01:00
|
|
|
pub fn with_uart_tx<R>(
|
|
|
|
|
f: impl FnOnce(CriticalSection<'_>, &'_ mut UartTx<'static, Blocking>) -> R,
|
|
|
|
|
) -> R {
|
2025-12-31 00:54:48 +01:00
|
|
|
critical_section::with(|cs| {
|
|
|
|
|
let mut uart = ALT_LOGGER_UART.borrow(cs).borrow_mut();
|
2025-12-31 01:08:12 +01:00
|
|
|
let uart = uart.as_mut().unwrap();
|
2025-12-31 00:54:48 +01:00
|
|
|
|
2025-12-31 01:08:12 +01:00
|
|
|
(f)(cs, uart)
|
2025-12-31 00:54:48 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 19:36:00 +01:00
|
|
|
struct AlternativeLogger;
|
|
|
|
|
|
|
|
|
|
impl Log for AlternativeLogger {
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn enabled(&self, _: &log::Metadata) -> bool {
|
|
|
|
|
// Filtered by `log` already
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn log(&self, record: &log::Record) {
|
2025-12-31 00:54:48 +01:00
|
|
|
with_uart_tx(|cs, uart| {
|
|
|
|
|
print_log_record(uart, record);
|
2025-12-29 19:36:00 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flush(&self) {}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 00:54:48 +01:00
|
|
|
const RESET: &str = "\u{001B}[0m";
|
|
|
|
|
const RED: &str = "\u{001B}[31m";
|
|
|
|
|
const GREEN: &str = "\u{001B}[32m";
|
|
|
|
|
const YELLOW: &str = "\u{001B}[33m";
|
|
|
|
|
const BLUE: &str = "\u{001B}[34m";
|
|
|
|
|
const CYAN: &str = "\u{001B}[35m";
|
2025-12-29 19:36:00 +01:00
|
|
|
|
2026-01-05 04:16:05 +01:00
|
|
|
#[cfg(feature = "rtt-log")]
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
use ::rtt_target::{rprint as print, rprintln as println};
|
|
|
|
|
|
2025-12-29 19:36:00 +01:00
|
|
|
#[cfg(feature = "alt-log")]
|
2026-01-01 03:59:24 +01:00
|
|
|
#[allow(unused)]
|
2025-12-29 19:36:00 +01:00
|
|
|
macro_rules! println {
|
|
|
|
|
() => {{
|
|
|
|
|
do_print(Default::default());
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
($($arg:tt)*) => {{
|
|
|
|
|
do_print(::core::format_args!($($arg)*));
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 03:59:24 +01:00
|
|
|
#[allow(unused)]
|
2025-12-29 19:36:00 +01:00
|
|
|
fn do_print(args: core::fmt::Arguments<'_>) {
|
2025-12-31 01:08:12 +01:00
|
|
|
with_uart_tx(|_, uart| {
|
2025-12-29 19:36:00 +01:00
|
|
|
uart.write_fmt(args).unwrap();
|
|
|
|
|
uart.write_str("\n").unwrap();
|
|
|
|
|
uart.flush().unwrap();
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_log_record(uart: &mut UartTx<'_, Blocking>, record: &log::Record) {
|
|
|
|
|
let color = match record.level() {
|
|
|
|
|
log::Level::Error => RED,
|
|
|
|
|
log::Level::Warn => YELLOW,
|
|
|
|
|
log::Level::Info => GREEN,
|
|
|
|
|
log::Level::Debug => BLUE,
|
|
|
|
|
log::Level::Trace => CYAN,
|
|
|
|
|
};
|
|
|
|
|
let reset = RESET;
|
2026-01-06 22:52:41 +01:00
|
|
|
let args = format_args!(
|
|
|
|
|
"{}{:>5} - {}{}\n",
|
|
|
|
|
color,
|
|
|
|
|
record.level(),
|
|
|
|
|
record.args(),
|
|
|
|
|
reset
|
|
|
|
|
);
|
2025-12-29 19:36:00 +01:00
|
|
|
uart.write_fmt(args).unwrap();
|
|
|
|
|
uart.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 04:16:05 +01:00
|
|
|
#[cfg(feature = "rtt-log")]
|
|
|
|
|
use panic_rtt_target as _;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "alt-log")]
|
2025-12-29 19:36:00 +01:00
|
|
|
#[panic_handler]
|
|
|
|
|
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
|
|
|
|
use esp_backtrace::Backtrace;
|
|
|
|
|
|
|
|
|
|
println!("{RED}");
|
2025-12-31 22:24:26 +01:00
|
|
|
println!("=============== CUSTOM PANIC HANDLER ==============");
|
2025-12-29 19:36:00 +01:00
|
|
|
println!("{info}{RESET}");
|
|
|
|
|
println!("");
|
|
|
|
|
println!("Backtrace:");
|
|
|
|
|
println!("");
|
|
|
|
|
|
|
|
|
|
let backtrace = Backtrace::capture();
|
|
|
|
|
|
|
|
|
|
for frame in backtrace.frames() {
|
|
|
|
|
println!("0x{:x}", frame.program_counter());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loop {}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 22:52:41 +01:00
|
|
|
#[cfg(feature = "alt-log")]
|
|
|
|
|
pub fn setup_alternative_logging(
|
|
|
|
|
alt_uart: UartTx<'static, Blocking>,
|
|
|
|
|
level_filter: log::LevelFilter,
|
|
|
|
|
) {
|
2025-12-29 19:36:00 +01:00
|
|
|
critical_section::with(|cs| {
|
|
|
|
|
*ALT_LOGGER_UART.borrow(cs).borrow_mut() = Some(alt_uart);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
log::set_logger_racy(&AlternativeLogger).unwrap();
|
|
|
|
|
log::set_max_level_racy(level_filter);
|
|
|
|
|
}
|
|
|
|
|
}
|