Skip to content

Commit

Permalink
todo: Packer::is_allow method
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 26, 2024
1 parent 9a8b93c commit c1d6554
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 50 deletions.
6 changes: 6 additions & 0 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fast_log::appender::FastLogRecord;
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::error::LogError;
Expand All @@ -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<bool, LogError> {
impl DateLogPacker {
pub fn new_log_name(&self, first_file_path: &str, date: fastdate::DateTime) -> String {
Expand Down
17 changes: 11 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
use crate::plugin::file_split::{FileSplitAppender, Keep, Packer, RawFile, SplitFile};
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:
Expand Down Expand Up @@ -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::<RawFile>::new(file, max_temp_size).expect("make file_loop fail"),
FileLoopAppender::new(file, max_temp_size).expect("make file_loop fail"),
)));
self
}
Expand All @@ -110,8 +110,13 @@ impl Config {
packer: P,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::<RawFile>::new(file_path, temp_size, rolling_type, Box::new(packer))
.unwrap(),
FileSplitAppender::new::<R, RawFile>(
file_path,
temp_size,
rolling_type,
Box::new(packer),
)
.unwrap(),
)));
self
}
Expand All @@ -138,7 +143,7 @@ impl Config {
packer: P,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::<F>::new(file_path, temp_size, keeper, Box::new(packer)).unwrap(),
FileSplitAppender::new::<R, F>(file_path, temp_size, keeper, Box::new(packer)).unwrap(),
)));
self
}
Expand Down
14 changes: 7 additions & 7 deletions src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -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<F: SplitFile> {
file: FileSplitAppender<F>,
pub struct FileLoopAppender {
file: FileSplitAppender,
}

impl<F: SplitFile> FileLoopAppender<F> {
pub fn new(log_file_path: &str, size: LogSize) -> Result<FileLoopAppender<F>, LogError> {
impl FileLoopAppender {
pub fn new(log_file_path: &str, size: LogSize) -> Result<FileLoopAppender, LogError> {
Ok(Self {
file: FileSplitAppender::<F>::new(
file: FileSplitAppender::new::<KeepType, RawFile>(
log_file_path,
size,
KeepType::KeepNum(1),
Expand All @@ -22,7 +22,7 @@ impl<F: SplitFile> FileLoopAppender<F> {
}
}

impl<F: SplitFile> LogAppender for FileLoopAppender<F> {
impl LogAppender for FileLoopAppender {
fn do_logs(&self, records: &[FastLogRecord]) {
self.file.do_logs(records);
}
Expand Down
63 changes: 32 additions & 31 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, LogError>;
/// 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;
Expand All @@ -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<F: SplitFile> {
file: F,
pub struct FileSplitAppender {
file: Box<dyn SplitFile>,
packer: Arc<Box<dyn Packer>>,
dir_path: String,
sender: Sender<LogPack>,
Expand All @@ -143,13 +143,13 @@ pub struct FileSplitAppender<F: SplitFile> {
temp_name: String,
}

impl<F: SplitFile> FileSplitAppender<F> {
pub fn new<R: Keep + 'static>(
impl FileSplitAppender {
pub fn new<R: Keep + 'static, F: SplitFile + 'static>(
file_path: &str,
temp_size: LogSize,
rolling_type: R,
packer: Box<dyn Packer>,
) -> Result<FileSplitAppender<F>, LogError> {
) -> Result<FileSplitAppender, LogError> {
let temp_name = {
let mut name = file_path.extract_file_name().to_string();
if name.is_empty() {
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<F: SplitFile> FileSplitAppender<F> {
Ok(Self {
temp_bytes,
dir_path: dir_path.to_string(),
file,
file: Box::new(file) as Box<dyn SplitFile>,
sender,
temp_size,
temp_name,
Expand All @@ -202,7 +202,9 @@ impl<F: SplitFile> FileSplitAppender<F> {
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 {
Expand Down Expand Up @@ -349,18 +351,17 @@ impl Keep for KeepType {
}
}

impl<F: SplitFile> LogAppender for FileSplitAppender<F> {
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());
Expand Down
21 changes: 18 additions & 3 deletions src/plugin/packer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, LogError> {
//do nothing,and not remove file
return Ok(false);
Expand All @@ -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<bool, LogError> {
use std::io::Write;
let mut log_name = log_file_path.replace("\\", "/").to_string();
Expand Down Expand Up @@ -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<bool, LogError> {
let lz4_path = log_file_path.replace(".log", ".lz4");
let lz4_file = File::create(&lz4_path);
Expand All @@ -107,6 +118,7 @@ impl Packer for LZ4Packer {
}
}

use crate::appender::FastLogRecord;
#[cfg(feature = "gzip")]
use flate2::write::GzEncoder;
#[cfg(feature = "gzip")]
Expand All @@ -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<bool, LogError> {
use std::io::Write;
let zip_path = log_file_path.replace(".log", ".gz");
Expand Down
10 changes: 7 additions & 3 deletions tests/split_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +16,7 @@ mod test {
#[test]
fn test_send_pack() {
let _ = remove_dir_all("target/test/");
let appender = FileSplitAppender::<RawFile>::new(
let appender = FileSplitAppender::new::<RollingType, RawFile>(
"target/test/",
LogSize::MB(1),
RollingType::All,
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c1d6554

Please sign in to comment.