1 use std::sync::{mpsc, Arc, Condvar, Mutex, OnceLock};
3 pub use logger_thread::{setup_channel, spawn_logger_thread};
5 use super::colors::{ORANGE, RESET, YELLOW};
6 use crate::accessible::is_running_in_accessible_mode;
8 /// An `[INFO]` log to be displayed if we're not running accessibility mode.
10 /// Same as `.info_accessible()`, but only displayed if accessibility mode
11 /// is turned off, which is detected by the function
12 /// `is_running_in_accessible_mode`.
14 /// Read more about accessibility mode in `accessible.rs`.
16 pub fn info(contents: String) {
17 info_with_accessibility(contents, false);
20 /// An `[INFO]` log to be displayed.
22 /// Same as `.info()`, but also displays if `is_running_in_accessible_mode`
25 /// Read more about accessibility mode in `accessible.rs`.
27 pub fn info_accessible(contents: String) {
28 info_with_accessibility(contents, true);
32 fn info_with_accessibility(contents: String, accessible: bool) {
33 logger_thread::send_log_message(PrintMessage {
36 level: MessageLevel::Info,
40 pub fn warning(contents: String) {
41 logger_thread::send_log_message(PrintMessage {
43 // Warnings are important and unlikely to flood, so they should be displayed
45 level: MessageLevel::Warning,
49 /// Message object used for sending logs from worker threads to a logging thread via channels.
50 /// See <https://github.com/ouch-org/ouch/issues/643>
52 pub struct PrintMessage {
59 fn to_processed_message(&self) -> Option<String> {
61 MessageLevel::Info => {
63 if is_running_in_accessible_mode() {
64 Some(format!("{}Info:{} {}", *YELLOW, *RESET, self.contents))
66 Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
68 } else if !is_running_in_accessible_mode() {
69 Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
74 MessageLevel::Warning => {
75 if is_running_in_accessible_mode() {
76 Some(format!("{}Warning:{} ", *ORANGE, *RESET))
78 Some(format!("{}[WARNING]{} ", *ORANGE, *RESET))
85 #[derive(Debug, PartialEq)]
94 type LogReceiver = mpsc::Receiver<Option<PrintMessage>>;
95 type LogSender = mpsc::Sender<Option<PrintMessage>>;
97 static SENDER: OnceLock<LogSender> = OnceLock::new();
100 pub fn setup_channel() -> (LogReceiver, LoggerDropper) {
101 let (tx, rx) = mpsc::channel();
102 SENDER.set(tx).expect("`setup_channel` should only be called once");
107 fn get_sender() -> &'static LogSender {
108 SENDER.get().expect("No sender, you need to call `setup_channel` first")
111 pub fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex<bool>, Condvar)>) {
112 rayon::spawn(move || {
113 const BUFFER_CAPACITY: usize = 10;
114 let mut buffer = Vec::<String>::with_capacity(BUFFER_CAPACITY);
117 let msg = log_receiver.recv().expect("Failed to receive log message");
119 let is_shutdown_message = msg.is_none();
121 // Append message to buffer
122 if let Some(msg) = msg.as_ref().and_then(PrintMessage::to_processed_message) {
126 let should_flush = buffer.len() == BUFFER_CAPACITY || is_shutdown_message;
129 let text = buffer.join("\n");
134 if is_shutdown_message {
139 // Wake up the main thread
140 let (lock, cvar) = &*synchronization_pair;
141 let mut flushed = lock.lock().unwrap();
148 pub fn send_log_message(msg: PrintMessage) {
149 send_message(Some(msg));
153 fn send_message(msg: Option<PrintMessage>) {
154 get_sender().send(msg).expect("Failed to send internal message");
157 pub struct LoggerDropper;
159 impl Drop for LoggerDropper {