Skip to content

Commit 652c66f

Browse files
committed
logger: log! macro now appends newline
This makes it harder to forget to append "\n" to log statments. efi::console is also modified to take the lock outside of the loop. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 7bb61d8 commit 652c66f

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

src/efi/console.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub extern "win64" fn stdout_output_string(
4949
_: *mut SimpleTextOutputProtocol,
5050
message: *mut Char16,
5151
) -> Status {
52+
use core::fmt::Write;
53+
let mut logger = crate::logger::LOGGER.lock();
5254
let mut string_end = false;
5355

5456
loop {
@@ -62,7 +64,8 @@ pub extern "win64" fn stdout_output_string(
6264
}
6365
i += 1;
6466
}
65-
crate::log!("{}", unsafe { core::str::from_utf8_unchecked(&output) });
67+
let s = unsafe { core::str::from_utf8_unchecked(&output) };
68+
logger.write_str(s).unwrap();
6669
if string_end {
6770
break;
6871
}

src/efi/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub extern "win64" fn open(
5353
let wrapper = unsafe { &*wrapper };
5454

5555
if !wrapper.root {
56-
log!("Attempt to open file from non-root file is unsupported\n");
56+
log!("Attempt to open file from non-root file is unsupported");
5757
return Status::UNSUPPORTED;
5858
}
5959

src/logger.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use spin::Mutex;
2222
use cpuio::Port;
2323

2424
lazy_static! {
25-
static ref LOGGER: Mutex<Logger> = Mutex::new(Logger {
25+
pub static ref LOGGER: Mutex<Logger> = Mutex::new(Logger {
2626
port: unsafe { Port::new(0x3f8) }
2727
});
2828
}
2929

30-
struct Logger {
30+
pub struct Logger {
3131
port: Port<u8>,
3232
}
3333

@@ -52,17 +52,11 @@ impl fmt::Write for Logger {
5252

5353
#[macro_export]
5454
macro_rules! log {
55-
($($arg:tt)*) => ($crate::logger::_log(format_args!($($arg)*)));
56-
}
57-
58-
#[cfg(not(test))]
59-
pub fn _log(args: fmt::Arguments) {
60-
use core::fmt::Write;
61-
LOGGER.lock().write_fmt(args).unwrap();
62-
}
63-
64-
#[cfg(test)]
65-
pub fn _log(args: fmt::Arguments) {
66-
use std::io::{self, Write};
67-
write!(&mut std::io::stdout(), "{}", args).expect("stdout logging failed");
55+
($($arg:tt)*) => {{
56+
use core::fmt::Write;
57+
#[cfg(not(test))]
58+
writeln!(&mut crate::logger::LOGGER.lock(), $($arg)*).unwrap();
59+
#[cfg(test)]
60+
println!($($arg)*);
61+
}};
6862
}

src/main.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn setup_pagetables() {
6868
pde.io_write_u64(i * 8, (0xb000u64 + (0x1000u64 * i)) | 0x03);
6969
}
7070

71-
log!("Page tables setup\n");
71+
log!("Page tables setup");
7272
}
7373

7474
const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4;
@@ -77,11 +77,11 @@ const VIRTIO_PCI_BLOCK_DEVICE_ID: u16 = 0x1042;
7777
fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
7878
match device.init() {
7979
Err(_) => {
80-
log!("Error configuring block device\n");
80+
log!("Error configuring block device");
8181
return false;
8282
}
8383
Ok(_) => log!(
84-
"Virtio block device configured. Capacity: {} sectors\n",
84+
"Virtio block device configured. Capacity: {} sectors",
8585
device.get_capacity()
8686
),
8787
}
@@ -90,20 +90,20 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
9090

9191
match part::find_efi_partition(device) {
9292
Ok((start, end)) => {
93-
log!("Found EFI partition\n");
93+
log!("Found EFI partition");
9494
f = fat::Filesystem::new(device, start, end);
9595
if f.init().is_err() {
96-
log!("Failed to create filesystem\n");
96+
log!("Failed to create filesystem");
9797
return false;
9898
}
9999
}
100100
Err(_) => {
101-
log!("Failed to find EFI partition\n");
101+
log!("Failed to find EFI partition");
102102
return false;
103103
}
104104
}
105105

106-
log!("Filesystem ready\n");
106+
log!("Filesystem ready");
107107

108108
let jump_address;
109109

@@ -112,28 +112,28 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
112112
jump_address = addr;
113113
}
114114
Err(_) => {
115-
log!("Error loading default entry. Using EFI boot.\n");
115+
log!("Error loading default entry. Using EFI boot.");
116116
match f.open("/EFI/BOOT/BOOTX64 EFI") {
117117
Ok(mut file) => {
118-
log!("Found bootloader (BOOTX64.EFI)\n");
118+
log!("Found bootloader (BOOTX64.EFI)");
119119
let mut l = pe::Loader::new(&mut file);
120120
match l.load(0x20_0000) {
121121
Ok((a, size)) => {
122-
log!("Executable loaded\n");
122+
log!("Executable loaded");
123123
efi::efi_exec(a, 0x20_0000, size, &f, device);
124124
return true;
125125
}
126126
Err(e) => {
127127
match e {
128-
pe::Error::FileError => log!("File error\n"),
129-
pe::Error::InvalidExecutable => log!("Invalid executable\n"),
128+
pe::Error::FileError => log!("File error"),
129+
pe::Error::InvalidExecutable => log!("Invalid executable"),
130130
}
131131
return false;
132132
}
133133
}
134134
}
135135
Err(_) => {
136-
log!("Failed to find bootloader\n");
136+
log!("Failed to find bootloader");
137137
return false;
138138
}
139139
}
@@ -142,7 +142,7 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
142142

143143
device.reset();
144144

145-
log!("Jumping to kernel\n");
145+
log!("Jumping to kernel");
146146

147147
// Rely on x86 C calling convention where second argument is put into %rsi register
148148
let ptr = jump_address as *const ();
@@ -157,7 +157,7 @@ pub extern "C" fn _start() -> ! {
157157
asm!("movq $$0x180000, %rsp");
158158
}
159159

160-
log!("Starting..\n");
160+
log!("Starting..");
161161

162162
enable_sse2();
163163
setup_pagetables();

src/pci.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn print_bus() {
6969
continue;
7070
}
7171
log!(
72-
"Found PCI device vendor={:x} device={:x} in slot={}\n",
72+
"Found PCI device vendor={:x} device={:x} in slot={}",
7373
vendor_id,
7474
device_id,
7575
device
@@ -151,7 +151,7 @@ impl PciDevice {
151151
self.device_id = device_id;
152152

153153
log!(
154-
"PCI Device: {}:{}.{} {:x}:{:x}\n",
154+
"PCI Device: {}:{}.{} {:x}:{:x}",
155155
self.bus,
156156
self.device,
157157
self.func,
@@ -197,7 +197,7 @@ impl PciDevice {
197197

198198
#[allow(clippy::blacklisted_name)]
199199
for bar in &self.bars {
200-
log!("Bar: type={:?} address={:x}\n", bar.bar_type, bar.address);
200+
log!("Bar: type={:?} address={:x}", bar.bar_type, bar.address);
201201
}
202202
}
203203
}
@@ -258,7 +258,7 @@ impl VirtioTransport for VirtioPciTransport {
258258

259259
// bit 4 of status is capability bit
260260
if status & 1 << 4 == 0 {
261-
log!("No capabilities detected\n");
261+
log!("No capabilities detected");
262262
return Err(VirtioError::VirtioUnsupportedDevice);
263263
}
264264

0 commit comments

Comments
 (0)