format xous-api-log

This commit is contained in:
bunnie 2024-01-14 18:50:49 +08:00
parent a9803af0c2
commit 65aef1c886
3 changed files with 23 additions and 31 deletions

View file

@ -12,7 +12,16 @@ pub struct LogRecord {
impl Default for LogRecord { impl Default for LogRecord {
fn default() -> Self { fn default() -> Self {
LogRecord { file_length: 0, file: [0u8; 128], line: None, module_length: 0, module: [0u8; 128], level: 0, args_length: 0, args: [0u8; 3820] } LogRecord {
file_length: 0,
file: [0u8; 128],
line: None,
module_length: 0,
module: [0u8; 128],
level: 0,
args_length: 0,
args: [0u8; 3820],
}
} }
} }
@ -30,7 +39,8 @@ pub enum Opcode {
/// A `xous::StringBuffer` containing this program's name /// A `xous::StringBuffer` containing this program's name
ProgramName = 3, ProgramName = 3,
/// Try to log console output to a USB serial port. Best-effort only; failures will not crash, will not be noted /// Try to log console output to a USB serial port. Best-effort only; failures will not crash, will not
/// be noted
TryHookUsbMirror = 4, TryHookUsbMirror = 4,
UnhookUsbMirror = 5, UnhookUsbMirror = 5,

View file

@ -6,15 +6,9 @@ pub struct BufferWrapper<'a> {
} }
impl<'a> BufferWrapper<'a> { impl<'a> BufferWrapper<'a> {
pub fn new(buf: &'a mut [u8]) -> Self { pub fn new(buf: &'a mut [u8]) -> Self { BufferWrapper { buf, offset: 0 } }
BufferWrapper {
buf: buf, pub fn len(&self) -> usize { self.offset }
offset: 0,
}
}
pub fn len(&self) -> usize {
self.offset
}
} }
impl<'a> core::fmt::Write for BufferWrapper<'a> { impl<'a> core::fmt::Write for BufferWrapper<'a> {

View file

@ -1,6 +1,7 @@
#![cfg_attr(any(target_os = "none", feature = "nostd"), no_std)] #![cfg_attr(any(target_os = "none", feature = "nostd"), no_std)]
use core::fmt::Write; use core::fmt::Write;
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
pub mod api; pub mod api;
@ -53,12 +54,7 @@ impl XousLogger {
xous::send_message( xous::send_message(
XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed), XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed),
xous::Message::new_lend( xous::Message::new_lend(crate::api::Opcode::LogRecord.to_usize().unwrap(), buf, None, None),
crate::api::Opcode::LogRecord.to_usize().unwrap(),
buf,
None,
None,
),
) )
.unwrap(); .unwrap();
} }
@ -66,20 +62,17 @@ impl XousLogger {
fn resume(&self) { fn resume(&self) {
xous::send_message( xous::send_message(
XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed), XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed),
xous::Message::new_scalar(2000, 0, 0, 0, 0), // logger is one of the few servers that uses special, non-encoded message IDs. xous::Message::new_scalar(2000, 0, 0, 0, 0), /* logger is one of the few servers that uses
* special, non-encoded message IDs. */
) )
.expect("couldn't send resume message to the logger implementation"); .expect("couldn't send resume message to the logger implementation");
} }
} }
impl log::Log for XousLogger { impl log::Log for XousLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool { fn enabled(&self, _metadata: &log::Metadata) -> bool { true }
true
}
fn log(&self, record: &log::Record) { fn log(&self, record: &log::Record) { XOUS_LOGGER.log_impl(record); }
XOUS_LOGGER.log_impl(record);
}
fn flush(&self) {} fn flush(&self) {}
} }
@ -98,15 +91,10 @@ pub fn init() -> Result<(), LogError> {
pub fn init_wait() -> Result<(), ()> { pub fn init_wait() -> Result<(), ()> {
let sid = xous::SID::from_bytes(b"xous-log-server ").unwrap(); let sid = xous::SID::from_bytes(b"xous-log-server ").unwrap();
let cid = xous::connect(sid).or(Err(()))?; let cid = xous::connect(sid).or(Err(()))?;
XOUS_LOGGER_CONNECTION.store( XOUS_LOGGER_CONNECTION.store(cid, Ordering::Relaxed);
cid,
Ordering::Relaxed,
);
log::set_logger(&XOUS_LOGGER).or(Err(()))?; log::set_logger(&XOUS_LOGGER).or(Err(()))?;
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
Ok(()) Ok(())
} }
pub fn resume() { pub fn resume() { XOUS_LOGGER.resume(); }
XOUS_LOGGER.resume();
}