1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

feat: v0.5.48

This commit is contained in:
2025-07-26 08:52:31 +08:00
parent c1ef4c4b53
commit af4b91d4e9
3 changed files with 28 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.6.47" version = "0.6.48"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

View File

@@ -1,10 +1,22 @@
#[macro_use] extern crate rust_util; #[macro_use] extern crate rust_util;
use std::sync::mpsc::channel;
use std::thread;
use rust_util::{XResult, SimpleError}; use rust_util::{XResult, SimpleError};
use rust_util::util_msg::set_logger_std_out; use rust_util::util_msg::{set_logger_sender, set_logger_std_out};
// cargo run --example log // cargo run --example log
fn main() -> XResult<()> { fn main() -> XResult<()> {
let (sender, receiver) = channel::<String>();
set_logger_sender(sender);
let _t = thread::spawn(move || {
loop {
let msg = receiver.recv();
println!("[RECV]: {:?}", msg)
}
});
std::env::set_var("LOGGER_LEVEL", "*"); std::env::set_var("LOGGER_LEVEL", "*");
println!(r##"env LOGGER_LEVEL set to: println!(r##"env LOGGER_LEVEL set to:
debug or * debug or *

View File

@@ -1,14 +1,21 @@
use std::env; use std::env;
use std::io::{self, Write}; use std::io::{self, Write};
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use std::sync::mpsc::Sender;
lazy_static! { lazy_static! {
pub static ref IS_ATTY: bool = is_atty(); pub static ref IS_ATTY: bool = is_atty();
static ref LOGGER_LEVEL: MessageType = get_logger_level(); static ref LOGGER_LEVEL: MessageType = get_logger_level();
static ref LOGGER_SENDER: Arc<RwLock<Option<Sender<String>>>> = Arc::new(RwLock::new(None));
static ref LOGGER_TO_STDOUT: Arc<RwLock<bool>> = Arc::new(RwLock::new(true)); static ref LOGGER_TO_STDOUT: Arc<RwLock<bool>> = Arc::new(RwLock::new(true));
static ref PRINT_MESSAGE_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(())); static ref PRINT_MESSAGE_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
} }
pub fn set_logger_sender(sender: Sender<String>) {
let mut logger_sender_opt = LOGGER_SENDER.write().unwrap();
logger_sender_opt.replace(sender);
}
pub fn set_logger_std_out(is_std_out: bool) { pub fn set_logger_std_out(is_std_out: bool) {
let mut std_out = LOGGER_TO_STDOUT.write().unwrap(); let mut std_out = LOGGER_TO_STDOUT.write().unwrap();
*std_out = is_std_out; *std_out = is_std_out;
@@ -105,6 +112,13 @@ pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m
} }
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) { pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
{
let logger_sender_opt = LOGGER_SENDER.read().unwrap();
if let Some(logger_sender) = &*logger_sender_opt {
logger_sender.send(format!("{} {}", h, message)).ok();
return;
}
}
let is_std_out = get_logger_std_out(); let is_std_out = get_logger_std_out();
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap(); let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
print_color(is_std_out, color, true, h); print_color(is_std_out, color, true, h);