From dfea96821f64f10cba709559b4066a7348fd1730 Mon Sep 17 00:00:00 2001 From: Jiahao XU <Jiahao_XU@outlook.com> Date: Sun, 19 Jun 2022 01:28:16 +1000 Subject: [PATCH] Update `UIThreadLogger` to use `std::sync::mpsc` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --- src/helpers/ui_thread_logger.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/helpers/ui_thread_logger.rs b/src/helpers/ui_thread_logger.rs index 9b7db7e5..8eb44604 100644 --- a/src/helpers/ui_thread_logger.rs +++ b/src/helpers/ui_thread_logger.rs @@ -1,22 +1,22 @@ use std::cell::Cell; use std::fmt::Write; +use std::sync::mpsc::SyncSender; use bytes::BytesMut; use log::{set_boxed_logger, set_max_level, LevelFilter, Log, Metadata, Record}; -use tokio::{runtime::Handle, sync::mpsc::Sender}; use super::ui_thread::UIRequest; #[derive(Debug)] pub(super) struct UIThreadLogger { - tx: Sender<UIRequest>, + tx: SyncSender<UIRequest>, level: LevelFilter, filter_ignore: &'static [&'static str], } impl UIThreadLogger { pub(super) fn init( - tx: Sender<UIRequest>, + tx: SyncSender<UIRequest>, level: LevelFilter, filter_ignore: &'static [&'static str], ) { @@ -25,7 +25,7 @@ impl UIThreadLogger { } fn new( - tx: Sender<UIRequest>, + tx: SyncSender<UIRequest>, level: LevelFilter, filter_ignore: &'static [&'static str], ) -> Box<Self> { @@ -36,18 +36,6 @@ impl UIThreadLogger { }) } - fn send_request(&self, request: UIRequest) { - // TODO: Use another mpsc type. - // Tokio's mpsc requires the async send to be used - // in async context. - if let Ok(handle) = Handle::try_current() { - let tx = self.tx.clone(); - handle.spawn(async move { tx.send(request).await.unwrap() }); - } else { - self.tx.blocking_send(request).unwrap(); - } - } - thread_local! { static BUFFER: Cell<BytesMut> = Cell::new(BytesMut::new()); } @@ -76,11 +64,11 @@ impl Log for UIThreadLogger { output }); - self.send_request(UIRequest::PrintToStdout(output)); + self.tx.send(UIRequest::PrintToStdout(output)).unwrap() } } fn flush(&self) { - self.send_request(UIRequest::FlushStdout); + self.tx.send(UIRequest::FlushStdout).unwrap(); } }