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 66cf371 commit 9a4f010
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/src/split_log.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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() {
//file_path also can use '"target/logs/test.log"'
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::MB(1),
RollingNum(2),
KeepNum(2),
LogPacker {},
))
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Config {
self
}
/// add a FileSplitAppender
pub fn file_split<P: Packer + Sync + 'static, R: Cleaner + 'static>(
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static>(
self,
file_path: &str,
temp_size: LogSize,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Config {
// LogPacker {},
// ),
// );
pub fn split<F: SplitFile + 'static, R: Cleaner + 'static, P: Packer + Sync + 'static>(
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static>(
self,
file_path: &str,
temp_size: LogSize,
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +16,7 @@ impl<F: SplitFile> FileLoopAppender<F> {
file: FileSplitAppender::<F, LogPacker>::new(
log_file_path,
size,
RollingNum(1),
KeepNum(1),
LogPacker {},
)?,
})
Expand Down
14 changes: 7 additions & 7 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct FileSplitAppender<F: SplitFile, P: Packer> {
}

impl<F: SplitFile, P: Packer + Sync + 'static> FileSplitAppender<F, P> {
pub fn new<R: Cleaner + 'static>(
pub fn new<R: Keep + 'static>(
file_path: &str,
temp_size: LogSize,
rolling: R,
Expand Down Expand Up @@ -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<DirEntry> {
let base_name = get_base_name(&Path::new(temp_name));
let paths = std::fs::read_dir(dir);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -426,7 +426,7 @@ impl<F: SplitFile, P: Packer + Sync + 'static> LogAppender for FileSplitAppender
}

///spawn an saver thread to save log file or zip file
fn spawn_saver<P: Packer + Sync + 'static, R: Cleaner + 'static>(
fn spawn_saver<P: Packer + Sync + 'static, R: Keep + 'static>(
temp_name: String,
r: Receiver<LogPack>,
rolling: R,
Expand All @@ -436,7 +436,7 @@ fn spawn_saver<P: Packer + Sync + 'static, R: Cleaner + 'static>(
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());
Expand Down
20 changes: 10 additions & 10 deletions src/plugin/cleaner.rs → src/plugin/keep.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub mod file_mmap;
pub mod file_split;
pub mod packer;

pub mod cleaner;
pub mod keep;
8 changes: 4 additions & 4 deletions tests/split_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +16,7 @@ mod test {
let appender = FileSplitAppender::<RawFile, LogPacker>::new(
"target/test/",
LogSize::MB(1),
RollingAll {},
KeepAll {},
LogPacker {},
)
.unwrap();
Expand All @@ -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/");
}
Expand Down

0 comments on commit 9a4f010

Please sign in to comment.