diff --git a/Cargo.toml b/Cargo.toml index b4129c0..8d31521 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] @@ -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" diff --git a/README.md b/README.md index 35d5888..91e4618 100644 --- a/README.md +++ b/README.md @@ -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)); } ``` @@ -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)); } ``` diff --git a/README_CH.md b/README_CH.md index 311a761..8ebde71 100644 --- a/README_CH.md +++ b/README_CH.md @@ -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)); } ``` @@ -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)); } ``` diff --git a/example/src/bench_test.rs b/example/src/bench_test.rs index 178ce02..6128c5a 100644 --- a/example/src/bench_test.rs +++ b/example/src/bench_test.rs @@ -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 {} @@ -26,5 +27,5 @@ fn main() { } now.time(total); now.qps(total); - cogo::coroutine::sleep(Duration::from_secs(1)); + sleep(Duration::from_secs(1)); } \ No newline at end of file diff --git a/example/src/split_log.rs b/example/src/split_log.rs index c0a460d..e00082c 100644 --- a/example/src/split_log.rs +++ b/example/src/split_log.rs @@ -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/") } \ No newline at end of file diff --git a/example/src/split_log_flush.rs b/example/src/split_log_flush.rs index 0a07be1..8106ab6 100644 --- a/example/src/split_log_flush.rs +++ b/example/src/split_log_flush.rs @@ -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/") } \ No newline at end of file diff --git a/example/src/split_log_gz.rs b/example/src/split_log_gz.rs index 49c6333..be1554e 100644 --- a/example/src/split_log_gz.rs +++ b/example/src/split_log_gz.rs @@ -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/") } \ No newline at end of file diff --git a/example/src/split_log_loop.rs b/example/src/split_log_loop.rs index bf2fa7b..24f2c68 100644 --- a/example/src/split_log_loop.rs +++ b/example/src/split_log_loop.rs @@ -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/") } \ No newline at end of file diff --git a/example/src/split_log_lz4.rs b/example/src/split_log_lz4.rs index 2b73c7a..f094303 100644 --- a/example/src/split_log_lz4.rs +++ b/example/src/split_log_lz4.rs @@ -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/") } \ No newline at end of file diff --git a/example/src/split_log_zip.rs b/example/src/split_log_zip.rs index 6a6ea74..e2c4c6c 100644 --- a/example/src/split_log_zip.rs +++ b/example/src/split_log_zip.rs @@ -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/") } \ No newline at end of file diff --git a/src/fast_log.rs b/src/fast_log.rs index 239841f..2cca785 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -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> = RwLock::new(Option::None); @@ -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(), @@ -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>> = vec![]; + let mut sender_vec: Vec>> = 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)); } @@ -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 { @@ -228,8 +222,6 @@ pub fn init_custom_log( drop(wait_group_back); break; } - } else { - cogo::coroutine::yield_now(); } } }); diff --git a/src/lib.rs b/src/lib.rs index e1bca40..cd5d177 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; + diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 99641c3..ee46b81 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -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 { @@ -127,7 +128,7 @@ pub struct FileSplitAppenderData { max_split_bytes: usize, dir_path: String, file: File, - sender: cogo::std::sync::mpmc::Sender, + sender: Sender, rolling_type: RollingType, //cache data temp_bytes: usize, @@ -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 { @@ -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, packer: Box) { +fn spawn_saver(temp_name: &str, r: Receiver, packer: Box) { let temp = temp_name.to_string(); std::thread::spawn(move || { loop { diff --git a/src/runtime.rs b/src/runtime.rs new file mode 100644 index 0000000..9da5661 --- /dev/null +++ b/src/runtime.rs @@ -0,0 +1,43 @@ +use std::time::Duration; + +/// if use cogo runtime +#[cfg(feature = "cogo")] +pub type Receiver = cogo::std::sync::mpmc::Receiver; +#[cfg(feature = "cogo")] +pub type Sender = cogo::std::sync::mpmc::Sender; + +#[cfg(feature = "cogo")] +pub fn chan() -> (Sender, Receiver) { + cogo::chan!() +} + +#[cfg(feature = "cogo")] +pub fn sleep(d: Duration) { + cogo::coroutine::sleep(d) +} + +#[cfg(feature = "cogo")] +pub fn spawn(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 = crossbeam::channel::Receiver; +#[cfg(not(feature = "cogo"))] +pub type Sender = crossbeam::channel::Sender; + +#[cfg(not(feature = "cogo"))] +pub fn chan() -> (Sender, Receiver) { + cogo::chan!() +} + +#[cfg(not(feature = "cogo"))] +pub fn sleep(d: Duration) { + std::thread::sleep(d) +} + +#[cfg(not(feature = "cogo"))] +pub fn spawn(f: F) where F: FnOnce() + std::marker::Send + 'static { + std::thread::spawn(f); +} \ No newline at end of file