Skip to content

Commit

Permalink
support choose runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jan 7, 2022
1 parent f9555e3 commit c9e8506
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 37 deletions.
7 changes: 5 additions & 2 deletions 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.4.12"
version = "1.4.13"
description = "Rust async log High-performance asynchronous logging"
readme = "Readme.md"
authors = ["ce <[email protected]>"]
Expand All @@ -15,9 +15,12 @@ license = "MIT"


[features]
default = ["zip","cogo"]
default = ["zip","runtime_cogo"]
gzip = ["flate2"]

runtime_thread = []
runtime_cogo = ["cogo"]

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
lazy_static = "1.4.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn test_file_compation() {
for _ in 0..200000 {
info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
}
```

Expand All @@ -129,6 +129,6 @@ impl LogAppender for CustomLog{
fn main(){
fast_log::init_custom_log(vec![Box::new(CustomLog {})], log::Level::Info, Box::new(NoFilter {}));
info!("Commencing yak shaving");
cogo::coroutine::sleep(std::time::Duration::from_secs(1));
sleep(std::time::Duration::from_secs(1));
}
```
4 changes: 2 additions & 2 deletions README_CH.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main(){
for _ in 0..200000 {
info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
}
```

Expand All @@ -96,7 +96,7 @@ use log::{error, info, warn};
fn main(){
fast_log::init_custom_log(vec![Box::new(CustomLog {})], log::Level::Info, Box::new(NoFilter {}));
info!("Commencing yak shaving");
cogo::coroutine::sleep(std::time::Duration::from_secs(1));
sleep(std::time::Duration::from_secs(1));
}
```

3 changes: 2 additions & 1 deletion example/src/bench_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use fast_log::filter::NoFilter;
use fast_log::appender::{FastLogFormatRecord, LogAppender, FastLogRecord};
use std::time::{Instant, Duration};
use fast_log::bencher::QPS;
use fast_log::sleep;

struct BenchRecvLog {}

Expand All @@ -26,5 +27,5 @@ fn main() {
}
now.time(total);
now.qps(total);
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
}
2 changes: 1 addition & 1 deletion example/src/split_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main(){
for _ in 0..20000 {
log::info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(3));
sleep(Duration::from_secs(3));
println!("you can see log files in path: {}","target/logs/")
}
2 changes: 1 addition & 1 deletion example/src/split_log_flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fn main(){
/// let wait = fast_log::init_split_log(...);
/// wait.wait();
///
cogo::coroutine::sleep(Duration::from_secs(3));
sleep(Duration::from_secs(3));
println!("you can see log files in path: {}","target/logs/")
}
2 changes: 1 addition & 1 deletion example/src/split_log_gz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main(){
for _ in 0..20000 {
log::info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
println!("you can see log files in path: {}","target/logs/")
}
2 changes: 1 addition & 1 deletion example/src/split_log_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main(){
for _ in 0..80000 {
log::info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(3));
sleep(Duration::from_secs(3));
println!("you can see log files in path: {}","target/logs/")
}
2 changes: 1 addition & 1 deletion example/src/split_log_lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main(){
for _ in 0..20000 {
log::info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
println!("you can see log files in path: {}","target/logs/")
}
2 changes: 1 addition & 1 deletion example/src/split_log_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main(){
for _ in 0..20000 {
log::info!("Commencing yak shaving");
}
cogo::coroutine::sleep(Duration::from_secs(1));
sleep(Duration::from_secs(1));
println!("you can see log files in path: {}","target/logs/")
}
34 changes: 13 additions & 21 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::result::Result::Ok;
use std::time::{SystemTime, Duration};
use std::sync::Arc;
use std::sync::mpsc::SendError;
use crate::{chan, Sender, spawn};

lazy_static! {
static ref LOG_SENDER: RwLock<Option<LoggerSender>> = RwLock::new(Option::None);
Expand Down Expand Up @@ -73,11 +74,6 @@ impl log::Log for Logger {
//send
if let Some(sender) = LOG_SENDER.read().as_ref() {
if !sender.filter.filter(record) {
if let Some(v) = record.module_path() {
if v == "cogo::io::sys::select" {
return;
}
}
let fast_log_record = FastLogRecord {
command: Command::CommandRecord,
level: record.level(),
Expand Down Expand Up @@ -177,9 +173,9 @@ pub fn init_custom_log(
let wait_group_back = wait_group.clone();
std::thread::spawn(move || {
let mut recever_vec = vec![];
let mut sender_vec: Vec<cogo::std::sync::mpsc::Sender<Arc<FastLogRecord>>> = vec![];
let mut sender_vec: Vec<Sender<Arc<FastLogRecord>>> = vec![];
for a in appenders {
let (s, r) = cogo::std::sync::mpsc::channel();
let (s, r) = chan();
sender_vec.push(s);
recever_vec.push((r, a));
}
Expand All @@ -195,24 +191,22 @@ pub fn init_custom_log(
break;
}
appender.do_log(msg.as_ref());
} else {
cogo::coroutine::yield_now();
}
}
});
} else {
// if is network appender, use thread spawn
cogo::go!(cogo::coroutine::Builder::new().stack_size(2*0x1000),move ||{
loop{
if let Ok(msg) = recever.recv(){
if msg.command.eq(&Command::CommandExit) {
drop(current_wait_group);
break;
}
appender.do_log(msg.as_ref());
spawn(move || {
loop {
if let Ok(msg) = recever.recv() {
if msg.command.eq(&Command::CommandExit) {
drop(current_wait_group);
break;
}
appender.do_log(msg.as_ref());
}
}
}
});
});
}
}
loop {
Expand All @@ -228,8 +222,6 @@ pub fn init_custom_log(
drop(wait_group_back);
break;
}
} else {
cogo::coroutine::yield_now();
}
}
});
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ pub mod fast_log;
pub mod filter;
pub mod plugin;
pub mod wait;
pub mod runtime;

pub use fast_log::*;
pub use runtime::*;

7 changes: 4 additions & 3 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::ops::Sub;
use std::time::Duration;
use zip::result::ZipResult;
use crate::error::LogError;
use crate::{chan, Receiver, Sender};

/// .zip or .lz4 or any one packer
pub trait Packer: Send {
Expand Down Expand Up @@ -127,7 +128,7 @@ pub struct FileSplitAppenderData {
max_split_bytes: usize,
dir_path: String,
file: File,
sender: cogo::std::sync::mpmc::Sender<LogPack>,
sender: Sender<LogPack>,
rolling_type: RollingType,
//cache data
temp_bytes: usize,
Expand Down Expand Up @@ -210,7 +211,7 @@ impl FileSplitAppender {
temp_bytes = m.len() as usize;
}
file.seek(SeekFrom::Start(temp_bytes as u64));
let (sender, receiver) = cogo::chan!();
let (sender, receiver) = chan();
spawn_saver(file_name,receiver, packer);
Self {
cell: RefCell::new(FileSplitAppenderData {
Expand Down Expand Up @@ -244,7 +245,7 @@ impl LogAppender for FileSplitAppender {
}

///spawn an saver thread to save log file or zip file
fn spawn_saver(temp_name: &str, r: cogo::std::sync::mpmc::Receiver<LogPack>, packer: Box<dyn Packer>) {
fn spawn_saver(temp_name: &str, r: Receiver<LogPack>, packer: Box<dyn Packer>) {
let temp = temp_name.to_string();
std::thread::spawn(move || {
loop {
Expand Down
43 changes: 43 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::time::Duration;

/// if use cogo runtime
#[cfg(feature = "cogo")]
pub type Receiver<T> = cogo::std::sync::mpmc::Receiver<T>;
#[cfg(feature = "cogo")]
pub type Sender<T> = cogo::std::sync::mpmc::Sender<T>;

#[cfg(feature = "cogo")]
pub fn chan<T>() -> (Sender<T>, Receiver<T>) {
cogo::chan!()
}

#[cfg(feature = "cogo")]
pub fn sleep(d: Duration) {
cogo::coroutine::sleep(d)
}

#[cfg(feature = "cogo")]
pub fn spawn<F>(f: F) where F: FnOnce() + std::marker::Send + 'static {
cogo::go!(cogo::coroutine::Builder::new().stack_size(2*0x1000),f);
}

/// if not cogo
#[cfg(not(feature = "cogo"))]
pub type Receiver<T> = crossbeam::channel::Receiver<T>;
#[cfg(not(feature = "cogo"))]
pub type Sender<T> = crossbeam::channel::Sender<T>;

#[cfg(not(feature = "cogo"))]
pub fn chan<T>() -> (Sender<T>, Receiver<T>) {
cogo::chan!()
}

#[cfg(not(feature = "cogo"))]
pub fn sleep(d: Duration) {
std::thread::sleep(d)
}

#[cfg(not(feature = "cogo"))]
pub fn spawn<F>(f: F) where F: FnOnce() + std::marker::Send + 'static {
std::thread::spawn(f);
}

0 comments on commit c9e8506

Please sign in to comment.