mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
fix logger thread shutdown system
This commit is contained in:
parent
39395c797a
commit
0b760aadf7
@ -57,19 +57,17 @@ pub fn run(
|
|||||||
question_policy: QuestionPolicy,
|
question_policy: QuestionPolicy,
|
||||||
file_visibility_policy: FileVisibilityPolicy,
|
file_visibility_policy: FileVisibilityPolicy,
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
let log_receiver = setup_channel();
|
let (log_receiver, dropper) = setup_channel();
|
||||||
|
|
||||||
let synchronization_pair = Arc::new((Mutex::new(false), Condvar::new()));
|
let synchronization_pair = Arc::new((Mutex::new(false), Condvar::new()));
|
||||||
spawn_logger_thread(log_receiver, synchronization_pair.clone());
|
spawn_logger_thread(log_receiver, synchronization_pair.clone());
|
||||||
run_cmd(args, question_policy, file_visibility_policy)?;
|
run_cmd(args, question_policy, file_visibility_policy)?;
|
||||||
|
|
||||||
// Drop our sender so when all threads are done, no clones are left.
|
// Send a message for the logger thread to shut down.
|
||||||
// This is needed, otherwise the logging thread will never exit since we would be keeping a
|
// This is needed, otherwise the logging thread will never exit.
|
||||||
// sender alive here.
|
drop(dropper);
|
||||||
todo!();
|
|
||||||
|
|
||||||
// Prevent the main thread from exiting until the background thread handling the
|
// Hold the main thread from exiting until the background thread signals its shutdown.
|
||||||
// logging has set `flushed` to true.
|
|
||||||
let (lock, cvar) = &*synchronization_pair;
|
let (lock, cvar) = &*synchronization_pair;
|
||||||
let guard = lock.lock().unwrap();
|
let guard = lock.lock().unwrap();
|
||||||
let _flushed = cvar.wait(guard).unwrap();
|
let _flushed = cvar.wait(guard).unwrap();
|
||||||
@ -257,38 +255,36 @@ fn run_cmd(
|
|||||||
|
|
||||||
fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex<bool>, Condvar)>) {
|
fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex<bool>, Condvar)>) {
|
||||||
rayon::spawn(move || {
|
rayon::spawn(move || {
|
||||||
const BUFFER_SIZE: usize = 10;
|
const BUFFER_CAPACITY: usize = 10;
|
||||||
let mut buffer = Vec::<String>::with_capacity(BUFFER_SIZE);
|
let mut buffer = Vec::<String>::with_capacity(BUFFER_CAPACITY);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let msg = log_receiver.recv();
|
let msg = log_receiver.recv().expect("Failed to receive log message");
|
||||||
|
|
||||||
// Senders are still active
|
let is_shutdown_message = msg.is_none();
|
||||||
if let Ok(msg) = msg {
|
|
||||||
// Print messages if buffer is full otherwise append to it
|
|
||||||
if buffer.len() == BUFFER_SIZE {
|
|
||||||
let mut tmp = buffer.join("\n");
|
|
||||||
|
|
||||||
if let Some(msg) = map_message(&msg) {
|
// Append message to buffer
|
||||||
tmp.push_str(&msg);
|
if let Some(msg) = msg.as_ref().and_then(map_message) {
|
||||||
}
|
buffer.push(msg);
|
||||||
|
}
|
||||||
|
|
||||||
eprintln!("{}", tmp);
|
let should_flush = buffer.len() == BUFFER_CAPACITY || is_shutdown_message;
|
||||||
buffer.clear();
|
|
||||||
} else if let Some(msg) = map_message(&msg) {
|
|
||||||
buffer.push(msg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// All senders have been dropped
|
|
||||||
eprintln!("{}", buffer.join("\n"));
|
|
||||||
|
|
||||||
// Wake up the main thread
|
if should_flush {
|
||||||
let (lock, cvar) = &*synchronization_pair;
|
let text = buffer.join("\n");
|
||||||
let mut flushed = lock.lock().unwrap();
|
eprintln!("{text}");
|
||||||
*flushed = true;
|
buffer.clear();
|
||||||
cvar.notify_one();
|
}
|
||||||
|
|
||||||
|
if is_shutdown_message {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wake up the main thread
|
||||||
|
let (lock, cvar) = &*synchronization_pair;
|
||||||
|
let mut flushed = lock.lock().unwrap();
|
||||||
|
*flushed = true;
|
||||||
|
cvar.notify_one();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,15 @@ use std::sync::{mpsc, OnceLock};
|
|||||||
use super::colors::{ORANGE, RESET, YELLOW};
|
use super::colors::{ORANGE, RESET, YELLOW};
|
||||||
use crate::accessible::is_running_in_accessible_mode;
|
use crate::accessible::is_running_in_accessible_mode;
|
||||||
|
|
||||||
pub type LogReceiver = mpsc::Receiver<PrintMessage>;
|
pub type LogReceiver = mpsc::Receiver<Option<PrintMessage>>;
|
||||||
type LogSender = mpsc::Sender<PrintMessage>;
|
type LogSender = mpsc::Sender<Option<PrintMessage>>;
|
||||||
|
|
||||||
static SENDER: OnceLock<LogSender> = OnceLock::new();
|
static SENDER: OnceLock<LogSender> = OnceLock::new();
|
||||||
|
|
||||||
pub fn setup_channel() -> LogReceiver {
|
pub fn setup_channel() -> (LogReceiver, LoggerDropper) {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
SENDER.set(tx).expect("`setup_channel` should only be called once");
|
SENDER.set(tx).expect("`setup_channel` should only be called once");
|
||||||
rx
|
(rx, LoggerDropper)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
@ -60,6 +60,7 @@ pub fn map_message(msg: &PrintMessage) -> Option<String> {
|
|||||||
/// `is_running_in_accessible_mode`.
|
/// `is_running_in_accessible_mode`.
|
||||||
///
|
///
|
||||||
/// Read more about accessibility mode in `accessible.rs`.
|
/// Read more about accessibility mode in `accessible.rs`.
|
||||||
|
#[track_caller]
|
||||||
pub fn info(contents: String) {
|
pub fn info(contents: String) {
|
||||||
info_with_accessibility(contents, false);
|
info_with_accessibility(contents, false);
|
||||||
}
|
}
|
||||||
@ -70,10 +71,12 @@ pub fn info(contents: String) {
|
|||||||
/// returns `true`.
|
/// returns `true`.
|
||||||
///
|
///
|
||||||
/// Read more about accessibility mode in `accessible.rs`.
|
/// Read more about accessibility mode in `accessible.rs`.
|
||||||
|
#[track_caller]
|
||||||
pub fn info_accessible(contents: String) {
|
pub fn info_accessible(contents: String) {
|
||||||
info_with_accessibility(contents, true);
|
info_with_accessibility(contents, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
fn info_with_accessibility(contents: String, accessible: bool) {
|
fn info_with_accessibility(contents: String, accessible: bool) {
|
||||||
send_log_message(PrintMessage {
|
send_log_message(PrintMessage {
|
||||||
contents,
|
contents,
|
||||||
@ -97,6 +100,20 @@ pub enum MessageLevel {
|
|||||||
Warning,
|
Warning,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
fn send_log_message(msg: PrintMessage) {
|
fn send_log_message(msg: PrintMessage) {
|
||||||
|
send_message(Some(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn send_message(msg: Option<PrintMessage>) {
|
||||||
get_sender().send(msg).expect("Failed to send internal message");
|
get_sender().send(msg).expect("Failed to send internal message");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct LoggerDropper;
|
||||||
|
|
||||||
|
impl Drop for LoggerDropper {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
send_message(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user