Skip to content

Commit

Permalink
add impl HowPackType
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 26, 2024
1 parent 046ebd0 commit 40c436a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions example/src/split_log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::file_split::{HowPackType, KeepType};
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),
HowPackType::BySize(LogSize::KB(500)),
KeepType::KeepNum(2),
LogPacker {},
))
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fast_log::config::Config;
use fast_log::error::LogError;
use fast_log::plugin::file_name::FileName;
use fast_log::plugin::file_split::{HowToPackType, KeepType, Packer};
use fast_log::plugin::file_split::{HowPackType, KeepType, Packer};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::thread::sleep;
Expand Down Expand Up @@ -63,7 +63,7 @@ fn main() {
"target/logs/",
KeepType::KeepNum(2),
DateLogPacker {},
HowToPackType::ByDate(DateTime::now()),
HowPackType::ByDate(DateTime::now()),
))
.unwrap();
for _ in 0..40000 {
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_gz.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::file_split::{HowPackType, KeepType};
use fast_log::plugin::packer::GZipPacker;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
HowPackType::BySize(LogSize::KB(50)),
KeepType::KeepNum(5),
GZipPacker {},
))
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_lz4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::file_split::{HowPackType, KeepType};
use fast_log::plugin::packer::LZ4Packer;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
HowPackType::BySize(LogSize::KB(50)),
KeepType::KeepNum(5),
LZ4Packer {},
))
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_zip.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::file_split::{HowPackType, KeepType};
use fast_log::plugin::packer::ZipPacker;

use fast_log::config::Config;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
HowPackType::BySize(LogSize::KB(50)),
KeepType::KeepNum(5),
ZipPacker {},
))
Expand Down
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::filter::Filter;
use crate::plugin::console::ConsoleAppender;
use crate::plugin::file::FileAppender;
use crate::plugin::file_loop::FileLoopAppender;
use crate::plugin::file_split::{FileSplitAppender, HowToPack, Keep, Packer, RawFile, SplitFile};
use crate::plugin::file_split::{FileSplitAppender, HowPack, Keep, Packer, RawFile, SplitFile};
use crate::FastLogFormat;
use dark_std::sync::SyncVec;
use log::LevelFilter;
Expand Down Expand Up @@ -102,12 +102,12 @@ impl Config {
self
}
/// add a FileSplitAppender
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static,H:HowToPack+'static>(
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static,H: HowPack +'static>(
self,
file_path: &str,
how:H,
rolling_type: R,
packer: P,
how:H
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<RawFile>(
Expand All @@ -127,22 +127,22 @@ impl Config {
/// ```rust
/// use fast_log::Config;
/// use fast_log::consts::LogSize;
/// use fast_log::plugin::file_split::{HowToPackType, RawFile, RollingType};
/// use fast_log::plugin::file_split::{HowPackType, RawFile, RollingType};
/// use fast_log::plugin::packer::LogPacker;
/// fn new(){
/// fast_log::init(
/// Config::new()
/// .chan_len(Some(100000))
/// .split::<RawFile, RollingType, LogPacker, HowToPackType>(
/// .split::<RawFile, RollingType, LogPacker, HowPackType>(
/// "target/logs/temp.log",
/// RollingType::All,
/// LogPacker {},
/// HowToPackType::BySize(LogSize::MB(1)),
/// HowPackType::BySize(LogSize::MB(1)),
/// ),
/// );
/// }
/// ```
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static,H:HowToPack+'static>(
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static,H: HowPack +'static>(
self,
file_path: &str,
keeper: R,
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::appender::{FastLogRecord, LogAppender};
use crate::consts::LogSize;
use crate::error::LogError;
use crate::plugin::file_split::{FileSplitAppender, HowToPackType, KeepType, RawFile};
use crate::plugin::file_split::{FileSplitAppender, HowPackType, KeepType, RawFile};
use crate::plugin::packer::LogPacker;

/// Single logs are stored in rolling mode by capacity
Expand All @@ -14,7 +14,7 @@ impl FileLoopAppender {
Ok(Self {
file: FileSplitAppender::new::<RawFile>(
log_file_path,
Box::new(HowToPackType::BySize(size)),
Box::new(HowPackType::BySize(size)),
Box::new(KeepType::KeepNum(1)),
Box::new(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 @@ -55,7 +55,7 @@ pub trait Packer: Send + Sync {
}


pub trait HowToPack: Send {
pub trait HowPack: Send {
fn need_pack(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool;
}

Expand Down Expand Up @@ -172,15 +172,15 @@ impl SplitFile for RawFile {
}


pub enum HowToPackType {
pub enum HowPackType {
ByDate(DateTime),
BySize(LogSize),
}

impl HowToPack for HowToPackType {
impl HowPack for HowPackType {
fn need_pack(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool {
match self {
HowToPackType::ByDate(date_time) => {
HowPackType::ByDate(date_time) => {
let dt = fastdate::DateTime::from_system_time(arg.now, fastdate::offset_sec());
if dt.day() > date_time.day() {
*date_time = dt;
Expand All @@ -189,7 +189,7 @@ impl HowToPack for HowToPackType {
return false;
}
}
HowToPackType::BySize(limit) => {
HowPackType::BySize(limit) => {
if temp_size >= limit.get_len() {
return true;
} else {
Expand All @@ -207,7 +207,7 @@ pub struct FileSplitAppender {
packer: Arc<Box<dyn Packer>>,
dir_path: String,
sender: Sender<LogPack>,
how_to_pack: Box<dyn HowToPack>,
how_to_pack: Box<dyn HowPack>,
//cache data
temp_bytes: AtomicUsize,
temp_name: String,
Expand All @@ -216,7 +216,7 @@ pub struct FileSplitAppender {
impl FileSplitAppender {
pub fn new<F: SplitFile + 'static>(
file_path: &str,
how_to_pack: Box<dyn HowToPack>,
how_to_pack: Box<dyn HowPack>,
rolling_type: Box<dyn Keep>,
packer: Box<dyn Packer>,
) -> Result<FileSplitAppender, LogError> {
Expand Down
4 changes: 2 additions & 2 deletions tests/split_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test {
use fast_log::appender::{Command, FastLogRecord, LogAppender};
use fast_log::consts::LogSize;
use fast_log::plugin::file_name::FileName;
use fast_log::plugin::file_split::{FileSplitAppender, HowToPackType, Keep, Packer, RawFile, RollingType};
use fast_log::plugin::file_split::{FileSplitAppender, HowPackType, Keep, Packer, RawFile, RollingType};
use fast_log::plugin::packer::LogPacker;
use fastdate::DateTime;
use log::Level;
Expand All @@ -16,7 +16,7 @@ mod test {
let _ = remove_dir_all("target/test/");
let mut appender = FileSplitAppender::new::<RawFile>(
"target/test/",
Box::new(HowToPackType::BySize(LogSize::MB(1))),
Box::new(HowPackType::BySize(LogSize::MB(1))),
Box::new(RollingType::All),
Box::new(LogPacker {}),
)
Expand Down

0 comments on commit 40c436a

Please sign in to comment.