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]
|
||||
name = "rust_util"
|
||||
version = "0.6.40"
|
||||
version = "0.6.41"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#[macro_use] extern crate rust_util;
|
||||
|
||||
use rust_util::{XResult, SimpleError};
|
||||
use rust_util::util_msg::set_logger_std_out;
|
||||
|
||||
// cargo run --example log
|
||||
fn main() -> XResult<()> {
|
||||
@@ -20,6 +21,11 @@ error or ^"##);
|
||||
|
||||
println!("{:?}", test_opt_result());
|
||||
|
||||
set_logger_std_out(false);
|
||||
information!("Std err!");
|
||||
warning!("Std err!");
|
||||
set_logger_std_out(true);
|
||||
|
||||
simple_error!("helloworld {}", 1)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
use std::env;
|
||||
use std::io::{self, Write};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref IS_ATTY: bool = is_atty();
|
||||
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(()));
|
||||
}
|
||||
|
||||
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)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum MessageType { DEBUG, INFO, OK, WARN, ERROR }
|
||||
@@ -37,7 +43,7 @@ pub fn get_logger_level() -> MessageType {
|
||||
_ => {
|
||||
print_message_ex(Some(term::color::YELLOW), "[WARN ]", &format!("Unknown logger level: {}, set to default INFO", logger_level));
|
||||
MessageType::INFO
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MessageType::INFO
|
||||
@@ -49,7 +55,8 @@ pub fn is_atty() -> bool {
|
||||
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) {
|
||||
if is_std_out {
|
||||
match term::stdout() {
|
||||
Some(mut t) => {
|
||||
if *IS_ATTY {
|
||||
@@ -64,27 +71,57 @@ pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
} else {
|
||||
write!(t, "{}", m).ok();
|
||||
}
|
||||
},
|
||||
}
|
||||
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) {
|
||||
print_color(color, is_bold, m);
|
||||
print_color(true, color, is_bold, m);
|
||||
flush_stdout();
|
||||
}
|
||||
|
||||
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();
|
||||
print_color(color, true, h);
|
||||
print_color(is_std_out, color, true, h);
|
||||
if is_std_out {
|
||||
println!(" {}", message);
|
||||
} else {
|
||||
eprintln!(" {}", message)
|
||||
}
|
||||
*lock = ();
|
||||
}
|
||||
|
||||
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_error(message: &str) { print_message(MessageType::ERROR, message); }
|
||||
|
||||
pub fn print_info(message: &str) { print_message(MessageType::INFO, message); }
|
||||
|
||||
pub fn print_debug(message: &str) { print_message(MessageType::DEBUG, message); }
|
||||
|
||||
#[inline]
|
||||
@@ -155,6 +192,6 @@ pub fn get_term_width_message(message: &str, left: usize) -> String {
|
||||
s.push_str("[...]");
|
||||
s.push_str(&message[find_char_boundary(&message, len - 10)..]);
|
||||
s
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user