Skip to content

Commit

Permalink
add impl PackType
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 26, 2024
1 parent 41be29b commit 48eb1a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
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;
use crate::plugin::console::ConsoleAppender;
use crate::plugin::file::FileAppender;
use crate::plugin::file_loop::FileLoopAppender;
use crate::plugin::file_split::{FileSplitAppender, IsPack, Keep, Packer, RawFile, SplitFile};
use crate::plugin::file_split::{FileSplitAppender, CanPack, Keep, Packer, RawFile, SplitFile};
use crate::FastLogFormat;
use dark_std::sync::SyncVec;
use log::LevelFilter;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Config {
self
}
/// add a FileSplitAppender
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static,H: IsPack +'static>(
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static,H: CanPack +'static>(
self,
file_path: &str,
how:H,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Config {
/// );
/// }
/// ```
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static,H: IsPack +'static>(
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static,H: CanPack +'static>(
self,
file_path: &str,
keeper: R,
Expand Down
18 changes: 9 additions & 9 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub trait Packer: Send + Sync {
}
}


pub trait IsPack: Send {
fn need_pack(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool;
/// is can do pack?
pub trait CanPack: Send {
fn is(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool;
}

/// keep logs, for example keep by log num or keep by log create time.
Expand Down Expand Up @@ -177,8 +177,8 @@ pub enum PackType {
BySize(LogSize),
}

impl IsPack for PackType {
fn need_pack(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool {
impl CanPack for PackType {
fn is(&mut self, temp_size: usize, arg: &FastLogRecord) -> bool {
match self {
PackType::ByDate(date_time) => {
let dt = fastdate::DateTime::from_system_time(arg.now, fastdate::offset_sec());
Expand Down Expand Up @@ -207,7 +207,7 @@ pub struct FileSplitAppender {
packer: Arc<Box<dyn Packer>>,
dir_path: String,
sender: Sender<LogPack>,
is_pack: Box<dyn IsPack>,
is_pack: Box<dyn CanPack>,
//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 IsPack>,
how_to_pack: Box<dyn CanPack>,
rolling_type: Box<dyn Keep>,
packer: Box<dyn Packer>,
) -> Result<FileSplitAppender, LogError> {
Expand Down Expand Up @@ -399,7 +399,7 @@ impl LogAppender for FileSplitAppender {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed)
+ temp.as_bytes().len()
+ x.formated.as_bytes().len();
if self.is_pack.need_pack(current_temp_size, x) {
if self.is_pack.is(current_temp_size, x) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand All @@ -419,7 +419,7 @@ impl LogAppender for FileSplitAppender {
Command::CommandExit => {}
Command::CommandFlush(ref w) => {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed);
if self.is_pack.need_pack(current_temp_size, x) {
if self.is_pack.is(current_temp_size, x) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand Down

0 comments on commit 48eb1a7

Please sign in to comment.