Update UIThreadLogger to use std::sync::mpsc

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-19 01:28:16 +10:00
parent af6a307a13
commit dfea96821f
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

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