diff --git a/src/fast_log.rs b/src/fast_log.rs index e9dc1ac..7264a03 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -49,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().send.get() { send.send(fast_log_record) } else { // Ok(()) @@ -67,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().cfg.get() { + if let Some(send) = logger().send.get() { for filter in filter.filters.iter() { if !filter.do_log(record) { return; @@ -103,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().send.set(s).map_err(|_| LogError::from("set fail"))?; + logger().recv.set(r).map_err(|_| LogError::from("set fail"))?; + logger().set_level(config.level); + logger() .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()) + .map(|()| log::set_max_level(logger().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().cfg.get().unwrap(); for a in cfg.appends.iter() { let (s, r) = chan(cfg.chan_len); sender_vec.push(s); @@ -172,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().recv.get().unwrap(); let mut remain = Vec::with_capacity(recv.len()); //recv if recv.len() == 0 { @@ -194,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().cfg.get().unwrap().format.do_format(x); } if x.command.eq(&Command::CommandExit) { exit = true; @@ -210,7 +210,7 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { } }); } - return Ok(LOGGER.get_or_init(|| { Logger::default() })); + return Ok(logger()); } pub fn exit() -> Result<(), LogError> { @@ -225,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() .send .get() .ok_or_else(|| LogError::from("not init"))? @@ -252,7 +252,7 @@ pub fn flush() -> Result { now: SystemTime::now(), formated: String::new(), }; - let result = LOGGER.get_or_init(|| { Logger::default() }) + let result = logger() .send .get() .ok_or_else(|| LogError::from("not init"))? @@ -267,5 +267,5 @@ pub fn flush() -> Result { } pub fn print(log: String) -> Result<(), SendError> { - LOGGER.get_or_init(|| { Logger::default() }).print(log) + logger().print(log) }