Skip to content

Commit

Permalink
add add_appender
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Mar 9, 2024
1 parent 544a752 commit d902067
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
30 changes: 16 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<Box<dyn LogAppender>>>,
pub appends: SyncVec<Box<dyn LogAppender>>,
/// the log level filter
pub level: LevelFilter,
/// filter log
Expand Down Expand Up @@ -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::<RawFile>::new(file, max_temp_size).expect("make file_loop fail"),
)));
));
self
}
/// add a FileSplitAppender
Expand All @@ -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::<RawFile>::new(file_path, temp_size, rolling_type, Box::new(packer))
.unwrap(),
)));
));
self
}

Expand All @@ -133,9 +135,9 @@ impl Config {
keeper: R,
packer: P,
) -> Self {
self.appends.push(Mutex::new(Box::new(
self.appends.push(Box::new(
FileSplitAppender::<F>::new(file_path, temp_size, keeper, Box::new(packer)).unwrap(),
)));
));
self
}
/// add a custom LogAppender
Expand All @@ -145,7 +147,7 @@ impl Config {

/// add a LogAppender
pub fn add_appender<Appender: LogAppender + 'static>(self, arg: Appender) -> Self {
self.appends.push(Mutex::new(Box::new(arg)));
self.appends.push(Box::new(arg));
self
}

Expand Down
10 changes: 7 additions & 3 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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 => {}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit d902067

Please sign in to comment.