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

@ -22,14 +22,18 @@ struct ConnectRequest {
#[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>,
} }
@ -37,7 +41,7 @@ pub struct UsbString {
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.
@ -120,7 +124,8 @@ fn reader_thread(arg: usize) {
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,7 +170,12 @@ 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")]