mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-16 07:36:38 +00:00
Update UIThreadLogger
to use std::sync::mpsc
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
af6a307a13
commit
dfea96821f
1 changed files with 6 additions and 18 deletions
|
@ -1,22 +1,22 @@
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use std::sync::mpsc::SyncSender;
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use log::{set_boxed_logger, set_max_level, LevelFilter, Log, Metadata, Record};
|
use log::{set_boxed_logger, set_max_level, LevelFilter, Log, Metadata, Record};
|
||||||
use tokio::{runtime::Handle, sync::mpsc::Sender};
|
|
||||||
|
|
||||||
use super::ui_thread::UIRequest;
|
use super::ui_thread::UIRequest;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) struct UIThreadLogger {
|
pub(super) struct UIThreadLogger {
|
||||||
tx: Sender<UIRequest>,
|
tx: SyncSender<UIRequest>,
|
||||||
level: LevelFilter,
|
level: LevelFilter,
|
||||||
filter_ignore: &'static [&'static str],
|
filter_ignore: &'static [&'static str],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UIThreadLogger {
|
impl UIThreadLogger {
|
||||||
pub(super) fn init(
|
pub(super) fn init(
|
||||||
tx: Sender<UIRequest>,
|
tx: SyncSender<UIRequest>,
|
||||||
level: LevelFilter,
|
level: LevelFilter,
|
||||||
filter_ignore: &'static [&'static str],
|
filter_ignore: &'static [&'static str],
|
||||||
) {
|
) {
|
||||||
|
@ -25,7 +25,7 @@ impl UIThreadLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new(
|
fn new(
|
||||||
tx: Sender<UIRequest>,
|
tx: SyncSender<UIRequest>,
|
||||||
level: LevelFilter,
|
level: LevelFilter,
|
||||||
filter_ignore: &'static [&'static str],
|
filter_ignore: &'static [&'static str],
|
||||||
) -> Box<Self> {
|
) -> 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! {
|
thread_local! {
|
||||||
static BUFFER: Cell<BytesMut> = Cell::new(BytesMut::new());
|
static BUFFER: Cell<BytesMut> = Cell::new(BytesMut::new());
|
||||||
}
|
}
|
||||||
|
@ -76,11 +64,11 @@ impl Log for UIThreadLogger {
|
||||||
output
|
output
|
||||||
});
|
});
|
||||||
|
|
||||||
self.send_request(UIRequest::PrintToStdout(output));
|
self.tx.send(UIRequest::PrintToStdout(output)).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&self) {
|
fn flush(&self) {
|
||||||
self.send_request(UIRequest::FlushStdout);
|
self.tx.send(UIRequest::FlushStdout).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue