more logging

This commit is contained in:
Ashley Wulber 2023-07-01 02:01:47 -04:00 committed by Ashley Wulber
parent 2f7255ea1b
commit c4566730ab

View file

@ -1,9 +1,3 @@
use tokio::{
io::{AsyncBufReadExt, BufReader},
net::UnixStream,
sync::oneshot,
};
use tracing::{error, info};
use cosmic::{ use cosmic::{
iced::{ iced::{
futures::{self, SinkExt}, futures::{self, SinkExt},
@ -12,8 +6,14 @@ use cosmic::{
iced_futures::Subscription, iced_futures::Subscription,
}; };
use cosmic_notifications_util::AppletEvent; use cosmic_notifications_util::AppletEvent;
use std::os::unix::io::{FromRawFd, RawFd};
use sendfd::RecvWithFd; use sendfd::RecvWithFd;
use std::os::unix::io::{FromRawFd, RawFd};
use tokio::{
io::{self, AsyncBufReadExt, BufReader},
net::UnixStream,
sync::oneshot,
};
use tracing::{error, info, warn};
#[derive(Debug)] #[derive(Debug)]
pub enum State { pub enum State {
@ -42,7 +42,7 @@ pub fn notifications() -> Subscription<AppletEvent> {
std::thread::spawn(move || -> anyhow::Result<()> { std::thread::spawn(move || -> anyhow::Result<()> {
let mut msg = String::new(); let mut msg = String::new();
std::io::stdin().read_line(&mut msg)?; std::io::stdin().read_line(&mut msg)?;
let raw_fd = msg.trim().parse::<RawFd>()?; let raw_fd = msg.trim().parse::<RawFd>()?;
if raw_fd == 0 { if raw_fd == 0 {
anyhow::bail!("Invalid fd received from panel"); anyhow::bail!("Invalid fd received from panel");
} }
@ -63,10 +63,9 @@ pub fn notifications() -> Subscription<AppletEvent> {
continue; continue;
}; };
state = State::WaitingForDaemon(stream); state = State::WaitingForDaemon(stream);
} }
State::WaitingForDaemon(stream) => { State::WaitingForDaemon(stream) => {
info!("Waiting for daemon to send us a stream"); info!("Waiting for panel to send us a stream");
if let Err(err) = stream.readable().await { if let Err(err) = stream.readable().await {
error!("Failed to wait for stream to be readable {}", err); error!("Failed to wait for stream to be readable {}", err);
state = State::Finished; state = State::Finished;
@ -79,13 +78,24 @@ pub fn notifications() -> Subscription<AppletEvent> {
match stream.recv_with_fd(&mut buf, &mut fd_buf) { match stream.recv_with_fd(&mut buf, &mut fd_buf) {
Ok((data_cnt, fd_cnt)) => { Ok((data_cnt, fd_cnt)) => {
if data_cnt != 4 || fd_cnt != 1 { if data_cnt == 0 && fd_cnt == 0 {
error!("Invalid data received from panel"); warn!("Received EOF from panel");
state = State::Finished; state = State::Finished;
continue; continue;
} }
let notif_stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(fd_buf[0]) }; if data_cnt != 4 || fd_cnt != 1 {
error!(
"Invalid data received from panel {} {}",
data_cnt, fd_cnt
);
state = State::Finished;
continue;
}
let notif_stream = unsafe {
std::os::unix::net::UnixStream::from_raw_fd(fd_buf[0])
};
let Ok(notif_stream) = UnixStream::from_std(notif_stream) else { let Ok(notif_stream) = UnixStream::from_std(notif_stream) else {
error!("Failed to convert raw fd to unix stream"); error!("Failed to convert raw fd to unix stream");
state = State::Finished; state = State::Finished;
@ -94,6 +104,9 @@ pub fn notifications() -> Subscription<AppletEvent> {
state = State::WaitingForNotificationEvent(notif_stream); state = State::WaitingForNotificationEvent(notif_stream);
} }
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue;
}
Err(err) => { Err(err) => {
error!("Failed to receive fd from panel: {}", err); error!("Failed to receive fd from panel: {}", err);
state = State::Finished; state = State::Finished;
@ -103,6 +116,7 @@ pub fn notifications() -> Subscription<AppletEvent> {
} }
} }
State::WaitingForNotificationEvent(stream) => { State::WaitingForNotificationEvent(stream) => {
info!("Waiting for notification event");
let mut reader = BufReader::new(stream); let mut reader = BufReader::new(stream);
// todo read messages // todo read messages
let mut request_buf = String::with_capacity(1024); let mut request_buf = String::with_capacity(1024);
@ -124,6 +138,6 @@ pub fn notifications() -> Subscription<AppletEvent> {
} }
} }
} }
} },
) )
} }