Skip to content

Commit

Permalink
add pub fn logger()
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 27, 2024
1 parent 608ea91 commit ad95f77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
38 changes: 22 additions & 16 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger> = 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<Config>,
pub send: OnceLock<Sender<FastLogRecord>>,
Expand Down Expand Up @@ -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(())
Expand All @@ -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;
Expand Down Expand Up @@ -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<Sender<Arc<Vec<FastLogRecord>>>> = 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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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> {
Expand All @@ -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"))?
Expand All @@ -246,7 +252,7 @@ pub fn flush() -> Result<WaitGroup, 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"))?
Expand All @@ -261,5 +267,5 @@ pub fn flush() -> Result<WaitGroup, LogError> {
}

pub fn print(log: String) -> Result<(), SendError<FastLogRecord>> {
LOGGER.get_or_init(||{ Logger::default() }).print(log)
LOGGER.get_or_init(|| { Logger::default() }).print(log)
}

0 comments on commit ad95f77

Please sign in to comment.