From a93db2530ccf2ff95d5495087e3abc5f7d8c10a2 Mon Sep 17 00:00:00 2001 From: zxj Date: Tue, 19 Sep 2023 18:16:54 +0800 Subject: [PATCH] add trait Keep --- example/src/split_log_gz.rs | 2 +- src/plugin/file_split.rs | 17 +++++++++-------- src/plugin/keep.rs | 3 ++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/example/src/split_log_gz.rs b/example/src/split_log_gz.rs index c0090b5..b6f2e4e 100644 --- a/example/src/split_log_gz.rs +++ b/example/src/split_log_gz.rs @@ -15,5 +15,5 @@ fn main() { log::info!("Commencing yak shaving"); } log::logger().flush(); - println!("you can see log files in path: {}", "target/logs/") + println!("you can see log files in path: {}", "target/logs/"); } diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 7b13ac6..2242c67 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -261,21 +261,23 @@ pub struct LogPack { impl LogPack { /// write an Pack to zip file - pub fn do_pack(mut self, packer: &P) -> Result { + pub fn do_pack(&self, packer: &P) -> Result { let log_file_path = self.new_log_name.as_str(); if log_file_path.is_empty() { - return Err(self); + return Err(LogError::from("log_file_path.is_empty")); } let log_file = OpenOptions::new().read(true).open(log_file_path); if log_file.is_err() { - return Err(self); + return Err(LogError::from(format!( + "open(log_file_path={}) fail", + log_file_path + ))); } //make let r = packer.do_pack(log_file.unwrap(), log_file_path); if r.is_err() && packer.retry() > 0 { let mut retry = 1; while let Err(packs) = self.do_pack(packer) { - self = packs; retry += 1; if retry > packer.retry() { break; @@ -305,7 +307,6 @@ pub trait Keep: Send { Ok(path) => { if let Some(v) = path.file_name().to_str() { if v == temp_name { - //temp_file = Some(path); continue; } if !v.starts_with(&base_name) { @@ -340,7 +341,7 @@ pub enum RollingType { } impl Keep for RollingType { - fn do_keep(&self, temp_name: &str, dir: &str) -> i64 { + fn do_keep(&self, dir: &str, temp_name: &str) -> i64 { let mut removed = 0; match self { RollingType::KeepNum(n) => { @@ -437,8 +438,6 @@ fn spawn_saver( std::thread::spawn(move || { loop { if let Ok(pack) = r.recv() { - //do rolling - rolling.do_keep(&pack.dir, &temp_name); let log_file_path = pack.new_log_name.clone(); //do save pack let remove = pack.do_pack(packer.deref()); @@ -447,6 +446,8 @@ fn spawn_saver( std::fs::remove_file(log_file_path); } } + //do rolling + rolling.do_keep(&pack.dir, &temp_name); } else { break; } diff --git a/src/plugin/keep.rs b/src/plugin/keep.rs index d7273ef..9b1629f 100644 --- a/src/plugin/keep.rs +++ b/src/plugin/keep.rs @@ -20,7 +20,8 @@ impl Keep for KeepNum { for index in 0..paths_vec.len() { if index >= (self.0) as usize { let item = &paths_vec[index]; - std::fs::remove_file(item.path()); + let path = item.path(); + std::fs::remove_file(path); removed += 1; } }