Skip to content

Commit

Permalink
opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 24, 2021
1 parent 35075db commit 4822155
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 63 deletions.
9 changes: 3 additions & 6 deletions src/plugin/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ pub struct FileAppender {
impl FileAppender {
pub fn new(log_file_path: &str) -> FileAppender {
let log_file_path = log_file_path.replace("\\", "/");
match log_file_path.rfind("/") {
None => {}
Some(right) => {
let path = &log_file_path[0..right];
std::fs::create_dir_all(path);
}
if let Some(right) = log_file_path.rfind("/") {
let path = &log_file_path[0..right];
std::fs::create_dir_all(path);
}
Self {
file: RefCell::new(
Expand Down
68 changes: 24 additions & 44 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,24 @@ pub enum RollingType {
impl RollingType {
fn read_paths(&self, dir: &str) -> Vec<DirEntry> {
let paths = std::fs::read_dir(dir);
match paths {
Ok(paths) => {
let mut paths_vec = vec![];
for path in paths {
match path {
Ok(path) => {
if let Some(v) = path.file_name().to_str() {
//filter temp.log and not start with temp
if v.ends_with("temp.log") || !v.starts_with("temp") {
continue;
}
if let Ok(paths) = paths {
let mut paths_vec = vec![];
for path in paths {
match path {
Ok(path) => {
if let Some(v) = path.file_name().to_str() {
//filter temp.log and not start with temp
if v.ends_with("temp.log") || !v.starts_with("temp") {
continue;
}
paths_vec.push(path);
}
_ => {}
paths_vec.push(path);
}
_ => {}
}
paths_vec.sort_by(|a, b| b.file_name().cmp(&a.file_name()));
return paths_vec;
}
_ => {}
paths_vec.sort_by(|a, b| b.file_name().cmp(&a.file_name()));
return paths_vec;
}
return vec![];
}
Expand Down Expand Up @@ -93,13 +90,10 @@ impl RollingType {
let item = &paths_vec[index];
let file_name = item.file_name();
let name = file_name.to_str().unwrap_or("").to_string();
match self.file_name_parse_time(&name) {
Some(time) => {
if now.sub(time) > duration {
std::fs::remove_file(item.path());
}
if let Some(time) = self.file_name_parse_time(&name) {
if now.sub(time) > duration {
std::fs::remove_file(item.path());
}
_ => {}
}
}
}
Expand All @@ -114,11 +108,8 @@ impl RollingType {
time_str = time_str[0..v].to_string();
}
let time = chrono::NaiveDateTime::parse_from_str(&time_str, "%Y_%m_%dT%H_%M_%S");
match time {
Ok(time) => {
return Some(time);
}
_ => {}
if let Ok(time) = time {
return Some(time);
}
}
return None;
Expand Down Expand Up @@ -214,11 +205,8 @@ impl FileSplitAppender {
}
let mut file = file.unwrap();
let mut temp_bytes = 0;
match file.metadata() {
Ok(m) => {
temp_bytes = m.len() as usize;
}
_ => {}
if let Ok(m) = file.metadata() {
temp_bytes = m.len() as usize;
}
file.seek(SeekFrom::Start(temp_bytes as u64));
let (sender, receiver) = crossbeam_channel::bounded(log_pack_cap);
Expand Down Expand Up @@ -247,11 +235,8 @@ impl LogAppender for FileSplitAppender {
}
let mut write_bytes = 0;
let w = data.file.write(record.formated.as_bytes());
match w {
Ok(w) => {
write_bytes = write_bytes + w;
}
_ => {}
if let Ok(w) = w {
write_bytes = write_bytes + w;
}
data.file.flush();
data.temp_bytes += write_bytes;
Expand All @@ -267,20 +252,15 @@ fn spawn_saver_thread(r: Receiver<LogPack>, packer: Box<dyn Packer>) {
pack.rolling.do_rolling(&pack.dir);
let log_file_path = pack.new_log_name.clone();
//do save pack
match pack.pack_name.as_str() {
p_name => {
do_pack(&packer, pack);
}
_ => {}
}
do_pack(&packer, pack);
std::fs::remove_file(log_file_path);
}
}
});
}

/// write an Pack to zip file
pub fn do_pack(packer: &Box<dyn Packer>, mut pack: LogPack) -> Result<(), LogPack>{
pub fn do_pack(packer: &Box<dyn Packer>, mut pack: LogPack) -> Result<(), LogPack> {
let log_file_path = pack.new_log_name.as_str();
if log_file_path.is_empty() {
return Err(pack);
Expand Down
21 changes: 8 additions & 13 deletions src/plugin/packer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ impl Packer for ZipPacker {

fn do_pack(&self, log_file: File, log_file_path: &str) -> Result<(), LogError> {
let mut log_name = log_file_path.replace("\\", "/").to_string();
match log_file_path.rfind("/") {
Some(v) => {
log_name = log_name[(v + 1)..log_name.len()].to_string();
}
_ => {}
if let Some(v) = log_file_path.rfind("/") {
log_name = log_name[(v + 1)..log_name.len()].to_string();
}
let zip_path = log_file_path.replace(".log", ".zip");
let zip_file = std::fs::File::create(&zip_path);
Expand Down Expand Up @@ -64,12 +61,14 @@ impl Packer for ZipPacker {
/// you need enable fast_log = { ... ,features=["lz4"]}
#[cfg(feature = "lz4")]
use lz4::EncoderBuilder;

#[cfg(feature = "lz4")]
impl From<std::io::Error> for LogError{
impl From<std::io::Error> for LogError {
fn from(arg: std::io::Error) -> Self {
LogError::E(arg.to_string())
}
}

/// the zip compress
#[cfg(feature = "lz4")]
pub struct LZ4Packer {}
Expand All @@ -81,13 +80,9 @@ impl Packer for LZ4Packer {
}

fn do_pack(&self, log_file: File, log_file_path: &str) -> Result<(), LogError> {

let mut log_name = log_file_path.replace("\\", "/").to_string();
match log_file_path.rfind("/") {
Some(v) => {
log_name = log_name[(v + 1)..log_name.len()].to_string();
}
_ => {}
if let Some(v) = log_file_path.rfind("/") {
log_name = log_name[(v + 1)..log_name.len()].to_string();
}
let lz4_path = log_file_path.replace(".log", ".lz4");
let lz4_file = std::fs::File::create(&lz4_path);
Expand All @@ -104,7 +99,7 @@ impl Packer for LZ4Packer {
let mut encoder = EncoderBuilder::new()
.level(0)
.build(lz4_file)?;
// io::copy(&mut lz4_file, &mut encoder)?;
// io::copy(&mut lz4_file, &mut encoder)?;
//buf reader
let mut r = BufReader::new(log_file);
let mut buf = String::new();
Expand Down

0 comments on commit 4822155

Please sign in to comment.