From c1d65545a11fa1a6a1661ac0d281fd8fb19ebe61 Mon Sep 17 00:00:00 2001 From: zxj Date: Fri, 26 Jul 2024 16:39:55 +0800 Subject: [PATCH] todo: Packer::is_allow method --- example/src/split_log_date.rs | 6 ++++ src/config.rs | 17 ++++++---- src/plugin/file_loop.rs | 14 ++++---- src/plugin/file_split.rs | 63 ++++++++++++++++++----------------- src/plugin/packer.rs | 21 ++++++++++-- tests/split_test.rs | 10 ++++-- 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/example/src/split_log_date.rs b/example/src/split_log_date.rs index 5ad5d1e..5b73f6c 100644 --- a/example/src/split_log_date.rs +++ b/example/src/split_log_date.rs @@ -1,3 +1,4 @@ +use fast_log::appender::FastLogRecord; use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::error::LogError; @@ -16,6 +17,11 @@ impl Packer for DateLogPacker { "log" } + fn is_allow(&self, temp_size: usize, arg: FastLogRecord) -> bool { + //TODO + false + } + fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { impl DateLogPacker { pub fn new_log_name(&self, first_file_path: &str, date: fastdate::DateTime) -> String { diff --git a/src/config.rs b/src/config.rs index e59085d..3df953f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use crate::appender::{LogAppender, RecordFormat}; use crate::consts::LogSize; -use crate::filter::{Filter}; +use crate::filter::Filter; use crate::plugin::console::ConsoleAppender; use crate::plugin::file::FileAppender; use crate::plugin::file_loop::FileLoopAppender; @@ -8,8 +8,8 @@ use crate::plugin::file_split::{FileSplitAppender, Keep, Packer, RawFile, SplitF use crate::FastLogFormat; use dark_std::sync::SyncVec; use log::LevelFilter; -use std::fmt::{Debug, Formatter}; use parking_lot::Mutex; +use std::fmt::{Debug, Formatter}; /// the fast_log Config /// for example: @@ -97,7 +97,7 @@ impl Config { /// add a FileLoopAppender pub fn file_loop(self, file: &str, max_temp_size: LogSize) -> Self { self.appends.push(Mutex::new(Box::new( - FileLoopAppender::::new(file, max_temp_size).expect("make file_loop fail"), + FileLoopAppender::new(file, max_temp_size).expect("make file_loop fail"), ))); self } @@ -110,8 +110,13 @@ impl Config { packer: P, ) -> Self { self.appends.push(Mutex::new(Box::new( - FileSplitAppender::::new(file_path, temp_size, rolling_type, Box::new(packer)) - .unwrap(), + FileSplitAppender::new::( + file_path, + temp_size, + rolling_type, + Box::new(packer), + ) + .unwrap(), ))); self } @@ -138,7 +143,7 @@ impl Config { packer: P, ) -> Self { self.appends.push(Mutex::new(Box::new( - FileSplitAppender::::new(file_path, temp_size, keeper, Box::new(packer)).unwrap(), + FileSplitAppender::new::(file_path, temp_size, keeper, Box::new(packer)).unwrap(), ))); self } diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index 67916df..de683c2 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -1,18 +1,18 @@ use crate::appender::{FastLogRecord, LogAppender}; use crate::consts::LogSize; use crate::error::LogError; -use crate::plugin::file_split::{FileSplitAppender, KeepType, SplitFile}; +use crate::plugin::file_split::{FileSplitAppender, KeepType, RawFile}; use crate::plugin::packer::LogPacker; /// Single logs are stored in rolling mode by capacity -pub struct FileLoopAppender { - file: FileSplitAppender, +pub struct FileLoopAppender { + file: FileSplitAppender, } -impl FileLoopAppender { - pub fn new(log_file_path: &str, size: LogSize) -> Result, LogError> { +impl FileLoopAppender { + pub fn new(log_file_path: &str, size: LogSize) -> Result { Ok(Self { - file: FileSplitAppender::::new( + file: FileSplitAppender::new::( log_file_path, size, KeepType::KeepNum(1), @@ -22,7 +22,7 @@ impl FileLoopAppender { } } -impl LogAppender for FileLoopAppender { +impl LogAppender for FileLoopAppender { fn do_logs(&self, records: &[FastLogRecord]) { self.file.do_logs(records); } diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index b4915e9..6d9b42e 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -90,39 +90,39 @@ impl SplitFile for RawFile { /// .zip or .lz4 or any one packer pub trait Packer: Send + Sync { fn pack_name(&self) -> &'static str; - //return bool: remove_log_file + + /// is allow do pack + fn is_allow(&self, current_size: usize, arg: FastLogRecord) -> bool; + + ///return bool: remove_log_file fn do_pack(&self, log_file: File, log_file_path: &str) -> Result; /// default 0 is not retry pack. if retry > 0 ,it will trying rePack fn retry(&self) -> i32 { return 0; } - fn log_name_create(&self, first_file_path: &str) -> String { + /// date to string + fn date_to_string(&self, arg: DateTime) -> String { + arg.display_stand() + .to_string() + .replace(" ", "T") + .replace(":", "-") + } + + /// create date style log name + /// input: 'temp.log' + /// output: 'temp2024-07-26T16-04-17.685429.log' + fn new_data_log_name(&self, first_file_path: &str, date: DateTime) -> String { let file_name = first_file_path.extract_file_name(); let mut new_log_name = String::new(); let point = file_name.rfind("."); match point { None => { - new_log_name.push_str( - &DateTime::now() - .display_stand() - .to_string() - .replace(" ", "T") - .replace(":", "-"), - ); + new_log_name.push_str(&self.date_string(date)); } Some(i) => { let (name, ext) = file_name.split_at(i); - new_log_name = format!( - "{}{}{}", - name, - DateTime::now() - .display_stand() - .to_string() - .replace(" ", "T") - .replace(":", "-"), - ext - ); + new_log_name = format!("{}{}{}", name, self.date_string(date), ext); } } new_log_name = first_file_path.trim_end_matches(&file_name).to_string() + &new_log_name; @@ -132,8 +132,8 @@ pub trait Packer: Send + Sync { /// split log file allow pack compress log /// Memory space swop running time , reduces the number of repeated queries for IO -pub struct FileSplitAppender { - file: F, +pub struct FileSplitAppender { + file: Box, packer: Arc>, dir_path: String, sender: Sender, @@ -143,13 +143,13 @@ pub struct FileSplitAppender { temp_name: String, } -impl FileSplitAppender { - pub fn new( +impl FileSplitAppender { + pub fn new( file_path: &str, temp_size: LogSize, rolling_type: R, packer: Box, - ) -> Result, LogError> { + ) -> Result { let temp_name = { let mut name = file_path.extract_file_name().to_string(); if name.is_empty() { @@ -188,7 +188,7 @@ impl FileSplitAppender { Ok(Self { temp_bytes, dir_path: dir_path.to_string(), - file, + file: Box::new(file) as Box, sender, temp_size, temp_name, @@ -202,7 +202,9 @@ impl FileSplitAppender { sp = "/"; } let first_file_path = format!("{}{}{}", self.dir_path, sp, &self.temp_name); - let new_log_name = self.packer.log_name_create(&first_file_path); + let new_log_name = self + .packer + .new_data_log_name(&first_file_path, DateTime::now()); self.file.flush(); let _ = std::fs::copy(&first_file_path, &new_log_name); let _ = self.sender.send(LogPack { @@ -349,18 +351,17 @@ impl Keep for KeepType { } } -impl LogAppender for FileSplitAppender { +impl LogAppender for FileSplitAppender { fn do_logs(&self, records: &[FastLogRecord]) { //if temp_bytes is full,must send pack let mut temp = String::with_capacity(records.len() * 10); for x in records { match x.command { Command::CommandRecord => { - if (self.temp_bytes.load(Ordering::Relaxed) + let current_temp_size = self.temp_bytes.load(Ordering::Relaxed) + temp.as_bytes().len() - + x.formated.as_bytes().len()) - >= self.temp_size.get_len() - { + + x.formated.as_bytes().len(); + if current_temp_size >= self.temp_size.get_len() { self.temp_bytes.fetch_add( { let w = self.file.write(temp.as_bytes()); diff --git a/src/plugin/packer.rs b/src/plugin/packer.rs index a2ac37b..79223b4 100644 --- a/src/plugin/packer.rs +++ b/src/plugin/packer.rs @@ -10,6 +10,11 @@ impl Packer for LogPacker { "log" } + fn is_allow(&self, temp_size: usize, arg: FastLogRecord) -> bool { + //TODO + false + } + fn do_pack(&self, _log_file: File, _log_file_path: &str) -> Result { //do nothing,and not remove file return Ok(false); @@ -31,7 +36,10 @@ impl Packer for ZipPacker { fn pack_name(&self) -> &'static str { "zip" } - + fn is_allow(&self, temp_size: usize, arg: FastLogRecord) -> bool { + //TODO + false + } fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { use std::io::Write; let mut log_name = log_file_path.replace("\\", "/").to_string(); @@ -80,7 +88,10 @@ impl Packer for LZ4Packer { fn pack_name(&self) -> &'static str { "lz4" } - + fn is_allow(&self, temp_size: usize, arg: FastLogRecord) -> bool { + //TODO + false + } fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { let lz4_path = log_file_path.replace(".log", ".lz4"); let lz4_file = File::create(&lz4_path); @@ -107,6 +118,7 @@ impl Packer for LZ4Packer { } } +use crate::appender::FastLogRecord; #[cfg(feature = "gzip")] use flate2::write::GzEncoder; #[cfg(feature = "gzip")] @@ -120,7 +132,10 @@ impl Packer for GZipPacker { fn pack_name(&self) -> &'static str { "gz" } - + fn is_allow(&self, temp_size: usize, arg: FastLogRecord) -> bool { + //TODO + false + } fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { use std::io::Write; let zip_path = log_file_path.replace(".log", ".gz"); diff --git a/tests/split_test.rs b/tests/split_test.rs index 1cc3ea8..6141e1c 100644 --- a/tests/split_test.rs +++ b/tests/split_test.rs @@ -3,8 +3,11 @@ 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, Keep, Packer, RawFile, RollingType}; + use fast_log::plugin::file_split::{ + FileSplitAppender, Keep, Packer, RawFile, RollingType, SplitFile, + }; use fast_log::plugin::packer::LogPacker; + use fastdate::DateTime; use log::Level; use std::fs::remove_dir_all; use std::thread::sleep; @@ -13,7 +16,7 @@ mod test { #[test] fn test_send_pack() { let _ = remove_dir_all("target/test/"); - let appender = FileSplitAppender::::new( + let appender = FileSplitAppender::new::( "target/test/", LogSize::MB(1), RollingType::All, @@ -41,7 +44,8 @@ mod test { #[test] fn test_log_name_create() { let p = LogPacker {}; - let name = p.log_name_create("temp.log"); + let name = p.new_data_log_name("temp.log", DateTime::now()); + println!("{}", name); assert_eq!(name.ends_with(".log"), true); }