Skip to content

Commit

Permalink
add trait Keep
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 19, 2023
1 parent 3dbacc1 commit a93db25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/src/split_log_gz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
}
17 changes: 9 additions & 8 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,23 @@ pub struct LogPack {

impl LogPack {
/// write an Pack to zip file
pub fn do_pack<P: Packer>(mut self, packer: &P) -> Result<bool, LogPack> {
pub fn do_pack<P: Packer>(&self, packer: &P) -> Result<bool, LogError> {
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -437,8 +438,6 @@ fn spawn_saver<P: Packer + Sync + 'static, R: Keep + 'static>(
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());
Expand All @@ -447,6 +446,8 @@ fn spawn_saver<P: Packer + Sync + 'static, R: Keep + 'static>(
std::fs::remove_file(log_file_path);
}
}
//do rolling
rolling.do_keep(&pack.dir, &temp_name);
} else {
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/keep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit a93db25

Please sign in to comment.