diff --git a/README.md b/README.md index d70436b..a679517 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ the fast log . This crate uses #! [forbid(unsafe_code)] to ensure everything is A log implementation for extreme speed, using Crossbeam/channel ,once Batch write logs,fast log date, Appender architecture, appender per thread -* Low overhead, based on thread +* Low overhead, log write based on thread, also support tokio/Future * High performance, use lockless message queue, log is stored in queue, then flush disk. It does not block the caller diff --git a/example/src/bench_test.rs b/example/src/bench_test.rs index 567f2d8..780ad1c 100644 --- a/example/src/bench_test.rs +++ b/example/src/bench_test.rs @@ -1,7 +1,7 @@ -use fast_log::appender::{LogAppender, FastLogRecord}; -use std::time::{Instant}; +use fast_log::appender::{FastLogRecord, LogAppender}; use fast_log::bencher::QPS; use fast_log::config::Config; +use std::time::Instant; /// cargo run --release --package example --bin bench_test fn main() { @@ -11,7 +11,7 @@ fn main() { //nothing } } - fast_log::init(Config::new().custom(BenchRecvLog{})).unwrap(); + fast_log::init(Config::new().custom(BenchRecvLog {})).unwrap(); let total = 1000000; let now = Instant::now(); for index in 0..total { @@ -21,4 +21,4 @@ fn main() { log::logger().flush(); now.time(total); now.qps(total); -} \ No newline at end of file +} diff --git a/example/src/bench_test_file.rs b/example/src/bench_test_file.rs index 30f7857..d73b52e 100644 --- a/example/src/bench_test_file.rs +++ b/example/src/bench_test_file.rs @@ -1,6 +1,6 @@ -use std::time::{Instant}; use fast_log::bencher::QPS; use fast_log::config::Config; +use std::time::Instant; /// cargo run --release --package example --bin bench_test_file fn main() { @@ -16,4 +16,4 @@ fn main() { log::logger().flush(); now.time(total); now.qps(total); -} \ No newline at end of file +} diff --git a/example/src/bench_test_file_split.rs b/example/src/bench_test_file_split.rs index f9e2ad5..9ae42bf 100644 --- a/example/src/bench_test_file_split.rs +++ b/example/src/bench_test_file_split.rs @@ -1,18 +1,21 @@ -use std::time::Instant; use fast_log::bencher::QPS; use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::file_split::RollingType; use fast_log::plugin::packer::LogPacker; +use std::time::Instant; /// cargo run --release --package example --bin bench_test_file_split fn main() { //clear data - let _=std::fs::remove_dir("target/logs/"); - fast_log::init(Config::new().file_split("target/logs/", - LogSize::MB(1), - RollingType::All, - LogPacker{})).unwrap(); + let _ = std::fs::remove_dir("target/logs/"); + fast_log::init(Config::new().file_split( + "target/logs/", + LogSize::MB(1), + RollingType::All, + LogPacker {}, + )) + .unwrap(); log::info!("Commencing yak shaving{}", 0); let total = 1000000; let now = Instant::now(); @@ -23,4 +26,4 @@ fn main() { log::logger().flush(); now.time(total); now.qps(total); -} \ No newline at end of file +} diff --git a/example/src/custom_appender.rs b/example/src/custom_appender.rs index dbcafea..56ecc53 100644 --- a/example/src/custom_appender.rs +++ b/example/src/custom_appender.rs @@ -1,7 +1,7 @@ -use fast_log::appender::{LogAppender, FastLogRecord}; -use log::Level; use chrono::{DateTime, Local}; +use fast_log::appender::{FastLogRecord, LogAppender}; use fast_log::config::Config; +use log::Level; struct CustomLog {} @@ -14,11 +14,7 @@ impl LogAppender for CustomLog { Level::Warn | Level::Error => { data = format!( "{} {} {} - {} {}\n", - now, - record.level, - record.module_path, - record.args, - record.formated + now, record.level, record.module_path, record.args, record.formated ); } _ => { @@ -38,4 +34,4 @@ fn main() { log::info!("Commencing yak shaving"); log::error!("Commencing error"); log::logger().flush(); -} \ No newline at end of file +} diff --git a/example/src/custom_appender_meilisearch.rs b/example/src/custom_appender_meilisearch.rs index 571a80b..c589fa9 100644 --- a/example/src/custom_appender_meilisearch.rs +++ b/example/src/custom_appender_meilisearch.rs @@ -1,11 +1,11 @@ -use std::sync::Arc; -use fast_log::appender::{LogAppender, FastLogRecord}; -use log::Level; use chrono::{DateTime, Local}; +use fast_log::appender::{FastLogRecord, LogAppender}; +use fast_log::config::Config; +use log::Level; use meilisearch_sdk::client::Client; use meilisearch_sdk::document::Document; -use fast_log::config::Config; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; +use std::sync::Arc; #[derive(Serialize, Deserialize, Debug)] struct LogDoc { @@ -22,7 +22,6 @@ impl Document for LogDoc { } } - /// you should download run [download](https://github.com/meilisearch/Meilisearch/releases) /// /// or use docker command run meilisearch @@ -32,18 +31,23 @@ impl Document for LogDoc { #[tokio::main] async fn main() { let client = Client::new("http://localhost:7700", "masterKey"); - fast_log::init(Config::new().custom(CustomLog { - c: Arc::new(client), - rt: tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap(), - })).unwrap(); + fast_log::init( + Config::new().custom(CustomLog { + c: Arc::new(client), + rt: tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(), + }), + ) + .unwrap(); for index in 0..1000 { - log::info!("Commencing yak shaving:{}",index); - log::error!("Commencing error:{}",index); + log::info!("Commencing yak shaving:{}", index); + log::error!("Commencing error:{}", index); } log::logger().flush(); } - struct CustomLog { c: Arc, rt: tokio::runtime::Runtime, @@ -58,11 +62,7 @@ impl LogAppender for CustomLog { Level::Warn | Level::Error => { data = format!( "{} {} {} - {} {}\n", - now, - record.level, - record.module_path, - record.args, - record.formated + now, record.level, record.module_path, record.args, record.formated ); } _ => { @@ -75,7 +75,7 @@ impl LogAppender for CustomLog { let id = now.timestamp_millis() as usize; let c = self.c.clone(); self.rt.block_on(async move { - println!("id:{}",id); + println!("id:{}", id); let doc = c.index("LogDoc"); //send to web,file,any way let log = LogDoc { @@ -90,4 +90,4 @@ impl LogAppender for CustomLog { }); } } -} \ No newline at end of file +} diff --git a/example/src/custom_appender_tokio.rs b/example/src/custom_appender_tokio.rs index 2f4a605..d38754e 100644 --- a/example/src/custom_appender_tokio.rs +++ b/example/src/custom_appender_tokio.rs @@ -1,11 +1,11 @@ -use fast_log::appender::{LogAppender, FastLogRecord}; -use log::Level; use chrono::{DateTime, Local}; -use tokio::runtime::Runtime; +use fast_log::appender::{FastLogRecord, LogAppender}; use fast_log::config::Config; +use log::Level; +use tokio::runtime::Runtime; struct CustomLog { - rt:Runtime + rt: Runtime, } impl LogAppender for CustomLog { @@ -18,11 +18,7 @@ impl LogAppender for CustomLog { Level::Warn | Level::Error => { data = format!( "{} {} {} - {} {}\n", - now, - record.level, - record.module_path, - record.args, - record.formated + now, record.level, record.module_path, record.args, record.formated ); } _ => { @@ -43,10 +39,16 @@ impl LogAppender for CustomLog { #[tokio::main] async fn main() { - fast_log::init(Config::new().custom(CustomLog { - rt: tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap() - })).unwrap(); + fast_log::init( + Config::new().custom(CustomLog { + rt: tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(), + }), + ) + .unwrap(); log::info!("Commencing yak shaving"); log::error!("Commencing error"); log::logger().flush(); -} \ No newline at end of file +} diff --git a/example/src/format_log.rs b/example/src/format_log.rs index 052ad6e..5ea019b 100644 --- a/example/src/format_log.rs +++ b/example/src/format_log.rs @@ -1,11 +1,14 @@ -use log::LevelFilter; use fast_log::appender::FastLogFormat; use fast_log::config::Config; +use log::LevelFilter; fn main() { - fast_log::init(Config::new() - .format(FastLogFormat::new().set_display_line_level(LevelFilter::Trace) - ).console()).unwrap(); + fast_log::init( + Config::new() + .format(FastLogFormat::new().set_display_line_level(LevelFilter::Trace)) + .console(), + ) + .unwrap(); log::info!("Commencing yak shaving{}", 0); log::logger().flush(); -} \ No newline at end of file +} diff --git a/example/src/main.rs b/example/src/main.rs index 9581f43..380adbe 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -5,4 +5,4 @@ fn main() { log::info!("Commencing yak shaving{}", 0); fast_log::print("Commencing print\n".into()).expect("fast log not init"); log::logger().flush(); -} \ No newline at end of file +} diff --git a/example/src/split_log.rs b/example/src/split_log.rs index 94cf56b..4bc69a2 100644 --- a/example/src/split_log.rs +++ b/example/src/split_log.rs @@ -1,18 +1,19 @@ +use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::file_split::RollingType; -use fast_log::plugin::packer::{LogPacker}; -use fast_log::config::Config; +use fast_log::plugin::packer::LogPacker; -fn main(){ - fast_log::init(Config::new() - .console() - .file_split("target/logs/", - LogSize::MB(1), - RollingType::All, - LogPacker{})).unwrap(); +fn main() { + fast_log::init(Config::new().console().file_split( + "target/logs/", + LogSize::MB(1), + RollingType::All, + LogPacker {}, + )) + .unwrap(); for _ in 0..20000 { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/split_log_flush.rs b/example/src/split_log_flush.rs index 08441fa..63b8a55 100644 --- a/example/src/split_log_flush.rs +++ b/example/src/split_log_flush.rs @@ -1,15 +1,16 @@ +use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::file_split::RollingType; -use fast_log::plugin::packer::{LogPacker}; -use fast_log::config::Config; +use fast_log::plugin::packer::LogPacker; -fn main(){ - fast_log::init(Config::new() - .console() - .file_split("target/logs/", - LogSize::MB(1), - RollingType::All, - LogPacker{})).unwrap(); +fn main() { + fast_log::init(Config::new().console().file_split( + "target/logs/", + LogSize::MB(1), + RollingType::All, + LogPacker {}, + )) + .unwrap(); for _ in 0..20000 { log::info!("Commencing yak shaving"); } @@ -23,5 +24,5 @@ fn main(){ // /// wait.wait(); // /// // wg.wait(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/split_log_gz.rs b/example/src/split_log_gz.rs index 918a45d..61af03b 100644 --- a/example/src/split_log_gz.rs +++ b/example/src/split_log_gz.rs @@ -1,18 +1,19 @@ +use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::file_split::RollingType; -use fast_log::config::Config; use fast_log::plugin::packer::GZipPacker; -fn main(){ - fast_log::init(Config::new() - .console() - .file_split("target/logs/", - LogSize::KB(50), - RollingType::KeepNum(5), - GZipPacker{})).unwrap(); +fn main() { + fast_log::init(Config::new().console().file_split( + "target/logs/", + LogSize::KB(50), + RollingType::KeepNum(5), + GZipPacker {}, + )) + .unwrap(); for _ in 0..20000 { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/split_log_loop.rs b/example/src/split_log_loop.rs index e915d77..0125037 100644 --- a/example/src/split_log_loop.rs +++ b/example/src/split_log_loop.rs @@ -1,14 +1,17 @@ -use fast_log::consts::LogSize; use fast_log::config::Config; +use fast_log::consts::LogSize; ///Single logs are stored in rolling mode by capacity -fn main(){ - fast_log::init(Config::new() - .console() - .file_loop("target/logs/sloop.log",LogSize::KB(1))).unwrap(); +fn main() { + fast_log::init( + Config::new() + .console() + .file_loop("target/logs/sloop.log", LogSize::KB(1)), + ) + .unwrap(); for _ in 0..80000 { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/split_log_lz4.rs b/example/src/split_log_lz4.rs index 84bd747..89ca026 100644 --- a/example/src/split_log_lz4.rs +++ b/example/src/split_log_lz4.rs @@ -1,18 +1,19 @@ +use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::file_split::RollingType; use fast_log::plugin::packer::LZ4Packer; -use fast_log::config::Config; -fn main(){ - fast_log::init(Config::new() - .console() - .file_split("target/logs/", - LogSize::KB(50), - RollingType::KeepNum(5), - LZ4Packer{})).unwrap(); +fn main() { + fast_log::init(Config::new().console().file_split( + "target/logs/", + LogSize::KB(50), + RollingType::KeepNum(5), + LZ4Packer {}, + )) + .unwrap(); for _ in 0..20000 { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/split_log_zip.rs b/example/src/split_log_zip.rs index a8d5190..eb6c55b 100644 --- a/example/src/split_log_zip.rs +++ b/example/src/split_log_zip.rs @@ -4,16 +4,17 @@ use fast_log::plugin::packer::ZipPacker; use fast_log::config::Config; -fn main(){ - fast_log::init(Config::new() - .console() - .file_split("target/logs/", - LogSize::KB(50), - RollingType::KeepNum(5), - ZipPacker{})).unwrap(); +fn main() { + fast_log::init(Config::new().console().file_split( + "target/logs/", + LogSize::KB(50), + RollingType::KeepNum(5), + ZipPacker {}, + )) + .unwrap(); for _ in 0..20000 { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}","target/logs/") -} \ No newline at end of file + println!("you can see log files in path: {}", "target/logs/") +} diff --git a/example/src/wait_log.rs b/example/src/wait_log.rs index 3c32aec..e29a133 100644 --- a/example/src/wait_log.rs +++ b/example/src/wait_log.rs @@ -1,11 +1,14 @@ -use log::LevelFilter; use fast_log::config::Config; +use log::LevelFilter; -fn main(){ - fast_log::init(Config::new() - .level(LevelFilter::Debug) - .console() - .file("target/requests.log")).unwrap(); +fn main() { + fast_log::init( + Config::new() + .level(LevelFilter::Debug) + .console() + .file("target/requests.log"), + ) + .unwrap(); log::debug!("Commencing yak shaving{}", 0); log::logger().flush(); -} \ No newline at end of file +} diff --git a/src/appender.rs b/src/appender.rs index 426d590..051d799 100644 --- a/src/appender.rs +++ b/src/appender.rs @@ -1,9 +1,9 @@ -use log::{LevelFilter}; -use std::time::{Duration, SystemTime}; -use std::ops::{Add, Sub}; -use crossbeam_utils::sync::WaitGroup; use crate::appender::Command::CommandRecord; use crate::date; +use crossbeam_utils::sync::WaitGroup; +use log::LevelFilter; +use std::ops::{Add, Sub}; +use std::time::{Duration, SystemTime}; /// LogAppender append logs /// Appender will be running on single main thread,please do_log for new thread or new an Future @@ -26,9 +26,9 @@ pub enum Command { impl Command { pub fn to_i32(&self) -> i32 { match self { - Command::CommandRecord => { 1 } - Command::CommandExit => { 2 } - Command::CommandFlush(_) => { 3 } + Command::CommandRecord => 1, + Command::CommandExit => 2, + Command::CommandFlush(_) => 3, } } } @@ -66,7 +66,6 @@ pub struct FastLogFormat { pub display_line_level: log::LevelFilter, } - impl RecordFormat for FastLogFormat { fn do_format(&self, arg: &mut FastLogRecord) -> String { match arg.command { @@ -123,4 +122,4 @@ impl FastLogFormat { self.duration_zone = duration; self } -} \ No newline at end of file +} diff --git a/src/config.rs b/src/config.rs index 0b1a300..f5bd6c2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,3 @@ -use log::{LevelFilter}; use crate::appender::{FastLogFormat, LogAppender, RecordFormat}; use crate::consts::LogSize; use crate::filter::{Filter, NoFilter}; @@ -6,6 +5,7 @@ use crate::plugin::console::ConsoleAppender; use crate::plugin::file::FileAppender; use crate::plugin::file_loop::FileLoopAppender; use crate::plugin::file_split::{FileSplitAppender, Packer, RollingType}; +use log::LevelFilter; pub struct Config { pub appends: Vec>, @@ -59,15 +59,24 @@ impl Config { } /// add a FileLoopAppender pub fn file_loop(mut self, file: &str, max_temp_size: LogSize) -> Self { - self.appends.push(Box::new(FileLoopAppender::new(file, max_temp_size))); + self.appends + .push(Box::new(FileLoopAppender::new(file, max_temp_size))); self } /// add a FileSplitAppender - pub fn file_split(mut self, file_path: &str, - max_temp_size: LogSize, - rolling_type: RollingType, - packer: P, ) -> Self { - self.appends.push(Box::new(FileSplitAppender::new(file_path, max_temp_size, rolling_type, Box::new(packer)))); + pub fn file_split( + mut self, + file_path: &str, + max_temp_size: LogSize, + rolling_type: RollingType, + packer: P, + ) -> Self { + self.appends.push(Box::new(FileSplitAppender::new( + file_path, + max_temp_size, + rolling_type, + Box::new(packer), + ))); self } /// add a custom LogAppender @@ -81,4 +90,4 @@ impl Config { self.chan_len = len; self } -} \ No newline at end of file +} diff --git a/src/date.rs b/src/date.rs index f30f39e..d73cc92 100644 --- a/src/date.rs +++ b/src/date.rs @@ -148,8 +148,8 @@ impl From for SystemTime { let days = (v.year as u64 - 1970) * 365 + leap_years as u64 + ydays; UNIX_EPOCH + Duration::from_secs( - v.sec as u64 + v.min as u64 * 60 + v.hour as u64 * 3600 + days * 86400, - ) + v.sec as u64 + v.min as u64 * 60 + v.hour as u64 * 3600 + days * 86400, + ) } } @@ -170,25 +170,46 @@ impl FromStr for LogDate { }; let bytes = s.as_bytes(); if bytes.len() > 4 { - if let Ok(year) = std::str::from_utf8(&bytes[0..4]).unwrap_or_default().parse::() { + if let Ok(year) = std::str::from_utf8(&bytes[0..4]) + .unwrap_or_default() + .parse::() + { date.year = year; } - if let Ok(mon) = std::str::from_utf8(&bytes[5..7]).unwrap_or_default().parse::() { + if let Ok(mon) = std::str::from_utf8(&bytes[5..7]) + .unwrap_or_default() + .parse::() + { date.mon = mon; } - if let Ok(day) = std::str::from_utf8(&bytes[8..10]).unwrap_or_default().parse::() { + if let Ok(day) = std::str::from_utf8(&bytes[8..10]) + .unwrap_or_default() + .parse::() + { date.day = day; } - if let Ok(hour) = std::str::from_utf8(&bytes[11..13]).unwrap_or_default().parse::() { + if let Ok(hour) = std::str::from_utf8(&bytes[11..13]) + .unwrap_or_default() + .parse::() + { date.hour = hour; } - if let Ok(min) = std::str::from_utf8(&bytes[14..16]).unwrap_or_default().parse::() { + if let Ok(min) = std::str::from_utf8(&bytes[14..16]) + .unwrap_or_default() + .parse::() + { date.min = min; } - if let Ok(sec) = std::str::from_utf8(&bytes[17..19]).unwrap_or_default().parse::() { + if let Ok(sec) = std::str::from_utf8(&bytes[17..19]) + .unwrap_or_default() + .parse::() + { date.sec = sec; } - if let Ok(ns) = std::str::from_utf8(&bytes[20..29]).unwrap_or_default().parse::() { + if let Ok(ns) = std::str::from_utf8(&bytes[20..29]) + .unwrap_or_default() + .parse::() + { date.nano = ns; } } @@ -196,7 +217,6 @@ impl FromStr for LogDate { } } - impl Display for LogDate { /// fmt RFC3339Nano = "2006-01-02T15:04:05.999999999" fn fmt(&self, f: &mut Formatter) -> fmt::Result { @@ -207,7 +227,6 @@ impl Display for LogDate { buf[2] = b'0' + (self.year / 10 % 10) as u8; buf[3] = b'0' + (self.year % 10) as u8; - buf[5] = b'0' + (self.mon / 10) as u8; buf[6] = b'0' + (self.mon % 10) as u8; @@ -249,48 +268,14 @@ impl PartialOrd for LogDate { } } -fn toint_1(x: u8) -> Result { - let result = x.wrapping_sub(b'0'); - if result < 10 { - Ok(result) - } else { - Err(Error::default()) - } -} - -fn toint_2(s: &[u8]) -> Result { - let high = s[0].wrapping_sub(b'0'); - let low = s[1].wrapping_sub(b'0'); - - if high < 10 && low < 10 { - Ok(high * 10 + low) - } else { - Err(Error::default()) - } -} - -#[allow(clippy::many_single_char_names)] -fn toint_4(s: &[u8]) -> Result { - let a = u16::from(s[0].wrapping_sub(b'0')); - let b = u16::from(s[1].wrapping_sub(b'0')); - let c = u16::from(s[2].wrapping_sub(b'0')); - let d = u16::from(s[3].wrapping_sub(b'0')); - - if a < 10 && b < 10 && c < 10 && d < 10 { - Ok(a * 1000 + b * 100 + c * 10 + d) - } else { - Err(Error::default()) - } -} - fn is_leap_year(y: u16) -> bool { y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) } #[cfg(test)] mod test { - use std::str::FromStr; use crate::date::LogDate; + use std::str::FromStr; #[test] fn test_date() { @@ -299,5 +284,3 @@ mod test { assert_eq!("1234-12-13 11:12:13.112345678".to_string(), d.to_string()); } } - - diff --git a/src/error.rs b/src/error.rs index c3a710e..f226dca 100644 --- a/src/error.rs +++ b/src/error.rs @@ -69,4 +69,4 @@ where Ok(o) => Ok(o.clone()), }; } -} \ No newline at end of file +} diff --git a/src/fast_log.rs b/src/fast_log.rs index 3e3d934..d159d6b 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -1,18 +1,18 @@ +use log::{LevelFilter, Log, Metadata, Record}; use std::ops::{Deref, DerefMut}; use std::sync::atomic::{AtomicI32, AtomicI64, Ordering}; -use log::{LevelFilter, Log, Metadata, Record}; use crate::appender::{Command, FastLogRecord, RecordFormat}; +use crate::config::Config; use crate::error::LogError; -use crate::filter::{Filter}; -use std::result::Result::Ok; -use std::time::{SystemTime}; -use std::sync::Arc; +use crate::filter::Filter; +use crate::{chan, spawn, Receiver, Sender}; use crossbeam_channel::SendError; use crossbeam_utils::sync::WaitGroup; use once_cell::sync::{Lazy, OnceCell}; -use crate::{chan, Receiver, Sender, spawn}; -use crate::config::Config; +use std::result::Result::Ok; +use std::sync::Arc; +use std::time::SystemTime; pub struct Chan { pub filter: OnceCell>, @@ -115,24 +115,17 @@ impl Log for Logger { } static CHAN_LEN: AtomicI64 = AtomicI64::new(-1); -static LOGGER: Lazy = Lazy::new(|| { - Logger { - level: AtomicI32::new(1), - chan: Chan::new({ - let len = CHAN_LEN.load(Ordering::SeqCst); - match len { - -1 => { - None - } - v => { - Some(v as usize) - } - } - }), - } +static LOGGER: Lazy = Lazy::new(|| Logger { + level: AtomicI32::new(1), + chan: Chan::new({ + let len = CHAN_LEN.load(Ordering::SeqCst); + match len { + -1 => None, + v => Some(v as usize), + } + }), }); - pub fn init(config: Config) -> Result<&'static Logger, LogError> { if config.appends.is_empty() { return Err(LogError::from("[fast_log] appenders can not be empty!")); @@ -239,7 +232,6 @@ fn recv_all(data: &mut Vec, recver: &Receiver) { } } - pub fn exit() -> Result<(), LogError> { let fast_log_record = FastLogRecord { command: Command::CommandExit, @@ -262,7 +254,6 @@ pub fn exit() -> Result<(), LogError> { return Err(LogError::E("[fast_log] exit fail!".to_string())); } - pub fn flush() -> Result { let wg = WaitGroup::new(); let fast_log_record = FastLogRecord { @@ -288,4 +279,4 @@ pub fn flush() -> Result { pub fn print(log: String) -> Result<(), SendError> { LOGGER.print(log) -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index c7898ba..92bd1bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,16 +7,15 @@ extern crate core; pub mod appender; pub mod bencher; +pub mod config; pub mod consts; +pub mod date; pub mod error; pub mod fast_log; pub mod filter; pub mod plugin; pub mod runtime; -pub mod config; -pub mod date; +pub use crate::config::Config; pub use crate::fast_log::*; pub use runtime::*; -pub use crate::config::Config; - diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index f7a8e6a..d919501 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -11,7 +11,12 @@ pub struct FileLoopAppender { impl FileLoopAppender { pub fn new(log_file_path: &str, max_temp_size: LogSize) -> FileLoopAppender { Self { - file: FileSplitAppender::new(log_file_path, max_temp_size, RollingType::KeepNum(1), Box::new(LogPacker {})) + file: FileSplitAppender::new( + log_file_path, + max_temp_size, + RollingType::KeepNum(1), + Box::new(LogPacker {}), + ), } } } diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 1c81de9..405d91a 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -6,10 +6,10 @@ use chrono::{Local, NaiveDateTime}; use crate::appender::{Command, FastLogRecord, LogAppender}; use crate::consts::LogSize; -use std::ops::{Sub}; -use std::time::{Duration}; use crate::error::LogError; use crate::{chan, Receiver, Sender}; +use std::ops::Sub; +use std::time::Duration; /// .zip or .lz4 or any one packer pub trait Packer: Send { @@ -17,7 +17,9 @@ pub trait Packer: Send { //return bool: remove_log_file fn do_pack(&self, log_file: File, log_file_path: &str) -> Result; /// default 0 is not retry pack. if retry > 0 ,it will trying rePack - fn retry(&self) -> i32 { return 0; } + fn retry(&self) -> i32 { + return 0; + } } /// split log file allow compress log @@ -56,7 +58,10 @@ impl RollingType { Ok(path) => { if let Some(v) = path.file_name().to_str() { //filter temp.log and not start with temp - if (v.ends_with(".log") && v.trim_end_matches(".log").ends_with(temp_name)) || !v.starts_with(temp_name) { + if (v.ends_with(".log") + && v.trim_end_matches(".log").ends_with(temp_name)) + || !v.starts_with(temp_name) + { continue; } } @@ -174,7 +179,8 @@ impl FileSplitAppender { let mut dir_path = file_path.to_owned(); let mut temp_file_name = dir_path.to_string(); if dir_path.contains("/") { - let new_dir_path = dir_path[0..dir_path.rfind("/").unwrap_or_default()].to_string() + "/"; + let new_dir_path = + dir_path[0..dir_path.rfind("/").unwrap_or_default()].to_string() + "/"; std::fs::create_dir_all(&new_dir_path); temp_file_name = dir_path.trim_start_matches(&new_dir_path).to_string(); dir_path = new_dir_path; @@ -329,4 +335,4 @@ pub fn do_pack(packer: &Box, mut pack: LogPack) -> Result &'static str { "log" @@ -18,12 +16,10 @@ impl Packer for LogPacker { } } - - -#[cfg(feature = "zip")] -use zip::write::FileOptions; #[cfg(feature = "zip")] use zip::result::ZipResult; +#[cfg(feature = "zip")] +use zip::write::FileOptions; /// you need enable fast_log = { ... ,features=["zip"]} /// the zip compress @@ -55,18 +51,20 @@ impl Packer for ZipPacker { let mut zip = zip::ZipWriter::new(zip_file); zip.start_file(log_name, FileOptions::default()); //buf reader - std::io::copy(&mut log_file,&mut zip); + std::io::copy(&mut log_file, &mut zip); zip.flush(); let finish: ZipResult = zip.finish(); if finish.is_err() { //println!("[fast_log] try zip fail{:?}", finish.err()); - return Err(LogError::from(format!("[fast_log] try zip fail{:?}", finish.err()))); + return Err(LogError::from(format!( + "[fast_log] try zip fail{:?}", + finish.err() + ))); } return Ok(true); } } - /// you need enable fast_log = { ... ,features=["lz4"]} #[cfg(feature = "lz4")] use lz4::EncoderBuilder; @@ -105,32 +103,31 @@ impl Packer for LZ4Packer { let lz4_file = lz4_file.unwrap(); //write lz4 bytes data - let mut encoder = EncoderBuilder::new() - .level(0) - .build(lz4_file)?; + let mut encoder = EncoderBuilder::new().level(0).build(lz4_file)?; // io::copy(&mut lz4_file, &mut encoder)?; //buf reader - std::io::copy(&mut log_file,&mut encoder); + std::io::copy(&mut log_file, &mut encoder); let (_output, result) = encoder.finish(); if result.is_err() { - return Err(LogError::from(format!("[fast_log] try zip fail{:?}", result.err()))); + return Err(LogError::from(format!( + "[fast_log] try zip fail{:?}", + result.err() + ))); } return Ok(true); } } - #[cfg(feature = "gzip")] use flate2::write::GzEncoder; #[cfg(feature = "gzip")] use flate2::Compression; - #[cfg(feature = "gzip")] pub struct GZipPacker {} #[cfg(feature = "gzip")] -impl Packer for GZipPacker{ +impl Packer for GZipPacker { fn pack_name(&self) -> &'static str { "gz" } @@ -151,13 +148,16 @@ impl Packer for GZipPacker{ } let zip_file = zip_file.unwrap(); //write zip bytes data - let mut zip = GzEncoder::new(zip_file,Compression::default()); - std::io::copy(&mut log_file,&mut zip); + let mut zip = GzEncoder::new(zip_file, Compression::default()); + std::io::copy(&mut log_file, &mut zip); zip.flush(); let finish = zip.finish(); if finish.is_err() { - return Err(LogError::from(format!("[fast_log] try zip fail{:?}", finish.err()))); + return Err(LogError::from(format!( + "[fast_log] try zip fail{:?}", + finish.err() + ))); } return Ok(true); } -} \ No newline at end of file +} diff --git a/src/runtime.rs b/src/runtime.rs index ef1729d..e48823e 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -10,14 +10,10 @@ pub type JoinHandle = std::thread::JoinHandle; pub type WaitGroup = crossbeam_utils::sync::WaitGroup; #[cfg(feature = "runtime_thread")] -pub fn chan(len:Option) -> (Sender, Receiver) { - match len{ - None => { - crossbeam::channel::unbounded() - } - Some(len) => { - crossbeam::channel::bounded(len) - } +pub fn chan(len: Option) -> (Sender, Receiver) { + match len { + None => crossbeam::channel::unbounded(), + Some(len) => crossbeam::channel::bounded(len), } } @@ -27,11 +23,17 @@ pub fn sleep(d: Duration) { } #[cfg(feature = "runtime_thread")] -pub fn spawn(f: F) -> JoinHandle<()> where F: FnOnce() + std::marker::Send + 'static { +pub fn spawn(f: F) -> JoinHandle<()> +where + F: FnOnce() + std::marker::Send + 'static, +{ std::thread::spawn(f) } #[cfg(feature = "runtime_thread")] -pub fn spawn_stack_size(f: F, stack_size:usize) -> JoinHandle<()> where F: FnOnce() + std::marker::Send + 'static { +pub fn spawn_stack_size(f: F, stack_size: usize) -> JoinHandle<()> +where + F: FnOnce() + std::marker::Send + 'static, +{ std::thread::spawn(f) -} \ No newline at end of file +}