mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
upgrade log server to pass rich message types
This commit is contained in:
parent
85fdb7453d
commit
7210f100f4
6 changed files with 81 additions and 21 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -687,6 +687,7 @@ name = "log-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"log",
|
"log",
|
||||||
|
"rkyv 0.4.3",
|
||||||
"utralib",
|
"utralib",
|
||||||
"vergen",
|
"vergen",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ version = "0.1.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
xous = {path = "../../xous-rs"}
|
xous = {path = "../../xous-rs"}
|
||||||
xous-ipc = {path = "../../xous-ipc"}
|
xous-ipc = {path = "../../xous-ipc"}
|
||||||
|
rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false}
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
vergen = "3"
|
vergen = "3"
|
||||||
|
|
|
||||||
8
services/log-server/src/api.rs
Normal file
8
services/log-server/src/api.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub struct LogRecord {
|
||||||
|
pub file: xous_ipc::String<128>,
|
||||||
|
pub line: Option<u32>,
|
||||||
|
pub module: xous_ipc::String<128>,
|
||||||
|
pub level: u32,
|
||||||
|
pub args: xous_ipc::String<2800>,
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use xous_ipc::String;
|
use xous_ipc::{String, Buffer};
|
||||||
|
|
||||||
|
pub mod api;
|
||||||
|
|
||||||
static XOUS_LOGGER: XousLogger = XousLogger {
|
static XOUS_LOGGER: XousLogger = XousLogger {
|
||||||
locked: AtomicBool::new(false),
|
locked: AtomicBool::new(false),
|
||||||
|
|
@ -17,19 +19,19 @@ static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking {
|
||||||
buffer: None,
|
buffer: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct XousLoggerBacking {
|
struct XousLoggerBacking<'a> {
|
||||||
conn: xous::CID,
|
conn: xous::CID,
|
||||||
buffer: Option<String<4000>>, // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
buffer: Option<Buffer<'a>>,
|
||||||
initialized: bool,
|
initialized: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XousLoggerBacking {
|
impl XousLoggerBacking<'_> {
|
||||||
fn init(&mut self) -> Result<(), xous::Error> {
|
fn init(&mut self) -> Result<(), xous::Error> {
|
||||||
if self.initialized {
|
if self.initialized {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
self.conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?;
|
self.conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?;
|
||||||
self.buffer = Some(String::<4000>::new());
|
self.buffer = Some(Buffer::new(4000)); // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -38,10 +40,19 @@ impl XousLoggerBacking {
|
||||||
if !self.initialized && self.init().is_err() {
|
if !self.initialized && self.init().is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Some(ref mut buf) = self.buffer {
|
let mut args = String::<2800>::new();
|
||||||
buf.clear();
|
write!(args, "{}", record.args()).unwrap();
|
||||||
write!(buf, "{} - {}", record.level(), record.args()).unwrap();
|
let lr = api::LogRecord {
|
||||||
buf.lend(self.conn).unwrap();
|
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!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
#![cfg_attr(target_os = "none", no_main)]
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
use api::*;
|
||||||
|
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod debug;
|
mod debug;
|
||||||
|
|
@ -250,7 +253,8 @@ fn reader_thread(arg: usize) {
|
||||||
let sender = envelope.sender;
|
let sender = envelope.sender;
|
||||||
// writeln!(output, "LOG: Got message envelope: {:?}", envelope).unwrap();
|
// writeln!(output, "LOG: Got message envelope: {:?}", envelope).unwrap();
|
||||||
match &mut envelope.body {
|
match &mut envelope.body {
|
||||||
xous::Message::Scalar(msg) => handle_scalar(&mut output, sender, msg, envelope.sender.pid().unwrap()),
|
xous::Message::Scalar(msg) =>
|
||||||
|
handle_scalar(&mut output, sender, msg, envelope.sender.pid().unwrap()),
|
||||||
xous::Message::BlockingScalar(msg) => {
|
xous::Message::BlockingScalar(msg) => {
|
||||||
writeln!(
|
writeln!(
|
||||||
output,
|
output,
|
||||||
|
|
@ -274,17 +278,33 @@ fn reader_thread(arg: usize) {
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
xous::Message::Borrow(msg) => {
|
xous::Message::Borrow(_msg) => {
|
||||||
String::<4000>::from_message(msg)
|
let mem = envelope.body.memory_message().unwrap();
|
||||||
.map(|log_entry: String<4000>| writeln!(output, "{}", log_entry).unwrap())
|
let buffer = unsafe { xous_ipc::Buffer::from_memory_message(mem) };
|
||||||
.or_else(|e| {
|
let lr: LogRecord = buffer.to_original::<LogRecord, _>().unwrap();
|
||||||
writeln!(
|
let level =
|
||||||
output,
|
if log::Level::Error as u32 == lr.level {
|
||||||
"LOG: unable to convert Borrow message to str: {}",
|
"ERR " }
|
||||||
e
|
else if log::Level::Warn as u32 == lr.level {
|
||||||
)
|
"WARN" }
|
||||||
})
|
else if log::Level::Info as u32 == lr.level {
|
||||||
.ok();
|
"INFO" }
|
||||||
|
else if log::Level::Debug as u32 == lr.level {
|
||||||
|
"DBG " }
|
||||||
|
else if log::Level::Trace as u32 == lr.level {
|
||||||
|
"TRCE" }
|
||||||
|
else {
|
||||||
|
"UNKNOWN"
|
||||||
|
};
|
||||||
|
if let Some(line)= lr.line {
|
||||||
|
writeln!(output, "{}:{}: {} ({}:{})",
|
||||||
|
level, lr.module, lr.args, lr.file, line
|
||||||
|
).unwrap();
|
||||||
|
} else {
|
||||||
|
writeln!(output, "{}:{}: {} ({})",
|
||||||
|
level, lr.module, lr.args, lr.file
|
||||||
|
).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
xous::Message::MutableBorrow(msg) => {
|
xous::Message::MutableBorrow(msg) => {
|
||||||
String::<4000>::from_message(msg)
|
String::<4000>::from_message(msg)
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,25 @@ impl<'a> Buffer<'a> {
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// erase ourself and re-use our allocated storage
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn rewrite<S>(&mut self, src: S) -> core::result::Result<(), xous::Error>
|
||||||
|
where
|
||||||
|
S: rkyv::Serialize<rkyv::ser::serializers::BufferSerializer<&'a mut [u8]>>,
|
||||||
|
{
|
||||||
|
let copied_slice =
|
||||||
|
unsafe { core::slice::from_raw_parts_mut(self.slice.as_mut_ptr(), self.slice.len()) };
|
||||||
|
// zeroize the slice before using it
|
||||||
|
/*for &mut s in copied_slice {
|
||||||
|
s = 0;
|
||||||
|
}*/
|
||||||
|
let mut ser = rkyv::ser::serializers::BufferSerializer::new(copied_slice);
|
||||||
|
let pos = ser.serialize_value(&src).or(Err(())).unwrap();
|
||||||
|
self.slice = ser.into_inner();
|
||||||
|
self.offset = MemoryAddress::new(pos);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn replace<S>(&mut self, src: S) -> core::result::Result<(), &'static str>
|
pub fn replace<S>(&mut self, src: S) -> core::result::Result<(), &'static str>
|
||||||
where
|
where
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue