Skip to content

Commit 86660a7

Browse files
josephlrrbradford
authored andcommitted
serial: Allow for nested logging
The current serial implementation will panic if you log! within a log! statment. For example, the following code panics: fn foo() { log!("foo: {}, bar()"); } fn bar() -> u64 { log!("bar"); 42 } We change the implementation to have the PORT be static rather than the entire Serial structure. Signed-off-by: Joe Richey <[email protected]>
1 parent efd9748 commit 86660a7

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

src/efi/console.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub extern "win64" fn stdout_output_string(
5252
message: *mut Char16,
5353
) -> Status {
5454
use core::fmt::Write;
55-
let mut serial = crate::serial::SERIAL.borrow_mut();
55+
let mut serial = crate::serial::Serial;
5656
let mut string_end = false;
5757

5858
loop {

src/serial.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,16 @@ use core::fmt;
2020
use atomic_refcell::AtomicRefCell;
2121
use x86_64::instructions::port::PortWriteOnly;
2222

23-
pub static SERIAL: AtomicRefCell<Serial> = AtomicRefCell::new(Serial::new());
23+
// We use COM1 as it is the standard first serial port.
24+
static PORT: AtomicRefCell<PortWriteOnly<u8>> = AtomicRefCell::new(PortWriteOnly::new(0x3f8));
2425

25-
pub struct Serial {
26-
port: PortWriteOnly<u8>,
27-
}
28-
29-
impl Serial {
30-
pub const fn new() -> Self {
31-
// We use COM1 as it is the standard first serial port.
32-
Self {
33-
port: PortWriteOnly::new(0x3f8),
34-
}
35-
}
36-
}
26+
pub struct Serial;
3727

3828
impl fmt::Write for Serial {
3929
fn write_str(&mut self, s: &str) -> fmt::Result {
30+
let mut port = PORT.borrow_mut();
4031
for b in s.bytes() {
41-
unsafe { self.port.write(b) }
32+
unsafe { port.write(b) }
4233
}
4334
Ok(())
4435
}
@@ -49,7 +40,7 @@ macro_rules! log {
4940
($($arg:tt)*) => {{
5041
use core::fmt::Write;
5142
#[cfg(all(feature = "log-serial", not(test)))]
52-
writeln!(crate::serial::SERIAL.borrow_mut(), $($arg)*).unwrap();
43+
writeln!(crate::serial::Serial, $($arg)*).unwrap();
5344
#[cfg(all(feature = "log-serial", test))]
5445
println!($($arg)*);
5546
}};

0 commit comments

Comments
 (0)