Skip to content

Commit 0669ebe

Browse files
committed
logger: Feature gate logging to serial output
Logging to serial outputs on all panics significantly increases code size. We add two (on by default) features to disable serial logging: - Everywhere - Just in panics Signed-off-by: Joe Richey <[email protected]>
1 parent 12c1679 commit 0669ebe

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ lto = true
1414
panic = "abort"
1515
lto = true
1616

17+
[features]
18+
default = ["log-serial", "log-panic"]
19+
# Have the log! macro write to serial output. Disabling this significantly
20+
# reduces code size, but makes debugging essentially impossible
21+
log-serial = []
22+
# Log panics to serial output. Disabling this (without disabling log-serial)
23+
# gets you most of the code size reduction, without losing _all_ debugging.
24+
log-panic = ["log-serial"]
25+
1726
[dependencies]
1827
cpuio = "*"
1928
spin = "0.4.9"

src/logger.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ impl fmt::Write for Logger {
5454
macro_rules! log {
5555
($($arg:tt)*) => {{
5656
use core::fmt::Write;
57-
#[cfg(not(test))]
57+
#[cfg(all(feature = "log-serial", not(test)))]
5858
writeln!(&mut crate::logger::LOGGER.lock(), $($arg)*).unwrap();
59-
#[cfg(test)]
59+
#[cfg(all(feature = "log-serial", test))]
6060
println!($($arg)*);
6161
}};
6262
}

src/main.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#![feature(global_asm)]
1616
#![cfg_attr(not(test), no_std)]
1717
#![cfg_attr(not(test), no_main)]
18-
#![cfg_attr(test, allow(unused_imports))]
19-
#![cfg_attr(test, allow(dead_code))]
18+
#![cfg_attr(test, allow(unused_imports, dead_code))]
19+
#![cfg_attr(not(feature = "log-serial"), allow(unused_variables, unused_imports))]
2020

2121
#[macro_use]
2222
mod logger;
@@ -45,12 +45,19 @@ extern "C" {
4545
fn halt_loop() -> !;
4646
}
4747

48-
#[cfg_attr(not(test), panic_handler)]
48+
#[cfg(all(not(test), feature = "log-panic"))]
49+
#[panic_handler]
4950
fn panic(info: &PanicInfo) -> ! {
5051
log!("PANIC: {}", info);
5152
unsafe { halt_loop() }
5253
}
5354

55+
#[cfg(all(not(test), not(feature = "log-panic")))]
56+
#[panic_handler]
57+
fn panic(_: &PanicInfo) -> ! {
58+
loop {}
59+
}
60+
5461
/// Setup page tables to provide an identity mapping over the full 4GiB range
5562
fn setup_pagetables() {
5663
type PageTable = [u64; 512];

0 commit comments

Comments
 (0)