mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
move spawn_logger_thread
to logger module
and create another module inside of the logger module to reorganize things
This commit is contained in:
parent
0b760aadf7
commit
792db000bb
@ -22,7 +22,7 @@ use crate::{
|
|||||||
list::ListOptions,
|
list::ListOptions,
|
||||||
utils::{
|
utils::{
|
||||||
self,
|
self,
|
||||||
logger::{info_accessible, map_message, setup_channel, warning, LogReceiver},
|
logger::{info_accessible, setup_channel, spawn_logger_thread, warning},
|
||||||
to_utf, EscapedPathDisplay, FileVisibilityPolicy,
|
to_utf, EscapedPathDisplay, FileVisibilityPolicy,
|
||||||
},
|
},
|
||||||
CliArgs, QuestionPolicy,
|
CliArgs, QuestionPolicy,
|
||||||
@ -252,39 +252,3 @@ fn run_cmd(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex<bool>, Condvar)>) {
|
|
||||||
rayon::spawn(move || {
|
|
||||||
const BUFFER_CAPACITY: usize = 10;
|
|
||||||
let mut buffer = Vec::<String>::with_capacity(BUFFER_CAPACITY);
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let msg = log_receiver.recv().expect("Failed to receive log message");
|
|
||||||
|
|
||||||
let is_shutdown_message = msg.is_none();
|
|
||||||
|
|
||||||
// Append message to buffer
|
|
||||||
if let Some(msg) = msg.as_ref().and_then(map_message) {
|
|
||||||
buffer.push(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
let should_flush = buffer.len() == BUFFER_CAPACITY || is_shutdown_message;
|
|
||||||
|
|
||||||
if should_flush {
|
|
||||||
let text = buffer.join("\n");
|
|
||||||
eprintln!("{text}");
|
|
||||||
buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_shutdown_message {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wake up the main thread
|
|
||||||
let (lock, cvar) = &*synchronization_pair;
|
|
||||||
let mut flushed = lock.lock().unwrap();
|
|
||||||
*flushed = true;
|
|
||||||
cvar.notify_one();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
@ -1,58 +1,10 @@
|
|||||||
use std::sync::{mpsc, OnceLock};
|
use std::sync::{mpsc, Arc, Condvar, Mutex, OnceLock};
|
||||||
|
|
||||||
|
pub use logger_thread::{setup_channel, spawn_logger_thread};
|
||||||
|
|
||||||
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<Option<PrintMessage>>;
|
|
||||||
type LogSender = mpsc::Sender<Option<PrintMessage>>;
|
|
||||||
|
|
||||||
static SENDER: OnceLock<LogSender> = OnceLock::new();
|
|
||||||
|
|
||||||
pub fn setup_channel() -> (LogReceiver, LoggerDropper) {
|
|
||||||
let (tx, rx) = mpsc::channel();
|
|
||||||
SENDER.set(tx).expect("`setup_channel` should only be called once");
|
|
||||||
(rx, LoggerDropper)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[track_caller]
|
|
||||||
fn get_sender() -> &'static LogSender {
|
|
||||||
SENDER.get().expect("No sender, you need to call `setup_channel` first")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Message object used for sending logs from worker threads to a logging thread via channels.
|
|
||||||
/// See <https://github.com/ouch-org/ouch/issues/643>
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct PrintMessage {
|
|
||||||
contents: String,
|
|
||||||
accessible: bool,
|
|
||||||
level: MessageLevel,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn map_message(msg: &PrintMessage) -> Option<String> {
|
|
||||||
match msg.level {
|
|
||||||
MessageLevel::Info => {
|
|
||||||
if msg.accessible {
|
|
||||||
if is_running_in_accessible_mode() {
|
|
||||||
Some(format!("{}Info:{} {}", *YELLOW, *RESET, msg.contents))
|
|
||||||
} else {
|
|
||||||
Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, msg.contents))
|
|
||||||
}
|
|
||||||
} else if !is_running_in_accessible_mode() {
|
|
||||||
Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, msg.contents))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MessageLevel::Warning => {
|
|
||||||
if is_running_in_accessible_mode() {
|
|
||||||
Some(format!("{}Warning:{} ", *ORANGE, *RESET))
|
|
||||||
} else {
|
|
||||||
Some(format!("{}[WARNING]{} ", *ORANGE, *RESET))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An `[INFO]` log to be displayed if we're not running accessibility mode.
|
/// An `[INFO]` log to be displayed if we're not running accessibility mode.
|
||||||
///
|
///
|
||||||
/// Same as `.info_accessible()`, but only displayed if accessibility mode
|
/// Same as `.info_accessible()`, but only displayed if accessibility mode
|
||||||
@ -78,7 +30,7 @@ pub fn info_accessible(contents: String) {
|
|||||||
|
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn info_with_accessibility(contents: String, accessible: bool) {
|
fn info_with_accessibility(contents: String, accessible: bool) {
|
||||||
send_log_message(PrintMessage {
|
logger_thread::send_log_message(PrintMessage {
|
||||||
contents,
|
contents,
|
||||||
accessible,
|
accessible,
|
||||||
level: MessageLevel::Info,
|
level: MessageLevel::Info,
|
||||||
@ -86,7 +38,7 @@ fn info_with_accessibility(contents: String, accessible: bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn warning(contents: String) {
|
pub fn warning(contents: String) {
|
||||||
send_log_message(PrintMessage {
|
logger_thread::send_log_message(PrintMessage {
|
||||||
contents,
|
contents,
|
||||||
// Warnings are important and unlikely to flood, so they should be displayed
|
// Warnings are important and unlikely to flood, so they should be displayed
|
||||||
accessible: true,
|
accessible: true,
|
||||||
@ -94,26 +46,119 @@ pub fn warning(contents: String) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Message object used for sending logs from worker threads to a logging thread via channels.
|
||||||
|
/// See <https://github.com/ouch-org/ouch/issues/643>
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PrintMessage {
|
||||||
|
contents: String,
|
||||||
|
accessible: bool,
|
||||||
|
level: MessageLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrintMessage {
|
||||||
|
fn to_processed_message(&self) -> Option<String> {
|
||||||
|
match self.level {
|
||||||
|
MessageLevel::Info => {
|
||||||
|
if self.accessible {
|
||||||
|
if is_running_in_accessible_mode() {
|
||||||
|
Some(format!("{}Info:{} {}", *YELLOW, *RESET, self.contents))
|
||||||
|
} else {
|
||||||
|
Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
|
||||||
|
}
|
||||||
|
} else if !is_running_in_accessible_mode() {
|
||||||
|
Some(format!("{}[INFO]{} {}", *YELLOW, *RESET, self.contents))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MessageLevel::Warning => {
|
||||||
|
if is_running_in_accessible_mode() {
|
||||||
|
Some(format!("{}Warning:{} ", *ORANGE, *RESET))
|
||||||
|
} else {
|
||||||
|
Some(format!("{}[WARNING]{} ", *ORANGE, *RESET))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum MessageLevel {
|
enum MessageLevel {
|
||||||
Info,
|
Info,
|
||||||
Warning,
|
Warning,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track_caller]
|
mod logger_thread {
|
||||||
fn send_log_message(msg: PrintMessage) {
|
use super::*;
|
||||||
send_message(Some(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[track_caller]
|
type LogReceiver = mpsc::Receiver<Option<PrintMessage>>;
|
||||||
fn send_message(msg: Option<PrintMessage>) {
|
type LogSender = mpsc::Sender<Option<PrintMessage>>;
|
||||||
get_sender().send(msg).expect("Failed to send internal message");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct LoggerDropper;
|
static SENDER: OnceLock<LogSender> = OnceLock::new();
|
||||||
|
|
||||||
impl Drop for LoggerDropper {
|
#[track_caller]
|
||||||
fn drop(&mut self) {
|
pub fn setup_channel() -> (LogReceiver, LoggerDropper) {
|
||||||
send_message(None);
|
let (tx, rx) = mpsc::channel();
|
||||||
|
SENDER.set(tx).expect("`setup_channel` should only be called once");
|
||||||
|
(rx, LoggerDropper)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn get_sender() -> &'static LogSender {
|
||||||
|
SENDER.get().expect("No sender, you need to call `setup_channel` first")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex<bool>, Condvar)>) {
|
||||||
|
rayon::spawn(move || {
|
||||||
|
const BUFFER_CAPACITY: usize = 10;
|
||||||
|
let mut buffer = Vec::<String>::with_capacity(BUFFER_CAPACITY);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let msg = log_receiver.recv().expect("Failed to receive log message");
|
||||||
|
|
||||||
|
let is_shutdown_message = msg.is_none();
|
||||||
|
|
||||||
|
// Append message to buffer
|
||||||
|
if let Some(msg) = msg.as_ref().and_then(PrintMessage::to_processed_message) {
|
||||||
|
buffer.push(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
let should_flush = buffer.len() == BUFFER_CAPACITY || is_shutdown_message;
|
||||||
|
|
||||||
|
if should_flush {
|
||||||
|
let text = buffer.join("\n");
|
||||||
|
eprintln!("{text}");
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_shutdown_message {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wake up the main thread
|
||||||
|
let (lock, cvar) = &*synchronization_pair;
|
||||||
|
let mut flushed = lock.lock().unwrap();
|
||||||
|
*flushed = true;
|
||||||
|
cvar.notify_one();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
pub 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LoggerDropper;
|
||||||
|
|
||||||
|
impl Drop for LoggerDropper {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
send_message(None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user