From ad95f77485c8350a659777adb87b047467449aa1 Mon Sep 17 00:00:00 2001 From: zxj Date: Sat, 27 Jul 2024 20:47:02 +0800 Subject: [PATCH] add pub fn logger() --- Cargo.toml | 2 +- src/fast_log.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e709b4c..f8c7055 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ [package] name = "fast_log" -version = "1.7.0" +version = "1.7.1" description = "Rust async log High-performance asynchronous logging" readme = "Readme.md" authors = ["ce "] diff --git a/src/fast_log.rs b/src/fast_log.rs index a74fa9a..e9dc1ac 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -5,8 +5,14 @@ use crate::{chan, spawn, Receiver, SendError, Sender, WaitGroup}; use log::{LevelFilter, Log, Metadata, Record}; use std::sync::{Arc, OnceLock}; use std::time::SystemTime; + pub static LOGGER: OnceLock = OnceLock::new(); +/// get Logger,but you must call `fast_log::init` +pub fn logger() -> &'static Logger { + LOGGER.get_or_init(|| { Logger::default() }) +} + pub struct Logger { pub cfg: OnceLock, pub send: OnceLock>, @@ -43,7 +49,7 @@ impl Logger { now: SystemTime::now(), formated: log, }; - if let Some(send) = LOGGER.get_or_init(||{ Logger::default() }).send.get() { + if let Some(send) = LOGGER.get_or_init(|| { Logger::default() }).send.get() { send.send(fast_log_record) } else { // Ok(()) @@ -61,8 +67,8 @@ impl Log for Logger { metadata.level() <= self.get_level() } fn log(&self, record: &Record) { - if let Some(filter) = LOGGER.get_or_init(||{ Logger::default() }).cfg.get() { - if let Some(send) = LOGGER.get_or_init(||{ Logger::default() }).send.get() { + if let Some(filter) = LOGGER.get_or_init(|| { Logger::default() }).cfg.get() { + if let Some(send) = LOGGER.get_or_init(|| { Logger::default() }).send.get() { for filter in filter.filters.iter() { if !filter.do_log(record) { return; @@ -97,21 +103,21 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { return Err(LogError::from("[fast_log] appends can not be empty!")); } let (s, r) = chan(config.chan_len); - LOGGER.get_or_init(||{ Logger::default() }).send.set(s).map_err(|_| LogError::from("set fail"))?; - LOGGER.get_or_init(||{ Logger::default() }).recv.set(r).map_err(|_| LogError::from("set fail"))?; - LOGGER.get_or_init(||{ Logger::default() }).set_level(config.level); - LOGGER.get_or_init(||{ Logger::default() }) + LOGGER.get_or_init(|| { Logger::default() }).send.set(s).map_err(|_| LogError::from("set fail"))?; + LOGGER.get_or_init(|| { Logger::default() }).recv.set(r).map_err(|_| LogError::from("set fail"))?; + LOGGER.get_or_init(|| { Logger::default() }).set_level(config.level); + LOGGER.get_or_init(|| { Logger::default() }) .cfg .set(config) .map_err(|_| LogError::from("set fail="))?; //main recv data - log::set_logger(LOGGER.get_or_init(||{ Logger::default() })) - .map(|()| log::set_max_level(LOGGER.get_or_init(||{ Logger::default() }).cfg.get().unwrap().level)) + log::set_logger(LOGGER.get_or_init(|| { Logger::default() })) + .map(|()| log::set_max_level(LOGGER.get_or_init(|| { Logger::default() }).cfg.get().unwrap().level)) .map_err(|e| LogError::from(e))?; let mut receiver_vec = vec![]; let mut sender_vec: Vec>>> = vec![]; - let cfg = LOGGER.get_or_init(||{ Logger::default() }).cfg.get().unwrap(); + let cfg = LOGGER.get_or_init(|| { Logger::default() }).cfg.get().unwrap(); for a in cfg.appends.iter() { let (s, r) = chan(cfg.chan_len); sender_vec.push(s); @@ -166,7 +172,7 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { let senders = sender_vec.clone(); spawn(move || { loop { - let recv = LOGGER.get_or_init(||{ Logger::default() }).recv.get().unwrap(); + let recv = LOGGER.get_or_init(|| { Logger::default() }).recv.get().unwrap(); let mut remain = Vec::with_capacity(recv.len()); //recv if recv.len() == 0 { @@ -188,7 +194,7 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { let mut exit = false; for x in &mut remain { if x.formated.is_empty() { - LOGGER.get_or_init(||{ Logger::default() }).cfg.get().unwrap().format.do_format(x); + LOGGER.get_or_init(|| { Logger::default() }).cfg.get().unwrap().format.do_format(x); } if x.command.eq(&Command::CommandExit) { exit = true; @@ -204,7 +210,7 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { } }); } - return Ok(LOGGER.get_or_init(||{ Logger::default() })); + return Ok(LOGGER.get_or_init(|| { Logger::default() })); } pub fn exit() -> Result<(), LogError> { @@ -219,7 +225,7 @@ pub fn exit() -> Result<(), LogError> { now: SystemTime::now(), formated: String::new(), }; - let result = LOGGER.get_or_init(||{ Logger::default() }) + let result = LOGGER.get_or_init(|| { Logger::default() }) .send .get() .ok_or_else(|| LogError::from("not init"))? @@ -246,7 +252,7 @@ pub fn flush() -> Result { now: SystemTime::now(), formated: String::new(), }; - let result = LOGGER.get_or_init(||{ Logger::default() }) + let result = LOGGER.get_or_init(|| { Logger::default() }) .send .get() .ok_or_else(|| LogError::from("not init"))? @@ -261,5 +267,5 @@ pub fn flush() -> Result { } pub fn print(log: String) -> Result<(), SendError> { - LOGGER.get_or_init(||{ Logger::default() }).print(log) + LOGGER.get_or_init(|| { Logger::default() }).print(log) }