1
0
Fork 0
forked from wry/wry

xwayland: fix logging of stderr

This commit is contained in:
Julian Orth 2024-03-23 08:58:35 +01:00
parent b8fedbd23c
commit 544f23ffb0
4 changed files with 53 additions and 46 deletions

View file

@ -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::<u8>::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));
}
}
});

View file

@ -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;

36
src/utils/line_logger.rs Normal file
View file

@ -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<OwnedFd>,
mut f: impl FnMut(&[u8], &[u8]),
) -> Result<(), IoUringError> {
let mut buf = VecDeque::<u8>::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(())
}

View file

@ -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<String>) {
async fn log_xwayland(state: Rc<State>, 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));
}
}