2020-10-30 13:26:19 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2021-04-01 17:54:17 +00:00
|
|
|
use xous_ipc::{String, Buffer};
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
2020-10-30 13:26:19 +00:00
|
|
|
|
|
|
|
|
static XOUS_LOGGER: XousLogger = XousLogger {
|
|
|
|
|
locked: AtomicBool::new(false),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct XousLogger {
|
|
|
|
|
locked: AtomicBool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking {
|
|
|
|
|
conn: 0,
|
|
|
|
|
initialized: false,
|
|
|
|
|
buffer: None,
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-01 17:54:17 +00:00
|
|
|
struct XousLoggerBacking<'a> {
|
2020-10-30 13:26:19 +00:00
|
|
|
conn: xous::CID,
|
2021-04-01 17:54:17 +00:00
|
|
|
buffer: Option<Buffer<'a>>,
|
2020-10-30 13:26:19 +00:00
|
|
|
initialized: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 17:54:17 +00:00
|
|
|
impl XousLoggerBacking<'_> {
|
2020-10-30 13:26:19 +00:00
|
|
|
fn init(&mut self) -> Result<(), xous::Error> {
|
|
|
|
|
if self.initialized {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
self.conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?;
|
2021-04-01 17:54:17 +00:00
|
|
|
self.buffer = Some(Buffer::new(4000)); // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
2020-10-30 13:26:19 +00:00
|
|
|
self.initialized = true;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn log_impl(&mut self, record: &log::Record) {
|
|
|
|
|
if !self.initialized && self.init().is_err() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-01 17:54:17 +00:00
|
|
|
let mut args = String::<2800>::new();
|
|
|
|
|
write!(args, "{}", record.args()).unwrap();
|
|
|
|
|
let lr = api::LogRecord {
|
|
|
|
|
file: String::from_str(record.file().unwrap_or("")),
|
|
|
|
|
line: record.line(),
|
|
|
|
|
module: String::from_str(record.module_path().unwrap_or("")),
|
|
|
|
|
level: record.level() as u32,
|
|
|
|
|
args,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(buf) = self.buffer.as_mut() {
|
|
|
|
|
buf.rewrite(lr).unwrap();
|
|
|
|
|
buf.lend(self.conn, 0).unwrap(); // there is only one type of buffer we should be sending!
|
2020-10-30 13:26:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl log::Log for XousLogger {
|
|
|
|
|
fn enabled(&self, _metadata: &log::Metadata) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
|
while self
|
|
|
|
|
.locked
|
|
|
|
|
.compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe { XOUS_LOGGER_BACKING.log_impl(record) };
|
|
|
|
|
self.locked
|
|
|
|
|
.compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire)
|
|
|
|
|
.expect("LOG: logger became unlocked somehow");
|
|
|
|
|
}
|
|
|
|
|
fn flush(&self) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Result<(), log::SetLoggerError> {
|
|
|
|
|
log::set_logger(&XOUS_LOGGER)?;
|
|
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init_wait() -> Result<(), log::SetLoggerError> {
|
|
|
|
|
log::set_logger(&XOUS_LOGGER)?;
|
|
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
|
|
|
|
unsafe {
|
|
|
|
|
while XOUS_LOGGER_BACKING.init().is_err() {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|