diff --git a/src/portal.rs b/src/portal.rs index c66f6cfe..dc86725a 100644 --- a/src/portal.rs +++ b/src/portal.rs @@ -21,15 +21,14 @@ use { ptl_screencast::{add_screencast_dbus_members, ScreencastSession}, }, utils::{ - buf::Buf, clone3::{fork_with_pidfd, Forked}, copyhashmap::CopyHashMap, errorfmt::ErrorFmt, + line_logger::log_lines, numcell::NumCell, oserror::OsError, process_name::set_process_name, run_toplevel::RunToplevel, - vecdeque_ext::VecDequeExt, xrd::xrd, }, video::dmabuf::DmaBufIds, @@ -38,7 +37,6 @@ use { }, log::Level, std::{ - collections::VecDeque, rc::{Rc, Weak}, sync::Arc, }, @@ -104,25 +102,14 @@ impl PortalStartup { let ring = ring.clone(); let logger = logger.clone(); async move { - let mut buf = VecDeque::::new(); - let mut buf2 = Buf::new(1024); - let mut done = false; - while !done { - match ring.read(&self.logs, buf2.clone()).await { - Ok(n) if n > 0 => buf.extend(&buf2[..n]), - Ok(_) => done = true, - Err(e) => { - log::error!("Could not read portal logs: {}", ErrorFmt(e)); - return; - } - }; - while let Some(pos) = buf.iter().position(|b| b == &b'\n') { - let (left, right) = buf.get_slices(..pos); - logger.write_raw(left); - logger.write_raw(right); - logger.write_raw(b" (portal)\n"); - buf.drain(..=pos); - } + let res = log_lines(&ring, &self.logs, |left, right| { + logger.write_raw(left); + logger.write_raw(right); + logger.write_raw(b" (portal)\n"); + }) + .await; + if let Err(e) = res { + log::error!("Could not read portal logs: {}", ErrorFmt(e)); } } }); diff --git a/src/utils.rs b/src/utils.rs index 5764751b..6dde581e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,7 @@ pub mod errorfmt; pub mod fdcloser; pub mod gfx_api_ext; pub mod hex; +pub mod line_logger; pub mod linkedlist; pub mod log_on_drop; pub mod mmap; diff --git a/src/utils/line_logger.rs b/src/utils/line_logger.rs new file mode 100644 index 00000000..91c75d42 --- /dev/null +++ b/src/utils/line_logger.rs @@ -0,0 +1,36 @@ +use { + crate::{ + io_uring::{IoUring, IoUringError}, + utils::{buf::Buf, vecdeque_ext::VecDequeExt}, + }, + isnt::std_1::collections::IsntVecDequeExt, + std::{collections::VecDeque, rc::Rc}, + uapi::OwnedFd, +}; + +pub async fn log_lines( + ring: &IoUring, + fd: &Rc, + mut f: impl FnMut(&[u8], &[u8]), +) -> Result<(), IoUringError> { + let mut buf = VecDeque::::new(); + let mut buf2 = Buf::new(1024); + let mut done = false; + while !done { + let n = ring.read(fd, buf2.clone()).await?; + buf.extend(&buf2[..n]); + if n == 0 { + done = true; + } + while let Some(pos) = buf.iter().position(|b| b == &b'\n') { + let (left, right) = buf.get_slices(..pos); + f(left, right); + buf.drain(..=pos); + } + } + if buf.is_not_empty() { + let (left, right) = buf.as_slices(); + f(left, right); + } + Ok(()) +} diff --git a/src/xwayland.rs b/src/xwayland.rs index af43e885..e98199f8 100644 --- a/src/xwayland.rs +++ b/src/xwayland.rs @@ -18,7 +18,7 @@ use { io_uring::IoUringError, state::State, user_session::import_environment, - utils::{buf::Buf, errorfmt::ErrorFmt, oserror::OsError}, + utils::{errorfmt::ErrorFmt, line_logger::log_lines, oserror::OsError}, wire::WlSurfaceId, xcon::XconError, xwayland::{ @@ -217,29 +217,12 @@ pub fn build_args() -> (String, Vec) { async fn log_xwayland(state: Rc, stderr: OwnedFd) { let stderr = Rc::new(stderr); - let mut buf = vec![]; - let mut buf2 = Buf::new(128); - let mut done = false; - while !done { - loop { - match state.ring.read(&stderr, buf2.clone()).await { - Ok(n) if n > 0 => { - buf.extend_from_slice(&buf2[..n]); - } - Ok(_) => { - done = true; - break; - } - Err(e) => { - log::error!("Could not read from stderr fd: {}", ErrorFmt(e)); - return; - } - } - } - for line in buf.lines() { - log::info!("Xwayland: {}", line.as_bstr()); - } - buf.clear(); + let res = log_lines(&state.ring, &stderr, |left, right| { + log::info!("Xwayland: {}{}", left.as_bstr(), right.as_bstr()); + }) + .await; + if let Err(e) = res { + log::error!("Could not read from stderr fd: {}", ErrorFmt(e)); } }