mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
log-server: add first draft of logging server
This server implements `log`, and comes with a `lib` that allows for setting up logging. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
4650e865fb
commit
e18219b078
3 changed files with 155 additions and 35 deletions
|
|
@ -7,6 +7,7 @@ description = "Log output program"
|
|||
|
||||
[dependencies]
|
||||
xous = { path = "../../xous-rs" }
|
||||
log = "0.4"
|
||||
|
||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = { path = "../../utralib"}
|
||||
|
|
|
|||
86
services/log-server/src/lib.rs
Normal file
86
services/log-server/src/lib.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
use core::fmt::Write;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use xous::String;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
struct XousLoggerBacking {
|
||||
conn: xous::CID,
|
||||
buffer: Option<String<'static>>,
|
||||
initialized: bool,
|
||||
}
|
||||
|
||||
impl XousLoggerBacking {
|
||||
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())?;
|
||||
self.buffer = Some(String::new(4096));
|
||||
self.initialized = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn log_impl(&mut self, record: &log::Record) {
|
||||
if !self.initialized && self.init().is_err() {
|
||||
return;
|
||||
}
|
||||
if let Some(ref mut buf) = self.buffer {
|
||||
buf.clear();
|
||||
write!(buf, "{} - {}", record.level(), record.args()).unwrap();
|
||||
buf.lend(self.conn, 1).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
|
@ -5,10 +5,8 @@
|
|||
#[macro_use]
|
||||
mod debug;
|
||||
|
||||
mod log_string;
|
||||
|
||||
use core::fmt::Write;
|
||||
use log_string::LogString;
|
||||
use xous::String;
|
||||
|
||||
#[cfg(not(target_os = "none"))]
|
||||
mod implementation {
|
||||
|
|
@ -119,8 +117,12 @@ mod implementation {
|
|||
crate::debug::DEFAULT.enable_rx();
|
||||
|
||||
println!("Allocating IRQ...");
|
||||
xous::syscall::claim_interrupt(utra::console::CONSOLE_IRQ, handle_irq, core::ptr::null_mut::<usize>())
|
||||
.expect("couldn't claim interrupt");
|
||||
xous::syscall::claim_interrupt(
|
||||
utra::console::CONSOLE_IRQ,
|
||||
handle_irq,
|
||||
core::ptr::null_mut::<usize>(),
|
||||
)
|
||||
.expect("couldn't claim interrupt");
|
||||
println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ);
|
||||
Output {
|
||||
// addr: uart.as_mut_ptr() as usize,
|
||||
|
|
@ -129,7 +131,7 @@ mod implementation {
|
|||
|
||||
impl Output {
|
||||
pub fn get_writer(&self) -> OutputWriter {
|
||||
OutputWriter { }
|
||||
OutputWriter {}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
|
|
@ -172,12 +174,11 @@ mod implementation {
|
|||
println!();
|
||||
}
|
||||
|
||||
pub struct OutputWriter {
|
||||
}
|
||||
pub struct OutputWriter {}
|
||||
|
||||
impl OutputWriter {
|
||||
pub fn putc(&self, c: u8) {
|
||||
let mut uart_csr = CSR::new(unsafe{ crate::debug::DEFAULT_UART_ADDR as *mut u32});
|
||||
let mut uart_csr = CSR::new(unsafe { crate::debug::DEFAULT_UART_ADDR as *mut u32 });
|
||||
|
||||
// Wait until TXFULL is `0`
|
||||
while uart_csr.r(utra::uart::TXFULL) != 0 {}
|
||||
|
|
@ -211,44 +212,76 @@ fn reader_thread(mut output: implementation::OutputWriter) {
|
|||
writeln!(output, "LOG: Counter tick: {}", counter).unwrap();
|
||||
}
|
||||
counter += 1;
|
||||
writeln!(output, "LOG: Waiting for an event...").unwrap();
|
||||
// writeln!(output, "LOG: Waiting for an event...").unwrap();
|
||||
let mut envelope =
|
||||
xous::syscall::receive_message(server_addr).expect("couldn't get address");
|
||||
writeln!(output, "LOG: Got message envelope: {:?}", envelope).unwrap();
|
||||
let sender = envelope.sender;
|
||||
// writeln!(output, "LOG: Got message envelope: {:?}", envelope).unwrap();
|
||||
match &mut envelope.body {
|
||||
xous::Message::Scalar(msg) => {
|
||||
writeln!(output, "LOG: Scalar message from {}: {:?}", envelope.sender, msg).unwrap();
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Scalar message from {}: {:?}",
|
||||
envelope.sender, msg
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
xous::Message::BlockingScalar(msg) => {
|
||||
writeln!(output, "LOG: BlockingScalar message from {}: {:?}", envelope.sender, msg).unwrap();
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: BlockingScalar message from {}: {:?}",
|
||||
envelope.sender, msg
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
xous::Message::Move(msg) => {
|
||||
let log_entry = LogString::from_message(msg);
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Moved log message from {}: {}",
|
||||
envelope.sender, log_entry
|
||||
)
|
||||
.unwrap();
|
||||
String::from_message(msg)
|
||||
.map(|log_entry| {
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Moved log message from {}: {}",
|
||||
sender, log_entry
|
||||
)
|
||||
.unwrap()
|
||||
})
|
||||
.or_else(|e| {
|
||||
writeln!(output, "LOG: unable to convert Move message to str: {}", e)
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
xous::Message::Borrow(msg) => {
|
||||
let log_entry = LogString::from_message(msg);
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Immutably borrowed log message from {}: {}",
|
||||
envelope.sender, log_entry
|
||||
)
|
||||
.unwrap();
|
||||
String::from_message(msg)
|
||||
.map(|log_entry| writeln!(output, "{}", log_entry).unwrap())
|
||||
.or_else(|e| {
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: unable to convert Borrow message to str: {}",
|
||||
e
|
||||
)
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
xous::Message::MutableBorrow(msg) => {
|
||||
let mut log_entry = LogString::from_message(msg);
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Mutable borrowed log message from {} len {}:\n\r {}\n\r",
|
||||
envelope.sender, log_entry.len, log_entry.s,
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(log_entry, " << HELLO FROM THE SERVER").unwrap();
|
||||
String::from_message(msg)
|
||||
.map(|mut log_entry| {
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: Mutable borrowed log message from {} len {}:\n\r {}\n\r",
|
||||
sender,
|
||||
log_entry.len(),
|
||||
log_entry,
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(log_entry, " << HELLO FROM THE SERVER").unwrap();
|
||||
})
|
||||
.or_else(|e| {
|
||||
writeln!(
|
||||
output,
|
||||
"LOG: unable to convert MutableBorrow message to str: {}",
|
||||
e
|
||||
)
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue