Skip to content

Commit

Permalink
runtime: print trace level log messages to UART during startup.
Browse files Browse the repository at this point in the history
There's no way to retrieve them otherwise if the startup kernel
hangs.

This commit was mistakenly removed in 88ad054.
whitequark committed Dec 19, 2016
1 parent b9588dd commit 739da9f
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions artiq/firmware/runtime/logger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use core::{mem, ptr};
use core::cell::RefCell;
use core::cell::{Cell, RefCell};
use log::{self, Log, LogLevel, LogMetadata, LogRecord, LogLevelFilter};
use log_buffer::LogBuffer;
use clock;

pub struct BufferLogger {
buffer: RefCell<LogBuffer<&'static mut [u8]>>
buffer: RefCell<LogBuffer<&'static mut [u8]>>,
trace_to_uart: Cell<bool>
}

unsafe impl Sync for BufferLogger {}
@@ -15,7 +16,8 @@ static mut LOGGER: *const BufferLogger = ptr::null();
impl BufferLogger {
pub fn new(buffer: &'static mut [u8]) -> BufferLogger {
BufferLogger {
buffer: RefCell::new(LogBuffer::new(buffer))
buffer: RefCell::new(LogBuffer::new(buffer)),
trace_to_uart: Cell::new(true)
}
}

@@ -48,6 +50,14 @@ impl BufferLogger {
pub fn extract<R, F: FnOnce(&str) -> R>(&self, f: F) -> R {
f(self.buffer.borrow_mut().extract())
}

pub fn disable_trace_to_uart(&self) {
if self.trace_to_uart.get() {
trace!("disabling tracing to UART; all further trace messages \
are sent to core log only");
self.trace_to_uart.set(false)
}
}
}

impl Log for BufferLogger {
@@ -61,7 +71,10 @@ impl Log for BufferLogger {
writeln!(self.buffer.borrow_mut(),
"[{:12}us] {:>5}({}): {}",
clock::get_us(), record.level(), record.target(), record.args()).unwrap();
if record.level() <= LogLevel::Info {

// Printing to UART is really slow, so avoid doing that when we have an alternative
// route to retrieve the debug messages.
if self.trace_to_uart.get() || record.level() <= LogLevel::Info {
println!("[{:12}us] {:>5}({}): {}",
clock::get_us(), record.level(), record.target(), record.args());
}
2 changes: 2 additions & 0 deletions artiq/firmware/runtime/session.rs
Original file line number Diff line number Diff line change
@@ -590,6 +590,8 @@ pub fn thread(waiter: Waiter, spawner: Spawner) {
}
}

BufferLogger::with_instance(|logger| logger.disable_trace_to_uart());

let addr = SocketAddr::new(IP_ANY, 1381);
let listener = TcpListener::bind(waiter, addr).expect("cannot bind socket");
listener.set_keepalive(true);

0 comments on commit 739da9f

Please sign in to comment.