From 9a4f01049493654ed3c52c947ceba7bfeef067b8 Mon Sep 17 00:00:00 2001 From: zxj Date: Tue, 19 Sep 2023 17:33:34 +0800 Subject: [PATCH] add trait Keep --- example/src/split_log.rs | 4 ++-- src/config.rs | 6 +++--- src/plugin/file_loop.rs | 4 ++-- src/plugin/file_split.rs | 14 +++++++------- src/plugin/{cleaner.rs => keep.rs} | 20 ++++++++++---------- src/plugin/mod.rs | 2 +- tests/split_test.rs | 8 ++++---- 7 files changed, 29 insertions(+), 29 deletions(-) rename src/plugin/{cleaner.rs => keep.rs} (74%) diff --git a/example/src/split_log.rs b/example/src/split_log.rs index 53b2432..12aca3d 100644 --- a/example/src/split_log.rs +++ b/example/src/split_log.rs @@ -1,6 +1,6 @@ use fast_log::config::Config; use fast_log::consts::LogSize; -use fast_log::plugin::cleaner::{RollingAll, RollingNum}; +use fast_log::plugin::keep::{KeepAll, KeepNum}; use fast_log::plugin::packer::LogPacker; fn main() { @@ -8,7 +8,7 @@ fn main() { fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", LogSize::MB(1), - RollingNum(2), + KeepNum(2), LogPacker {}, )) .unwrap(); diff --git a/src/config.rs b/src/config.rs index 72a2fac..6bd46d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use crate::filter::{Filter, NoFilter}; use crate::plugin::console::ConsoleAppender; use crate::plugin::file::FileAppender; use crate::plugin::file_loop::FileLoopAppender; -use crate::plugin::file_split::{Cleaner, FileSplitAppender, Packer, RawFile, SplitFile}; +use crate::plugin::file_split::{FileSplitAppender, Keep, Packer, RawFile, SplitFile}; use crate::FastLogFormat; use dark_std::sync::SyncVec; use log::LevelFilter; @@ -90,7 +90,7 @@ impl Config { self } /// add a FileSplitAppender - pub fn file_split( + pub fn file_split( self, file_path: &str, temp_size: LogSize, @@ -118,7 +118,7 @@ impl Config { // LogPacker {}, // ), // ); - pub fn split( + pub fn split( self, file_path: &str, temp_size: LogSize, diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index b0b7897..b141334 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -1,8 +1,8 @@ use crate::appender::{FastLogRecord, LogAppender}; use crate::consts::LogSize; use crate::error::LogError; -use crate::plugin::cleaner::RollingNum; use crate::plugin::file_split::{FileSplitAppender, SplitFile}; +use crate::plugin::keep::KeepNum; use crate::plugin::packer::LogPacker; /// Single logs are stored in rolling mode by capacity @@ -16,7 +16,7 @@ impl FileLoopAppender { file: FileSplitAppender::::new( log_file_path, size, - RollingNum(1), + KeepNum(1), LogPacker {}, )?, }) diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 9af68b5..63aaf6b 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -174,7 +174,7 @@ pub struct FileSplitAppender { } impl FileSplitAppender { - pub fn new( + pub fn new( file_path: &str, temp_size: LogSize, rolling: R, @@ -289,9 +289,9 @@ impl LogPack { } } -pub trait Cleaner: Send { +pub trait Keep: Send { /// return removed - fn do_clean(&self, dir: &str, temp_name: &str) -> i64; + fn do_keep(&self, dir: &str, temp_name: &str) -> i64; fn read_paths(&self, dir: &str, temp_name: &str) -> Vec { let base_name = get_base_name(&Path::new(temp_name)); let paths = std::fs::read_dir(dir); @@ -337,8 +337,8 @@ pub enum RollingType { KeepNum(i64), } -impl Cleaner for RollingType { - fn do_clean(&self, temp_name: &str, dir: &str) -> i64 { +impl Keep for RollingType { + fn do_keep(&self, temp_name: &str, dir: &str) -> i64 { let mut removed = 0; match self { RollingType::KeepNum(n) => { @@ -426,7 +426,7 @@ impl LogAppender for FileSplitAppender } ///spawn an saver thread to save log file or zip file -fn spawn_saver( +fn spawn_saver( temp_name: String, r: Receiver, rolling: R, @@ -436,7 +436,7 @@ fn spawn_saver( loop { if let Ok(pack) = r.recv() { //do rolling - rolling.do_clean(&pack.dir, &temp_name); + 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()); diff --git a/src/plugin/cleaner.rs b/src/plugin/keep.rs similarity index 74% rename from src/plugin/cleaner.rs rename to src/plugin/keep.rs index 97c06b0..d7273ef 100644 --- a/src/plugin/cleaner.rs +++ b/src/plugin/keep.rs @@ -1,20 +1,20 @@ -use crate::plugin::file_split::Cleaner; +use crate::plugin::file_split::Keep; use fastdate::DateTime; use std::time::Duration; /// keeps all,do not rolling -pub struct RollingAll {} -impl Cleaner for RollingAll { - fn do_clean(&self, dir: &str, temp_name: &str) -> i64 { +pub struct KeepAll {} +impl Keep for KeepAll { + fn do_keep(&self, dir: &str, temp_name: &str) -> i64 { 0 } } /// rolling from file num -pub struct RollingNum(pub i64); +pub struct KeepNum(pub i64); -impl Cleaner for RollingNum { - fn do_clean(&self, dir: &str, temp_name: &str) -> i64 { +impl Keep for KeepNum { + fn do_keep(&self, dir: &str, temp_name: &str) -> i64 { let mut removed = 0; let paths_vec = self.read_paths(dir, temp_name); for index in 0..paths_vec.len() { @@ -29,10 +29,10 @@ impl Cleaner for RollingNum { } /// rolling from metadata -pub struct RollingDuration(pub Duration); +pub struct KeepDuration(pub Duration); -impl Cleaner for RollingDuration { - fn do_clean(&self, dir: &str, temp_name: &str) -> i64 { +impl Keep for KeepDuration { + fn do_keep(&self, dir: &str, temp_name: &str) -> i64 { let mut removed = 0; let paths_vec = self.read_paths(dir, temp_name); let now = DateTime::now(); diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 1213dfd..932f5a8 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -6,4 +6,4 @@ pub mod file_mmap; pub mod file_split; pub mod packer; -pub mod cleaner; +pub mod keep; diff --git a/tests/split_test.rs b/tests/split_test.rs index 3ca837a..0a77c13 100644 --- a/tests/split_test.rs +++ b/tests/split_test.rs @@ -2,8 +2,8 @@ mod test { use fast_log::appender::{Command, FastLogRecord, LogAppender}; use fast_log::consts::LogSize; - use fast_log::plugin::cleaner::{RollingAll, RollingNum}; - use fast_log::plugin::file_split::{Cleaner, FileSplitAppender, Packer, RawFile}; + use fast_log::plugin::file_split::{FileSplitAppender, Keep, Packer, RawFile}; + use fast_log::plugin::keep::{KeepAll, KeepNum}; use fast_log::plugin::packer::LogPacker; use log::Level; use std::fs::remove_dir_all; @@ -16,7 +16,7 @@ mod test { let appender = FileSplitAppender::::new( "target/test/", LogSize::MB(1), - RollingAll {}, + KeepAll {}, LogPacker {}, ) .unwrap(); @@ -33,7 +33,7 @@ mod test { }]); appender.send_pack(); sleep(Duration::from_secs(1)); - let rolling_num = RollingNum(0).do_clean("target/test/", "temp.log"); + let rolling_num = KeepNum(0).do_keep("target/test/", "temp.log"); assert_eq!(rolling_num, 1); let _ = remove_dir_all("target/test/"); }