mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 15:40:03 +08:00
feat: add logger level, read json config
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
[package]
|
||||
name = "rust_util"
|
||||
version = "0.6.7"
|
||||
version = "0.6.8"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
|
||||
[features]
|
||||
default = [] #["serde", "serde_json"]
|
||||
use_serde = ["serde", "serde_json"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.65"
|
||||
term = "0.5.2"
|
||||
term_size = "0.3.1"
|
||||
lazy_static = "1.3.0"
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
|
||||
@@ -118,6 +118,26 @@ pub fn find_parents_exists_dir(dir: &str) -> Option<PathBuf> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_serde")]
|
||||
pub fn read_json_config<T>(config: Option<String>, files: &[String]) -> XResult<Option<(PathBuf, T)>> {
|
||||
let config_path_buf_opt = read_config(config, files);
|
||||
match config_path_buf_opt {
|
||||
None => Ok(None),
|
||||
Some(config_path_buf) => {
|
||||
information!("Read config: {}", config_path_buf);
|
||||
let config_content = fs::read_to_string(config_path_buf)?;
|
||||
Ok(Some((config_path_buf, serde_json::from_str(&config_content)?)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_config(config: Option<String>, files: &[String]) -> Option<PathBuf> {
|
||||
match config {
|
||||
Some(config_str) => Some(PathBuf::from(config_str)),
|
||||
None => locate_file(files),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn locate_file(files: &[String]) -> Option<PathBuf> {
|
||||
for f in files {
|
||||
match PathBuf::from(&resolve_file_path(f)) {
|
||||
|
||||
@@ -1,15 +1,47 @@
|
||||
use std::{
|
||||
env,
|
||||
io::{ self, Write },
|
||||
sync::{ Arc, Mutex },
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref IS_ATTY: bool = is_atty();
|
||||
static ref LOGGER_LEVEL: MessageType = get_logger_level();
|
||||
static ref PRINT_MESSAGE_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, }
|
||||
pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR, }
|
||||
|
||||
impl MessageType {
|
||||
pub fn get_u8_value(&self) -> u8 {
|
||||
match self {
|
||||
MessageType::DEBUG => 0,
|
||||
MessageType::INFO => 1,
|
||||
MessageType::OK => 2,
|
||||
MessageType::WARN => 3,
|
||||
MessageType::ERROR => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_logger_level() -> MessageType {
|
||||
if let Some(logger_level) = env::var("LOGGER_LEVEL").ok().or(env::var("LOGGER").ok()).or(env::var("LEVEL").ok()) {
|
||||
match logger_level.trim().to_lowercase().as_str() {
|
||||
"debug" => MessageType::DEBUG,
|
||||
"info" => MessageType::INFO,
|
||||
"ok" => MessageType::OK,
|
||||
"warn" => MessageType::WARN,
|
||||
"error" => MessageType::ERROR,
|
||||
_ => {
|
||||
print_message_ex(Some(term::color::YELLOW), "[WARN ]", &format!("Unknown logger level: {}, set to default INFO", logger_level));
|
||||
MessageType::INFO
|
||||
},
|
||||
}
|
||||
} else {
|
||||
MessageType::INFO
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_atty() -> bool {
|
||||
let stdout_fileno = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) };
|
||||
@@ -55,6 +87,8 @@ pub fn print_info (message: &str) { print_message(MessageType::INFO, message);
|
||||
pub fn print_debug(message: &str) { print_message(MessageType::DEBUG, message); }
|
||||
|
||||
pub fn print_message(mt: MessageType, message: &str) {
|
||||
let logger_level = *LOGGER_LEVEL;
|
||||
if mt.get_u8_value() >= logger_level.get_u8_value() {
|
||||
match mt {
|
||||
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
||||
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
|
||||
@@ -63,6 +97,7 @@ pub fn print_message(mt: MessageType, message: &str) {
|
||||
MessageType::DEBUG => print_message_ex(Some(term::color::MAGENTA), "[DEBUG]", message),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MessageType {
|
||||
pub fn print(&self, message: &str) {
|
||||
|
||||
Reference in New Issue
Block a user