1 use std::sync::{mpsc, Arc, Barrier, OnceLock};
3 pub use logger_thread::spawn_logger_thread;
5 use super::colors::{ORANGE, RESET, YELLOW};
6 use crate::accessible::is_running_in_accessible_mode;
8 /// Asks logger to shutdown and waits till it flushes all pending messages.
10 pub fn shutdown_logger_and_wait() {
11 logger_thread::send_shutdown_command_and_wait();
14 /// Asks logger to flush all messages, useful before starting STDIN interaction.
16 pub fn flush_messages() {
17 logger_thread::send_flush_command_and_wait();
20 /// An `[INFO]` log to be displayed if we're not running accessibility mode.
22 /// Same as `.info_accessible()`, but only displayed if accessibility mode
23 /// is turned off, which is detected by the function
24 /// `is_running_in_accessible_mode`.
26 /// Read more about accessibility mode in `accessible.rs`.
28 pub fn info(contents: String) {
29 info_with_accessibility(contents, false);
32 /// An `[INFO]` log to be displayed.
34 /// Same as `.info()`, but also displays if `is_running_in_accessible_mode`
37 /// Read more about accessibility mode in `accessible.rs`.
39 pub fn info_accessible(contents: String) {
40 info_with_accessibility(contents, true);
44 fn info_with_accessibility(contents: String, accessible: bool) {
45 logger_thread::send_print_command(PrintMessage {
48 level: MessageLevel::Info,
53 pub fn warning(contents: String) {
54 logger_thread::send_print_command(PrintMessage {
56 // Warnings are important and unlikely to flood, so they should be displayed
58 level: MessageLevel::Warning,
65 Flush { finished_barrier: Arc<Barrier> },
66 FlushAndShutdown { finished_barrier: Arc<Barrier> },
69 /// Message object used for sending logs from worker threads to a logging thread via channels.
70 /// See <https://github.com/ouch-org/ouch/issues/643>
79 fn to_formatted_message(&self) -> Option<String> {
81 MessageLevel::Info => {
83 if is_running_in_accessible_mode() {
84 Some(format!("{}Info:{} {}", *YELLOW, *RESET, self.contents))
86 Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
88 } else if !is_running_in_accessible_mode() {
89 Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
94 MessageLevel::Warning => {
95 if is_running_in_accessible_mode() {
96 Some(format!("{}Warning:{} {}", *ORANGE, *RESET, self.contents))
98 Some(format!("{}[WARNING]{} {}", *ORANGE, *RESET, self.contents))
105 #[derive(Debug, PartialEq)]
113 sync::{mpsc::RecvTimeoutError, Arc, Barrier},
119 type LogReceiver = mpsc::Receiver<LoggerCommand>;
120 type LogSender = mpsc::Sender<LoggerCommand>;
122 static SENDER: OnceLock<LogSender> = OnceLock::new();
125 fn setup_channel() -> LogReceiver {
126 let (tx, rx) = mpsc::channel();
127 SENDER.set(tx).expect("`setup_channel` should only be called once");
132 fn get_sender() -> &'static LogSender {
133 SENDER.get().expect("No sender, you need to call `setup_channel` first")
137 pub(super) fn send_print_command(msg: PrintMessage) {
139 .send(LoggerCommand::Print(msg))
140 .expect("Failed to send print command");
144 pub(super) fn send_flush_command_and_wait() {
145 let barrier = Arc::new(Barrier::new(2));
148 .send(LoggerCommand::Flush {
149 finished_barrier: barrier.clone(),
151 .expect("Failed to send flush command");
157 pub(super) fn send_shutdown_command_and_wait() {
158 let barrier = Arc::new(Barrier::new(2));
161 .send(LoggerCommand::FlushAndShutdown {
162 finished_barrier: barrier.clone(),
164 .expect("Failed to send shutdown command");
169 pub fn spawn_logger_thread() {
170 let log_receiver = setup_channel();
171 rayon::spawn(move || run_logger(log_receiver));
174 fn run_logger(log_receiver: LogReceiver) {
175 const FLUSH_TIMEOUT: Duration = Duration::from_millis(200);
177 let mut buffer = Vec::<String>::with_capacity(16);
180 let msg = match log_receiver.recv_timeout(FLUSH_TIMEOUT) {
182 Err(RecvTimeoutError::Timeout) => {
183 flush_logs_to_stderr(&mut buffer);
186 Err(RecvTimeoutError::Disconnected) => unreachable!("sender is static"),
190 LoggerCommand::Print(msg) => {
191 // Append message to buffer
192 if let Some(msg) = msg.to_formatted_message() {
196 if buffer.len() == buffer.capacity() {
197 flush_logs_to_stderr(&mut buffer);
200 LoggerCommand::Flush { finished_barrier } => {
201 flush_logs_to_stderr(&mut buffer);
202 finished_barrier.wait();
204 LoggerCommand::FlushAndShutdown { finished_barrier } => {
205 flush_logs_to_stderr(&mut buffer);
206 finished_barrier.wait();
213 fn flush_logs_to_stderr(buffer: &mut Vec<String>) {
214 if !buffer.is_empty() {
215 let text = buffer.join("\n");