xous-log: stdout: insert \r when printing \n

When printing messages to stdout or stderr, insert `\r` whenever we
encounter `\n`.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-12-29 19:01:19 +08:00
parent 9a1191c0d2
commit 061abb05db

View file

@ -12,32 +12,36 @@ use num_traits::FromPrimitive;
use platform::implementation; use platform::implementation;
/// A page-aligned stack allocation for connection requests (used by USB resolver) /// A page-aligned stack allocation for connection requests (used by USB resolver)
#[cfg(feature="usb")] #[cfg(feature = "usb")]
#[repr(C, align(4096))] #[repr(C, align(4096))]
struct ConnectRequest { struct ConnectRequest {
name: [u8; 64], name: [u8; 64],
len: u32, len: u32,
_padding: [u8; 4096 - 4 - 64], _padding: [u8; 4096 - 4 - 64],
} }
#[cfg(feature="usb")] #[cfg(feature = "usb")]
impl Default for ConnectRequest { impl Default for ConnectRequest {
fn default() -> Self { fn default() -> Self {
ConnectRequest { name: [0u8; 64], len: 0, _padding: [0u8; 4096 - 4 - 64] } ConnectRequest {
name: [0u8; 64],
len: 0,
_padding: [0u8; 4096 - 4 - 64],
}
} }
} }
#[cfg(feature="usb")] #[cfg(feature = "usb")]
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct UsbString { pub struct UsbString {
pub s: xous_ipc::String::<4000>, pub s: xous_ipc::String<4000>,
pub sent: Option<u32>, pub sent: Option<u32>,
} }
#[cfg(feature="usb")] #[cfg(feature = "usb")]
fn usb_send_str(conn: xous::CID, s: &str) { fn usb_send_str(conn: xous::CID, s: &str) {
let serializer = UsbString { let serializer = UsbString {
s: xous_ipc::String::<4000>::from_str(s), s: xous_ipc::String::<4000>::from_str(s),
sent: None sent: None,
}; };
let buf = xous_ipc::Buffer::into_buf(serializer).expect("usb error"); let buf = xous_ipc::Buffer::into_buf(serializer).expect("usb error");
// failures to send are silent & ignored; also, this API doesn't block. // failures to send are silent & ignored; also, this API doesn't block.
@ -49,10 +53,10 @@ fn reader_thread(arg: usize) {
writeln!(output, "LOG: Xous Logging Server starting up...").ok(); writeln!(output, "LOG: Xous Logging Server starting up...").ok();
let server_addr = xous::create_server_with_address(b"xous-log-server ").expect("create server"); let server_addr = xous::create_server_with_address(b"xous-log-server ").expect("create server");
writeln!(output, "LOG: Server listening on address {:?}", server_addr).ok(); writeln!(output, "LOG: Server listening on address {:?}", server_addr).ok();
#[cfg(feature="usb")] #[cfg(feature = "usb")]
let mut usb_serial: Option<xous::CID> = None; let mut usb_serial: Option<xous::CID> = None;
// use a stack-allocated string to ensure no heap thrashing results from String manipulations // use a stack-allocated string to ensure no heap thrashing results from String manipulations
#[cfg(feature="usb")] #[cfg(feature = "usb")]
let mut usb_str = xous_ipc::String::<4000>::new(); let mut usb_str = xous_ipc::String::<4000>::new();
println!("LOG: my PID is {}", xous::process::id()); println!("LOG: my PID is {}", xous::process::id());
@ -118,9 +122,10 @@ fn reader_thread(arg: usize) {
write!(output, ":{}", line.get()).ok(); write!(output, ":{}", line.get()).ok();
} }
writeln!(output, ")").ok(); writeln!(output, ")").ok();
#[cfg(feature="usb")] #[cfg(feature = "usb")]
if let Some(conn) = usb_serial { if let Some(conn) = usb_serial {
if usb_str.len() > 0 { // test length so we aren't constantly copying 4096 bytes of 0's clearing an already cleared structure. if usb_str.len() > 0 {
// test length so we aren't constantly copying 4096 bytes of 0's clearing an already cleared structure.
usb_str.clear(); usb_str.clear();
} }
// duplicate the above code because doing repeated calls to the USB stack is inefficient // duplicate the above code because doing repeated calls to the USB stack is inefficient
@ -165,10 +170,15 @@ fn reader_thread(arg: usize) {
buffer_length, buffer_length,
) )
}; };
output.write_all(buffer).unwrap(); for c in buffer {
if *c == b'\n' {
output.putc(b'\r');
}
output.putc(*c);
}
// TODO: If the buffer is mutable, set `length` to 0. // TODO: If the buffer is mutable, set `length` to 0.
#[cfg(feature="usb")] #[cfg(feature = "usb")]
if let Some(conn) = usb_serial { if let Some(conn) = usb_serial {
// safety: this routine will just blow up if you try to pass non utf-8 to it, // safety: this routine will just blow up if you try to pass non utf-8 to it,
// so it's not very safe. On the other hand, it's fast and shame on you for sending // so it's not very safe. On the other hand, it's fast and shame on you for sending