mirror of
https://github.com/jht5945/rust_util.git
synced 2026-01-12 07:10:05 +08:00
Compare commits
5 Commits
c1ef4c4b53
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
254585bb90
|
|||
|
e13b2a3db4
|
|||
|
e2b258ca09
|
|||
|
4b596db8de
|
|||
|
af4b91d4e9
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.47"
|
version = "0.6.51"
|
||||||
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"
|
||||||
|
|||||||
@@ -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 *
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
pub fn is_env_on(var: &str) -> bool {
|
pub fn is_env_on(var: &str) -> bool {
|
||||||
env::var(var).ok().map(|val| is_on(&val)).unwrap_or(false)
|
env_var(var).map(|val| is_on(&val)).unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_env_off(var: &str) -> bool {
|
pub fn is_env_off(var: &str) -> bool {
|
||||||
env::var(var).ok().map(|val| is_off(&val)).unwrap_or(false)
|
env_var(var).map(|val| is_off(&val)).unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_on(val: &str) -> bool {
|
pub fn is_on(val: &str) -> bool {
|
||||||
@@ -17,3 +17,15 @@ pub fn is_off(val: &str) -> bool {
|
|||||||
let lower_val = val.to_lowercase();
|
let lower_val = val.to_lowercase();
|
||||||
["false", "no", "0"].iter().any(|x| *x == lower_val)
|
["false", "no", "0"].iter().any(|x| *x == lower_val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn env_var(var: &str) -> Option<String> {
|
||||||
|
let var_from_env = env::var(var).ok();
|
||||||
|
if var_from_env.is_some() {
|
||||||
|
return var_from_env;
|
||||||
|
}
|
||||||
|
let var_content = crate::util_file::read_file_content(&format!("~/.config/envs/{}", var));
|
||||||
|
if let Ok(var_content) = var_content {
|
||||||
|
return Some(var_content.trim().to_string());
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -2,9 +2,42 @@ use std::io::{self, Write};
|
|||||||
|
|
||||||
use crate::util_msg;
|
use crate::util_msg;
|
||||||
|
|
||||||
|
pub const STD_BLACK: &str = "\x1B[30m";
|
||||||
|
pub const STD_RED: &str = "\x1B[31m";
|
||||||
|
pub const STD_GREEN: &str = "\x1B[32m";
|
||||||
|
pub const STD_YELLOW: &str = "\x1B[33m";
|
||||||
|
pub const STD_BLUE: &str = "\x1B[34m";
|
||||||
|
pub const STD_MAGENTA: &str = "\x1B[35m"; // 品红色/洋红
|
||||||
|
pub const STD_CYAN: &str = "\x1B[36m"; // 青色
|
||||||
|
pub const STD_WHITE: &str = "\x1B[37m";
|
||||||
|
|
||||||
|
pub const BLACK: &str = "\x1B[90m";
|
||||||
pub const RED: &str = "\x1B[91m";
|
pub const RED: &str = "\x1B[91m";
|
||||||
pub const GREEN: &str = "\x1B[92m";
|
pub const GREEN: &str = "\x1B[92m";
|
||||||
pub const YELLOW: &str = "\x1B[93m";
|
pub const YELLOW: &str = "\x1B[93m";
|
||||||
|
pub const BLUE: &str = "\x1B[94m";
|
||||||
|
pub const MAGENTA: &str = "\x1B[95m";
|
||||||
|
pub const CYAN: &str = "\x1B[96m";
|
||||||
|
pub const WHITE: &str = "\x1B[97m";
|
||||||
|
|
||||||
|
pub const BG_STD_BLACK: &str = "\x1B[40m";
|
||||||
|
pub const BG_STD_RED: &str = "\x1B[41m";
|
||||||
|
pub const BG_STD_GREEN: &str = "\x1B[42m";
|
||||||
|
pub const BG_STD_YELLOW: &str = "\x1B[43m";
|
||||||
|
pub const BG_STD_BLUE: &str = "\x1B[44m";
|
||||||
|
pub const BG_STD_MAGENTA: &str = "\x1B[45m";
|
||||||
|
pub const BG_STD_CYAN: &str = "\x1B[46m";
|
||||||
|
pub const BG_STD_WHITE: &str = "\x1B[47m";
|
||||||
|
|
||||||
|
pub const BG_BLACK: &str = "\x1B[100m";
|
||||||
|
pub const BG_RED: &str = "\x1B[101m";
|
||||||
|
pub const BG_GREEN: &str = "\x1B[102m";
|
||||||
|
pub const BG_YELLOW: &str = "\x1B[103m";
|
||||||
|
pub const BG_BLUE: &str = "\x1B[104m";
|
||||||
|
pub const BG_MAGENTA: &str = "\x1B[105m";
|
||||||
|
pub const BG_CYAN: &str = "\x1B[106m";
|
||||||
|
pub const BG_WHITE: &str = "\x1B[107m";
|
||||||
|
|
||||||
pub const BOLD: &str = "\x1B[1m";
|
pub const BOLD: &str = "\x1B[1m";
|
||||||
pub const UNDER: &str = "\x1B[4m";
|
pub const UNDER: &str = "\x1B[4m";
|
||||||
pub const END: &str = "\x1B[0m";
|
pub const END: &str = "\x1B[0m";
|
||||||
|
|||||||
Reference in New Issue
Block a user