Skip to main content

ax_runtime/
emergency_console.rs

1//! Synchronous emergency text output.
2//!
3//! This is the only public console path for panic, oops, and other contexts
4//! that cannot sleep. It performs direct synchronous hardware output and never
5//! waits for the serial worker or acquires a sleepable lock. When runtime
6//! ownership has not been committed yet, it uses the platform early console.
7
8use core::fmt::{self, Write};
9
10/// Synchronously writes one formatted emergency record.
11///
12/// The call itself does not queue work, allocate, or sleep. Once the emergency
13/// path claims a runtime UART, normal worker and IRQ register access remains
14/// excluded until shutdown so fatal records cannot be interleaved.
15pub fn write_fmt(args: fmt::Arguments<'_>) -> usize {
16    if let Some(written) = crate::serial::emergency_write(args) {
17        return written;
18    }
19
20    let mut writer = PlatformEmergencyWriter::default();
21    let _ = fmt::write(&mut writer, args);
22    writer.written
23}
24
25#[derive(Default)]
26struct PlatformEmergencyWriter {
27    written: usize,
28}
29
30impl Write for PlatformEmergencyWriter {
31    fn write_str(&mut self, text: &str) -> fmt::Result {
32        crate::hal::console::write_text_bytes(text.as_bytes());
33        self.written = self.written.saturating_add(text.len());
34        Ok(())
35    }
36}