From d902067fe8767f68db00c950c75acedd521fda5a Mon Sep 17 00:00:00 2001 From: "zhuxiujia@qq.com" Date: Sun, 10 Mar 2024 00:29:31 +0800 Subject: [PATCH] add add_appender --- src/config.rs | 30 ++++++++++++++++-------------- src/fast_log.rs | 10 +++++++--- src/lib.rs | 1 + 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/config.rs b/src/config.rs index a80db60..ed9c4eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,17 +8,19 @@ use crate::plugin::file_split::{FileSplitAppender, Keep, Packer, RawFile, SplitF use crate::FastLogFormat; use dark_std::sync::SyncVec; use log::LevelFilter; -use parking_lot::Mutex; use std::fmt::{Debug, Formatter}; /// the fast_log Config /// for example: -// fast_log::init( -// Config::new().console().chan_len(Some(1000000)) -// ) +/// ```rust +/// use fast_log::Config; +/// fn main(){ +/// fast_log::init(Config::new().console().chan_len(Some(1000000))).unwrap(); +/// } +/// ``` pub struct Config { /// Each appender is responsible for printing its own business - pub appends: SyncVec>>, + pub appends: SyncVec>, /// the log level filter pub level: LevelFilter, /// filter log @@ -81,20 +83,20 @@ impl Config { } /// add a ConsoleAppender pub fn console(self) -> Self { - self.appends.push(Mutex::new(Box::new(ConsoleAppender {}))); + self.appends.push(Box::new(ConsoleAppender {})); self } /// add a FileAppender pub fn file(self, file: &str) -> Self { self.appends - .push(Mutex::new(Box::new(FileAppender::new(file).unwrap()))); + .push(Box::new(FileAppender::new(file).unwrap())); self } /// add a FileLoopAppender pub fn file_loop(self, file: &str, max_temp_size: LogSize) -> Self { - self.appends.push(Mutex::new(Box::new( + self.appends.push(Box::new( FileLoopAppender::::new(file, max_temp_size).expect("make file_loop fail"), - ))); + )); self } /// add a FileSplitAppender @@ -105,10 +107,10 @@ impl Config { rolling_type: R, packer: P, ) -> Self { - self.appends.push(Mutex::new(Box::new( + self.appends.push(Box::new( FileSplitAppender::::new(file_path, temp_size, rolling_type, Box::new(packer)) .unwrap(), - ))); + )); self } @@ -133,9 +135,9 @@ impl Config { keeper: R, packer: P, ) -> Self { - self.appends.push(Mutex::new(Box::new( + self.appends.push(Box::new( FileSplitAppender::::new(file_path, temp_size, keeper, Box::new(packer)).unwrap(), - ))); + )); self } /// add a custom LogAppender @@ -145,7 +147,7 @@ impl Config { /// add a LogAppender pub fn add_appender(self, arg: Appender) -> Self { - self.appends.push(Mutex::new(Box::new(arg))); + self.appends.push(Box::new(arg)); self } diff --git a/src/fast_log.rs b/src/fast_log.rs index dfed9de..6f83d8d 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -1,4 +1,4 @@ -use crate::appender::{Command, FastLogRecord}; +use crate::appender::{Command, FastLogRecord, LogAppender}; use crate::config::Config; use crate::error::LogError; use crate::{chan, spawn, Receiver, SendError, Sender, WaitGroup}; @@ -117,6 +117,9 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { receiver_vec.push((r, a)); } for (receiver, appender) in receiver_vec { + let append = appender.as_ref(); + let ptr = append as *const dyn LogAppender; + let raw_appender: u128 = unsafe { std::mem::transmute(ptr) }; spawn(move || { let mut exit = false; loop { @@ -137,9 +140,10 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { } } } - let append = appender.lock(); + let addr: *const dyn LogAppender = unsafe { std::mem::transmute(raw_appender) }; + let shared_appender: &dyn LogAppender = unsafe { &*(addr) }; for msg in remain { - append.do_logs(msg.as_ref()); + shared_appender.do_logs(msg.as_ref()); for x in msg.iter() { match x.command { Command::CommandRecord => {} diff --git a/src/lib.rs b/src/lib.rs index 7a5baaf..98e090a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod formats; pub mod plugin; pub mod runtime; + pub use crate::config::Config; pub use crate::fast_log::*; pub use crate::formats::*;