Skip to content

Commit

Permalink
logger: Feature gate logging to serial output
Browse files Browse the repository at this point in the history
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 <joerichey@google.com>
  • Loading branch information
josephlr committed Nov 13, 2019
1 parent 12c1679 commit 0669ebe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ lto = true
panic = "abort"
lto = true

[features]
default = ["log-serial", "log-panic"]
# Have the log! macro write to serial output. Disabling this significantly
# reduces code size, but makes debugging essentially impossible
log-serial = []
# Log panics to serial output. Disabling this (without disabling log-serial)
# gets you most of the code size reduction, without losing _all_ debugging.
log-panic = ["log-serial"]

[dependencies]
cpuio = "*"
spin = "0.4.9"
Expand Down
4 changes: 2 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl fmt::Write for Logger {
macro_rules! log {
($($arg:tt)*) => {{
use core::fmt::Write;
#[cfg(not(test))]
#[cfg(all(feature = "log-serial", not(test)))]
writeln!(&mut crate::logger::LOGGER.lock(), $($arg)*).unwrap();
#[cfg(test)]
#[cfg(all(feature = "log-serial", test))]
println!($($arg)*);
}};
}
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#![feature(global_asm)]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
#![cfg_attr(test, allow(unused_imports))]
#![cfg_attr(test, allow(dead_code))]
#![cfg_attr(test, allow(unused_imports, dead_code))]
#![cfg_attr(not(feature = "log-serial"), allow(unused_variables, unused_imports))]

#[macro_use]
mod logger;
Expand Down Expand Up @@ -45,12 +45,19 @@ extern "C" {
fn halt_loop() -> !;
}

#[cfg_attr(not(test), panic_handler)]
#[cfg(all(not(test), feature = "log-panic"))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
log!("PANIC: {}", info);
unsafe { halt_loop() }
}

#[cfg(all(not(test), not(feature = "log-panic")))]
#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
loop {}
}

/// Setup page tables to provide an identity mapping over the full 4GiB range
fn setup_pagetables() {
type PageTable = [u64; 512];
Expand Down

0 comments on commit 0669ebe

Please sign in to comment.