diff --git a/example/src/split_log_date.rs b/example/src/split_log_date.rs index e0e1af7..1f38df1 100644 --- a/example/src/split_log_date.rs +++ b/example/src/split_log_date.rs @@ -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 { + //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( diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 8c75455..c6c9a6c 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -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", diff --git a/src/plugin/packer.rs b/src/plugin/packer.rs index b8410df..052a100 100644 --- a/src/plugin/packer.rs +++ b/src/plugin/packer.rs @@ -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)] @@ -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 { - //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")]