Skip to content

Commit

Permalink
LogDatePacker
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 19, 2023
1 parent dc1eaf0 commit 771c3c8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 72 deletions.
70 changes: 68 additions & 2 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::keep::{KeepAll, KeepNum};
use fast_log::plugin::packer::{LogDatePacker, LogPacker};
use fast_log::error::LogError;
use fast_log::plugin::file_split::Packer;
use fast_log::plugin::keep::KeepNum;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;

///pack by an date
#[derive(Clone)]
pub struct LogDatePacker {}

impl LogDatePacker {
pub fn log_name_create_by_time(
&self,
first_file_path: &str,
date: fastdate::DateTime,
) -> String {
let path = Path::new(first_file_path);
let file_name = path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
let mut new_log_name = date.to_string().replace(" ", "T").replace(":", "-");
new_log_name.push_str(".");
new_log_name.push_str(self.pack_name());
new_log_name = first_file_path.trim_end_matches(&file_name).to_string() + &new_log_name;
return new_log_name;
}
}
impl Packer for LogDatePacker {
fn pack_name(&self) -> &'static str {
"log"
}

fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result<bool, LogError> {
//do nothing,and not remove file
let now = fastdate::DateTime::now()
.set_hour(0)
.set_min(0)
.set_sec(0)
.set_nano(0);
let name = self.log_name_create_by_time(log_file_path, now);
let mut f = OpenOptions::new()
.write(true)
.read(true)
.append(true)
.open(&name);
if let Ok(mut f) = f {
//append to file
let mut data = vec![];
log_file.read_to_end(&mut data)?;
f.write_all(&data)?;
std::fs::remove_file(log_file_path)?;
} else {
//create file
f = OpenOptions::new().write(true).create(true).open(name);
if let Ok(mut f) = f {
let mut data = vec![];
log_file.read_to_end(&mut data)?;
f.write_all(&data)?;
std::fs::remove_file(log_file_path)?;
}
}
return Ok(false);
}
}

fn main() {
//file_path also can use '"target/logs/test.log"'
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ impl LogPack {
if log_file_path.is_empty() {
return Err(LogError::from("log_file_path.is_empty"));
}
let log_file = OpenOptions::new().read(true).open(log_file_path);
let log_file = OpenOptions::new()
.write(true)
.read(true)
.open(log_file_path);
if log_file.is_err() {
return Err(LogError::from(format!(
"open(log_file_path={}) fail",
Expand Down
71 changes: 2 additions & 69 deletions src/plugin/packer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::error::LogError;
use crate::plugin::file_split::Packer;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use std::fs::File;
use std::io::Write;

/// keep temp{date}.log
#[derive(Clone)]
Expand All @@ -18,72 +17,6 @@ impl Packer for LogPacker {
}
}

///pack by an date
#[derive(Clone)]
pub struct LogDatePacker {}

impl LogDatePacker {
pub fn log_name_create_by_time(
&self,
first_file_path: &str,
date: fastdate::DateTime,
) -> String {
let path = Path::new(first_file_path);
let file_name = path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
let mut new_log_name = date.to_string().replace(" ", "T").replace(":", "-");
new_log_name.push_str(".");
new_log_name.push_str(self.pack_name());
new_log_name = first_file_path.trim_end_matches(&file_name).to_string() + &new_log_name;
return new_log_name;
}
}
impl Packer for LogDatePacker {
fn pack_name(&self) -> &'static str {
"log"
}

fn do_pack(&self, log_file: File, log_file_path: &str) -> Result<bool, LogError> {
//do nothing,and not remove file
let now = fastdate::DateTime::now()
.set_hour(0)
.set_min(0)
.set_sec(0)
.set_nano(0);
let name = self.log_name_create_by_time(log_file_path, now);
let mut f = OpenOptions::new()
.write(true)
.read(true)
.append(true)
.open(&name);
if let Ok(mut f) = f {
//append to file
let mut data = vec![];
if let Ok(mut old) = File::open(log_file_path) {
old.read_to_end(&mut data);
f.write_all(&data);
std::fs::remove_file(log_file_path);
}
} else {
//create file
f = OpenOptions::new().write(true).create(true).open(name);
if let Ok(mut f) = f {
let mut data = vec![];
if let Ok(mut old) = File::open(log_file_path) {
old.read_to_end(&mut data);
f.write_all(&data);
std::fs::remove_file(log_file_path);
}
}
}
return Ok(false);
}
}

#[cfg(feature = "zip")]
use zip::result::ZipResult;
#[cfg(feature = "zip")]
Expand Down

0 comments on commit 771c3c8

Please sign in to comment.