2021-12-11 23:14:24 +00:00
|
|
|
use parking_lot::Mutex;
|
2021-12-11 01:14:33 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-12-11 23:14:24 +00:00
|
|
|
// Must use async_std channel to send to main thread from blocking thread
|
|
|
|
use async_std::channel::bounded as async_bounded;
|
|
|
|
use async_std::channel::Receiver as AsyncReceiver;
|
|
|
|
pub use async_std::channel::RecvError;
|
2021-12-11 01:14:33 +00:00
|
|
|
|
2021-12-11 23:14:24 +00:00
|
|
|
// Must use std mpsc so no logs are generated by async code
|
|
|
|
use std::sync::mpsc::sync_channel as std_sync_channel;
|
|
|
|
use std::sync::mpsc::SyncSender as StdSender;
|
|
|
|
use std::sync::mpsc::TrySendError as StdTrySendError;
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
2022-01-15 23:24:37 +00:00
|
|
|
#[derive(Clone)]
|
2022-03-11 00:00:59 +00:00
|
|
|
pub struct LogSafeChannelCloser {
|
2021-12-11 23:14:24 +00:00
|
|
|
sender: Arc<Mutex<Option<StdSender<String>>>>,
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 00:00:59 +00:00
|
|
|
impl LogSafeChannelCloser {
|
2021-12-11 23:14:24 +00:00
|
|
|
pub fn close(&self) {
|
|
|
|
// Drop the sender
|
|
|
|
self.sender.lock().take();
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
2021-12-11 23:14:24 +00:00
|
|
|
}
|
2021-12-11 01:14:33 +00:00
|
|
|
|
2021-12-11 23:14:24 +00:00
|
|
|
//////////////////////////////////////////
|
2022-03-11 00:00:59 +00:00
|
|
|
pub struct LogSafeChannelWriterShim {
|
2021-12-11 23:14:24 +00:00
|
|
|
sender: Arc<Mutex<Option<StdSender<String>>>>,
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 00:00:59 +00:00
|
|
|
impl std::io::Write for LogSafeChannelWriterShim {
|
2021-12-11 01:14:33 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
2021-12-11 03:04:38 +00:00
|
|
|
let bufstr = String::from_utf8_lossy(buf).to_string();
|
2021-12-11 23:14:24 +00:00
|
|
|
let sender = self.sender.lock();
|
|
|
|
if let Some(sender) = &*sender {
|
|
|
|
if let Err(e) = sender.try_send(bufstr) {
|
|
|
|
match e {
|
|
|
|
StdTrySendError::Full(_) => {
|
|
|
|
Err(std::io::Error::from(std::io::ErrorKind::WouldBlock))
|
|
|
|
}
|
|
|
|
StdTrySendError::Disconnected(_) => {
|
|
|
|
Err(std::io::Error::from(std::io::ErrorKind::BrokenPipe))
|
|
|
|
}
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
2021-12-11 23:14:24 +00:00
|
|
|
} else {
|
|
|
|
Ok(buf.len())
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-11 23:14:24 +00:00
|
|
|
Err(std::io::Error::from(std::io::ErrorKind::BrokenPipe))
|
2021-12-11 01:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 23:14:24 +00:00
|
|
|
|
2022-03-11 00:00:59 +00:00
|
|
|
pub type LogSafeChannelWriter = std::io::LineWriter<LogSafeChannelWriterShim>;
|
2021-12-11 23:14:24 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2022-01-15 23:24:37 +00:00
|
|
|
#[derive(Clone)]
|
2022-03-11 00:00:59 +00:00
|
|
|
pub struct LogSafeChannel {
|
2021-12-11 23:14:24 +00:00
|
|
|
async_receiver: AsyncReceiver<String>,
|
|
|
|
}
|
|
|
|
|
2022-03-11 00:00:59 +00:00
|
|
|
impl LogSafeChannel {
|
|
|
|
pub fn new() -> (Self, LogSafeChannelWriter, LogSafeChannelCloser) {
|
2021-12-11 23:14:24 +00:00
|
|
|
let (async_sender, async_receiver) = async_bounded(1024);
|
|
|
|
let (std_sender, std_receiver) = std_sync_channel(1024);
|
|
|
|
let shared_std_sender = Arc::new(Mutex::new(Some(std_sender)));
|
|
|
|
|
|
|
|
// Spawn a processing thread for the blocking std sender
|
|
|
|
async_std::task::spawn(async move {
|
|
|
|
#[allow(clippy::while_let_loop)]
|
|
|
|
loop {
|
|
|
|
let message = match std_receiver.recv() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
|
|
|
if async_sender.send(message).await.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(
|
|
|
|
Self { async_receiver },
|
2022-03-11 00:00:59 +00:00
|
|
|
LogSafeChannelWriter::with_capacity(
|
2021-12-11 23:14:24 +00:00
|
|
|
65536,
|
2022-03-11 00:00:59 +00:00
|
|
|
LogSafeChannelWriterShim {
|
2021-12-11 23:14:24 +00:00
|
|
|
sender: shared_std_sender.clone(),
|
|
|
|
},
|
|
|
|
),
|
2022-03-11 00:00:59 +00:00
|
|
|
LogSafeChannelCloser {
|
2021-12-11 23:14:24 +00:00
|
|
|
sender: shared_std_sender,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recv(&mut self) -> Result<String, RecvError> {
|
|
|
|
self.async_receiver.recv().await
|
|
|
|
}
|
|
|
|
}
|