Skip to content

Commit

Permalink
thread spawn main channel recv
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Dec 14, 2021
1 parent 12db585 commit 5d3f4f5
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 17 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ A log implementation for extreme speed, using Crossbeam to double the efficiency
log data-> | main channel(crossbeam) | ->
-----------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appenders |
-> |coroutines channel(may)| -> background coroutines | appender1 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender2 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender3 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender4 |
---------------- ----------------------
Expand Down
16 changes: 14 additions & 2 deletions README_CH.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ the fast log . This crate uses #![forbid(unsafe_code)] to ensure everything is
```
-----------------
log data-> | main channel | ->
log data-> | main channel(crossbeam) | ->
-----------------
---------------- ----------------------
-> |may coroutines channel| -> background coroutines | appenders |
-> |coroutines channel(may)| -> background coroutines | appender1 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender2 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender3 |
---------------- ----------------------
---------------- ----------------------
-> |coroutines channel(may)| -> background coroutines | appender4 |
---------------- ----------------------
```

Expand Down
6 changes: 3 additions & 3 deletions example/src/bench_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ use fast_log::bencher::QPS;
struct BenchRecvLog {}

impl LogAppender for BenchRecvLog {
fn do_log(&self, record: &mut FastLogRecord) {
fn do_log(&self, record: &FastLogRecord) {
//do nothing
}
}

// this example should be "cargo run --release --package example --bin bench_test"
fn main(){
fn main() {
fast_log::init_custom_log(
vec![Box::new(BenchRecvLog {})],
log::Level::Info,
Box::new(NoFilter {}),
Box::new(FastLogFormatRecord::new()),
);
let total = 10000;
let total = 1000000;
let now = Instant::now();
for index in 0..total {
log::info!("Commencing yak shaving{}", index);
Expand Down
2 changes: 1 addition & 1 deletion src/appender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ops::{Add, Sub};
pub trait LogAppender: Send {
/// this method use one coroutines run this(Multiple appenders share one Appender).
/// so. if you want access the network, you can launch a coroutine using go! (| | {});
fn do_log(&self, record: &mut FastLogRecord);
fn do_log(&self, record: &FastLogRecord);

fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
Expand Down
29 changes: 23 additions & 6 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Borrow;
use std::sync::atomic::AtomicI32;
use may::sync::mpsc::{Receiver, Sender};
use log::{Level, Metadata, Record};
Expand Down Expand Up @@ -192,24 +193,40 @@ pub fn init_custom_log(
}
});
let wait_group_back = wait_group.clone();

//back recv data
go!(move || {
let back_recv = move || {
let mut recever_vec = vec![];
let mut sender_vec: Vec<may::sync::mpsc::Sender<Arc<FastLogRecord>>> = vec![];
for a in appenders {
let (s, r) = may::sync::mpsc::channel();
sender_vec.push(s);
recever_vec.push((r, a));
}
for (recever, appender) in recever_vec {
go!(move ||{
if let Ok(msg) = recever.recv(){
appender.do_log(msg.as_ref());
}
});
}
loop {
//recv
let data = back_recv.recv();
if let Ok(mut data) = data {
if data.command.eq(&Command::CommandExit){
if data.command.eq(&Command::CommandExit) {
drop(wait_group_back);
break;
}
data.formated = format.do_format(&mut data);
for x in &appenders {
x.do_log(&mut data);
let data = Arc::new(data);
for x in &sender_vec {
x.send(data.clone());
}
}
}
});

};
go!(back_recv);
let r = log::set_logger(&LOGGER).map(|()| log::set_max_level(level.to_level_filter()));
if r.is_err() {
return Err(LogError::from(r.err().unwrap()));
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::appender::{FastLogRecord, LogAppender};
pub struct ConsoleAppender {}

impl LogAppender for ConsoleAppender {
fn do_log(&self, record: &mut FastLogRecord) {
fn do_log(&self, record: &FastLogRecord) {
print!("{}", record.formated);
}
}
2 changes: 1 addition & 1 deletion src/plugin/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl FileAppender {
}

impl LogAppender for FileAppender {
fn do_log(&self, record: &mut FastLogRecord) {
fn do_log(&self, record: &FastLogRecord) {
let mut log_file = self.file.borrow_mut();
let mut buf = vec![];
for x in record.formated.bytes() {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl FileLoopAppender {
}

impl LogAppender for FileLoopAppender {
fn do_log(&self, record: &mut FastLogRecord) {
fn do_log(&self, record: &FastLogRecord) {
self.file.do_log(record);
}
}
2 changes: 1 addition & 1 deletion src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl FileSplitAppender {
}

impl LogAppender for FileSplitAppender {
fn do_log(&self, record: &mut FastLogRecord) {
fn do_log(&self, record: &FastLogRecord) {
let mut data = self.cell.borrow_mut();
if record.command.eq(&Command::CommandFlush) || (data.temp_bytes >= data.max_split_bytes) {
data.send_pack();
Expand Down

0 comments on commit 5d3f4f5

Please sign in to comment.