mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
feat: add logger to std err
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.40"
|
version = "0.6.41"
|
||||||
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,6 +1,7 @@
|
|||||||
#[macro_use] extern crate rust_util;
|
#[macro_use] extern crate rust_util;
|
||||||
|
|
||||||
use rust_util::{XResult, SimpleError};
|
use rust_util::{XResult, SimpleError};
|
||||||
|
use rust_util::util_msg::set_logger_std_out;
|
||||||
|
|
||||||
// cargo run --example log
|
// cargo run --example log
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
@@ -20,6 +21,11 @@ error or ^"##);
|
|||||||
|
|
||||||
println!("{:?}", test_opt_result());
|
println!("{:?}", test_opt_result());
|
||||||
|
|
||||||
|
set_logger_std_out(false);
|
||||||
|
information!("Std err!");
|
||||||
|
warning!("Std err!");
|
||||||
|
set_logger_std_out(true);
|
||||||
|
|
||||||
simple_error!("helloworld {}", 1)
|
simple_error!("helloworld {}", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
111
src/util_msg.rs
111
src/util_msg.rs
@@ -1,13 +1,19 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
|
|
||||||
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_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_std_out(is_std_out: bool) {
|
||||||
|
let mut std_out = LOGGER_TO_STDOUT.write().unwrap();
|
||||||
|
*std_out = is_std_out;
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR }
|
pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR }
|
||||||
@@ -16,9 +22,9 @@ impl MessageType {
|
|||||||
pub fn get_u8_value(&self) -> u8 {
|
pub fn get_u8_value(&self) -> u8 {
|
||||||
match self {
|
match self {
|
||||||
MessageType::DEBUG => 0,
|
MessageType::DEBUG => 0,
|
||||||
MessageType::INFO => 1,
|
MessageType::INFO => 1,
|
||||||
MessageType::OK => 2,
|
MessageType::OK => 2,
|
||||||
MessageType::WARN => 3,
|
MessageType::WARN => 3,
|
||||||
MessageType::ERROR => 4,
|
MessageType::ERROR => 4,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,18 +32,18 @@ impl MessageType {
|
|||||||
|
|
||||||
pub fn get_logger_level() -> MessageType {
|
pub fn get_logger_level() -> MessageType {
|
||||||
if let Some(logger_level) = env::var("LOGGER_LEVEL").ok()
|
if let Some(logger_level) = env::var("LOGGER_LEVEL").ok()
|
||||||
.or_else(|| env::var("LOGGER").ok())
|
.or_else(|| env::var("LOGGER").ok())
|
||||||
.or_else(|| env::var("LEVEL").ok()) {
|
.or_else(|| env::var("LEVEL").ok()) {
|
||||||
match logger_level.trim().to_lowercase().as_str() {
|
match logger_level.trim().to_lowercase().as_str() {
|
||||||
"debug" | "*" => MessageType::DEBUG,
|
"debug" | "*" => MessageType::DEBUG,
|
||||||
"info" | "?" => MessageType::INFO,
|
"info" | "?" => MessageType::INFO,
|
||||||
"ok" | "#" => MessageType::OK,
|
"ok" | "#" => MessageType::OK,
|
||||||
"warn" | "!" => MessageType::WARN,
|
"warn" | "!" => MessageType::WARN,
|
||||||
"error" | "^" => MessageType::ERROR,
|
"error" | "^" => MessageType::ERROR,
|
||||||
_ => {
|
_ => {
|
||||||
print_message_ex(Some(term::color::YELLOW), "[WARN ]", &format!("Unknown logger level: {}, set to default INFO", logger_level));
|
print_message_ex(Some(term::color::YELLOW), "[WARN ]", &format!("Unknown logger level: {}, set to default INFO", logger_level));
|
||||||
MessageType::INFO
|
MessageType::INFO
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
MessageType::INFO
|
MessageType::INFO
|
||||||
@@ -49,42 +55,73 @@ pub fn is_atty() -> bool {
|
|||||||
stdout_fileno != 0
|
stdout_fileno != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
pub fn print_color(is_std_out: bool, color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||||
match term::stdout() {
|
if is_std_out {
|
||||||
Some(mut t) => {
|
match term::stdout() {
|
||||||
if *IS_ATTY {
|
Some(mut t) => {
|
||||||
if let Some(c) = color {
|
if *IS_ATTY {
|
||||||
t.fg(c).ok();
|
if let Some(c) = color {
|
||||||
|
t.fg(c).ok();
|
||||||
|
}
|
||||||
|
if is_bold {
|
||||||
|
t.attr(term::Attr::Bold).ok();
|
||||||
|
}
|
||||||
|
write!(t, "{}", m).ok();
|
||||||
|
t.reset().ok();
|
||||||
|
} else {
|
||||||
|
write!(t, "{}", m).ok();
|
||||||
}
|
}
|
||||||
if is_bold {
|
|
||||||
t.attr(term::Attr::Bold).ok();
|
|
||||||
}
|
|
||||||
write!(t, "{}", m).ok();
|
|
||||||
t.reset().ok();
|
|
||||||
} else {
|
|
||||||
write!(t, "{}", m).ok();
|
|
||||||
}
|
}
|
||||||
},
|
None => print!("{}", m),
|
||||||
None => print!("{}", m),
|
}
|
||||||
|
} else {
|
||||||
|
match term::stderr() {
|
||||||
|
Some(mut t) => {
|
||||||
|
if *IS_ATTY {
|
||||||
|
if let Some(c) = color {
|
||||||
|
t.fg(c).ok();
|
||||||
|
}
|
||||||
|
if is_bold {
|
||||||
|
t.attr(term::Attr::Bold).ok();
|
||||||
|
}
|
||||||
|
write!(t, "{}", m).ok();
|
||||||
|
t.reset().ok();
|
||||||
|
} else {
|
||||||
|
write!(t, "{}", m).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => eprint!("{}", m),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||||
print_color(color, is_bold, m);
|
print_color(true, color, is_bold, m);
|
||||||
flush_stdout();
|
flush_stdout();
|
||||||
}
|
}
|
||||||
|
|
||||||
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 is_std_out = {
|
||||||
|
*LOGGER_TO_STDOUT.read().unwrap()
|
||||||
|
};
|
||||||
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
|
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
|
||||||
print_color(color, true, h);
|
print_color(is_std_out, color, true, h);
|
||||||
println!(" {}", message);
|
if is_std_out {
|
||||||
|
println!(" {}", message);
|
||||||
|
} else {
|
||||||
|
eprintln!(" {}", message)
|
||||||
|
}
|
||||||
*lock = ();
|
*lock = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_ok (message: &str) { print_message(MessageType::OK, message); }
|
pub fn print_ok(message: &str) { print_message(MessageType::OK, message); }
|
||||||
pub fn print_warn (message: &str) { print_message(MessageType::WARN, message); }
|
|
||||||
|
pub fn print_warn(message: &str) { print_message(MessageType::WARN, message); }
|
||||||
|
|
||||||
pub fn print_error(message: &str) { print_message(MessageType::ERROR, message); }
|
pub fn print_error(message: &str) { print_message(MessageType::ERROR, message); }
|
||||||
pub fn print_info (message: &str) { print_message(MessageType::INFO, message); }
|
|
||||||
|
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_debug(message: &str) { print_message(MessageType::DEBUG, message); }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -102,10 +139,10 @@ pub fn when<F>(mt: MessageType, f: F) where F: Fn() {
|
|||||||
pub fn print_message(mt: MessageType, message: &str) {
|
pub fn print_message(mt: MessageType, message: &str) {
|
||||||
if is_logger_level_enabled(mt) {
|
if is_logger_level_enabled(mt) {
|
||||||
match mt {
|
match mt {
|
||||||
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
||||||
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
|
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
|
||||||
MessageType::ERROR => print_message_ex(Some(term::color::RED), "[ERROR]", message),
|
MessageType::ERROR => print_message_ex(Some(term::color::RED), "[ERROR]", message),
|
||||||
MessageType::INFO => print_message_ex(None, "[INFO ]", message),
|
MessageType::INFO => print_message_ex(None, "[INFO ]", message),
|
||||||
MessageType::DEBUG => print_message_ex(Some(term::color::MAGENTA), "[DEBUG]", message),
|
MessageType::DEBUG => print_message_ex(Some(term::color::MAGENTA), "[DEBUG]", message),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,13 +185,13 @@ pub fn get_term_width_message(message: &str, left: usize) -> String {
|
|||||||
Some((w, _h)) => {
|
Some((w, _h)) => {
|
||||||
let len = message.len();
|
let len = message.len();
|
||||||
if w > len {
|
if w > len {
|
||||||
return message.to_string();
|
return message.to_string();
|
||||||
}
|
}
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
s.push_str(&message[0..find_char_boundary(&message, w - 10 - 5 - left)]);
|
s.push_str(&message[0..find_char_boundary(&message, w - 10 - 5 - left)]);
|
||||||
s.push_str("[...]");
|
s.push_str("[...]");
|
||||||
s.push_str(&message[find_char_boundary(&message, len - 10)..]);
|
s.push_str(&message[find_char_boundary(&message, len - 10)..]);
|
||||||
s
|
s
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user