From 48221555ae4138772bd626492c25b21f3653e5a8 Mon Sep 17 00:00:00 2001 From: zhuxiujia Date: Fri, 24 Sep 2021 17:22:15 +0800 Subject: [PATCH] opt code --- src/plugin/file.rs | 9 ++---- src/plugin/file_split.rs | 68 ++++++++++++++-------------------------- src/plugin/packer.rs | 21 +++++-------- 3 files changed, 35 insertions(+), 63 deletions(-) diff --git a/src/plugin/file.rs b/src/plugin/file.rs index d67e23a..59200b0 100644 --- a/src/plugin/file.rs +++ b/src/plugin/file.rs @@ -11,12 +11,9 @@ pub struct FileAppender { impl FileAppender { pub fn new(log_file_path: &str) -> FileAppender { let log_file_path = log_file_path.replace("\\", "/"); - match log_file_path.rfind("/") { - None => {} - Some(right) => { - let path = &log_file_path[0..right]; - std::fs::create_dir_all(path); - } + if let Some(right) = log_file_path.rfind("/") { + let path = &log_file_path[0..right]; + std::fs::create_dir_all(path); } Self { file: RefCell::new( diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 5031785..a535602 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -45,27 +45,24 @@ pub enum RollingType { impl RollingType { fn read_paths(&self, dir: &str) -> Vec { let paths = std::fs::read_dir(dir); - match paths { - Ok(paths) => { - let mut paths_vec = vec![]; - for path in paths { - match path { - Ok(path) => { - if let Some(v) = path.file_name().to_str() { - //filter temp.log and not start with temp - if v.ends_with("temp.log") || !v.starts_with("temp") { - continue; - } + if let Ok(paths) = paths { + let mut paths_vec = vec![]; + for path in paths { + match path { + Ok(path) => { + if let Some(v) = path.file_name().to_str() { + //filter temp.log and not start with temp + if v.ends_with("temp.log") || !v.starts_with("temp") { + continue; } - paths_vec.push(path); } - _ => {} + paths_vec.push(path); } + _ => {} } - paths_vec.sort_by(|a, b| b.file_name().cmp(&a.file_name())); - return paths_vec; } - _ => {} + paths_vec.sort_by(|a, b| b.file_name().cmp(&a.file_name())); + return paths_vec; } return vec![]; } @@ -93,13 +90,10 @@ impl RollingType { let item = &paths_vec[index]; let file_name = item.file_name(); let name = file_name.to_str().unwrap_or("").to_string(); - match self.file_name_parse_time(&name) { - Some(time) => { - if now.sub(time) > duration { - std::fs::remove_file(item.path()); - } + if let Some(time) = self.file_name_parse_time(&name) { + if now.sub(time) > duration { + std::fs::remove_file(item.path()); } - _ => {} } } } @@ -114,11 +108,8 @@ impl RollingType { time_str = time_str[0..v].to_string(); } let time = chrono::NaiveDateTime::parse_from_str(&time_str, "%Y_%m_%dT%H_%M_%S"); - match time { - Ok(time) => { - return Some(time); - } - _ => {} + if let Ok(time) = time { + return Some(time); } } return None; @@ -214,11 +205,8 @@ impl FileSplitAppender { } let mut file = file.unwrap(); let mut temp_bytes = 0; - match file.metadata() { - Ok(m) => { - temp_bytes = m.len() as usize; - } - _ => {} + if let Ok(m) = file.metadata() { + temp_bytes = m.len() as usize; } file.seek(SeekFrom::Start(temp_bytes as u64)); let (sender, receiver) = crossbeam_channel::bounded(log_pack_cap); @@ -247,11 +235,8 @@ impl LogAppender for FileSplitAppender { } let mut write_bytes = 0; let w = data.file.write(record.formated.as_bytes()); - match w { - Ok(w) => { - write_bytes = write_bytes + w; - } - _ => {} + if let Ok(w) = w { + write_bytes = write_bytes + w; } data.file.flush(); data.temp_bytes += write_bytes; @@ -267,12 +252,7 @@ fn spawn_saver_thread(r: Receiver, packer: Box) { pack.rolling.do_rolling(&pack.dir); let log_file_path = pack.new_log_name.clone(); //do save pack - match pack.pack_name.as_str() { - p_name => { - do_pack(&packer, pack); - } - _ => {} - } + do_pack(&packer, pack); std::fs::remove_file(log_file_path); } } @@ -280,7 +260,7 @@ fn spawn_saver_thread(r: Receiver, packer: Box) { } /// write an Pack to zip file -pub fn do_pack(packer: &Box, mut pack: LogPack) -> Result<(), LogPack>{ +pub fn do_pack(packer: &Box, mut pack: LogPack) -> Result<(), LogPack> { let log_file_path = pack.new_log_name.as_str(); if log_file_path.is_empty() { return Err(pack); diff --git a/src/plugin/packer.rs b/src/plugin/packer.rs index 2a07204..b93107d 100644 --- a/src/plugin/packer.rs +++ b/src/plugin/packer.rs @@ -21,11 +21,8 @@ impl Packer for ZipPacker { fn do_pack(&self, log_file: File, log_file_path: &str) -> Result<(), LogError> { let mut log_name = log_file_path.replace("\\", "/").to_string(); - match log_file_path.rfind("/") { - Some(v) => { - log_name = log_name[(v + 1)..log_name.len()].to_string(); - } - _ => {} + if let Some(v) = log_file_path.rfind("/") { + log_name = log_name[(v + 1)..log_name.len()].to_string(); } let zip_path = log_file_path.replace(".log", ".zip"); let zip_file = std::fs::File::create(&zip_path); @@ -64,12 +61,14 @@ impl Packer for ZipPacker { /// you need enable fast_log = { ... ,features=["lz4"]} #[cfg(feature = "lz4")] use lz4::EncoderBuilder; + #[cfg(feature = "lz4")] -impl From for LogError{ +impl From for LogError { fn from(arg: std::io::Error) -> Self { LogError::E(arg.to_string()) } } + /// the zip compress #[cfg(feature = "lz4")] pub struct LZ4Packer {} @@ -81,13 +80,9 @@ impl Packer for LZ4Packer { } fn do_pack(&self, log_file: File, log_file_path: &str) -> Result<(), LogError> { - let mut log_name = log_file_path.replace("\\", "/").to_string(); - match log_file_path.rfind("/") { - Some(v) => { - log_name = log_name[(v + 1)..log_name.len()].to_string(); - } - _ => {} + if let Some(v) = log_file_path.rfind("/") { + log_name = log_name[(v + 1)..log_name.len()].to_string(); } let lz4_path = log_file_path.replace(".log", ".lz4"); let lz4_file = std::fs::File::create(&lz4_path); @@ -104,7 +99,7 @@ impl Packer for LZ4Packer { let mut encoder = EncoderBuilder::new() .level(0) .build(lz4_file)?; - // io::copy(&mut lz4_file, &mut encoder)?; + // io::copy(&mut lz4_file, &mut encoder)?; //buf reader let mut r = BufReader::new(log_file); let mut buf = String::new();