Skip to content

Commit

Permalink
add Packer method
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 19, 2023
1 parent 771c3c8 commit 5b88cc4
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "fast_log"
version = "1.6.2"
version = "1.6.3"
description = "Rust async log High-performance asynchronous logging"
readme = "Readme.md"
authors = ["ce <[email protected]>"]
Expand Down
4 changes: 2 additions & 2 deletions benches/log_mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fast_log::Config;

use fast_log::consts::LogSize;
use fast_log::plugin::file_mmap::MmapFile;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::LogPacker;
use test::{black_box, Bencher};

Expand All @@ -20,7 +20,7 @@ fn bench_log_mmap(b: &mut Bencher) {
.split::<MmapFile, LogPacker>(
"target/logs/temp.log",
LogSize::MB(100),
RollingType::All,
KeepType::All,
LogPacker {},
),
)
Expand Down
4 changes: 2 additions & 2 deletions example/src/bench_test_file_split.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fast_log::bencher::TPS;
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::LogPacker;
use std::time::Instant;

Expand All @@ -14,7 +14,7 @@ fn main() {
.file_split(
"target/logs/",
LogSize::MB(1),
RollingType::All,
KeepType::All,
LogPacker {},
)
.chan_len(Some(100000)),
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::keep::{KeepAll, KeepNum};
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::LogPacker;

fn main() {
//file_path also can use '"target/logs/test.log"'
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::MB(1),
KeepNum(2),
KeepType::KeepNum(2),
LogPacker {},
))
.unwrap();
Expand Down
13 changes: 6 additions & 7 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::error::LogError;
use fast_log::plugin::file_split::Packer;
use fast_log::plugin::keep::KeepNum;
use fast_log::plugin::file_split::{Packer, KeepType};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
Expand All @@ -11,9 +10,9 @@ use std::time::Duration;

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

impl LogDatePacker {
impl DateLogPacker {
pub fn log_name_create_by_time(
&self,
first_file_path: &str,
Expand All @@ -33,7 +32,7 @@ impl LogDatePacker {
return new_log_name;
}
}
impl Packer for LogDatePacker {
impl Packer for DateLogPacker {
fn pack_name(&self) -> &'static str {
"log"
}
Expand Down Expand Up @@ -76,8 +75,8 @@ fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::MB(1),
KeepNum(2),
LogDatePacker {},
KeepType::KeepNum(2),
DateLogPacker {},
))
.unwrap();
for _ in 0..40000 {
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_gz.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::GZipPacker;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
RollingType::KeepNum(5),
KeepType::KeepNum(5),
GZipPacker {},
))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_lz4.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::LZ4Packer;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
RollingType::KeepNum(5),
KeepType::KeepNum(5),
LZ4Packer {},
))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_mmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_mmap::MmapFile;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::LogPacker;

fn main() {
Expand All @@ -12,7 +12,7 @@ fn main() {
.split::<MmapFile, LogPacker>(
"target/logs/temp.log",
LogSize::MB(1),
RollingType::All,
KeepType::All,
LogPacker {},
),
)
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_zip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::file_split::KeepType;
use fast_log::plugin::packer::ZipPacker;

use fast_log::config::Config;
Expand All @@ -8,7 +8,7 @@ fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
LogSize::KB(50),
RollingType::KeepNum(5),
KeepType::KeepNum(5),
ZipPacker {},
))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ impl Config {
self,
file_path: &str,
temp_size: LogSize,
rolling_type: R,
keeper: R,
packer: P,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::<F, P>::new(file_path, temp_size, rolling_type, packer).unwrap(),
FileSplitAppender::<F, P>::new(file_path, temp_size, keeper, packer).unwrap(),
)));
self
}
Expand Down
10 changes: 5 additions & 5 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Log for Logger {
if let Some(filter) = LOGGER.cfg.get() {
if let Some(send) = LOGGER.send.get() {
if !filter.filter.filter(record) {
send.send(FastLogRecord {
let _= send.send(FastLogRecord {
command: Command::CommandRecord,
level: record.level(),
target: record.metadata().target().to_string(),
Expand Down Expand Up @@ -93,13 +93,13 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> {
return Err(LogError::from("[fast_log] appends can not be empty!"));
}
let (s, r) = chan(config.chan_len);
LOGGER.send.set(s).map_err(|e| LogError::from("set fail"))?;
LOGGER.recv.set(r).map_err(|e| LogError::from("set fail"))?;
LOGGER.send.set(s).map_err(|_| LogError::from("set fail"))?;
LOGGER.recv.set(r).map_err(|_| LogError::from("set fail"))?;
LOGGER.set_level(config.level);
LOGGER
.cfg
.set(config)
.map_err(|e| LogError::from("set fail"))?;
.map_err(|_| LogError::from("set fail="))?;
//main recv data
log::set_logger(LOGGER.deref())
.map(|()| log::set_max_level(LOGGER.cfg.get().unwrap().level))
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> {
}
let data = Arc::new(remain);
for x in senders.iter() {
x.send(data.clone());
let _= x.send(data.clone());
}
if exit {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait Filter: Send + Sync {
pub struct NoFilter {}

impl Filter for NoFilter {
fn filter(&self, module: &log::Record) -> bool {
fn filter(&self, _module: &log::Record) -> bool {
return false;
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![allow(unused_variables)]
#![allow(unused_assignments)]
#![allow(unused_must_use)]

extern crate core;

pub mod appender;
Expand Down
6 changes: 3 additions & 3 deletions src/plugin/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl FileAppender {
let log_file_path = log_file_path.replace("\\", "/");
if let Some(right) = log_file_path.rfind("/") {
let path = &log_file_path[0..right];
std::fs::create_dir_all(path);
let _= std::fs::create_dir_all(path);
}
Ok(Self {
file: RefCell::new(
Expand All @@ -34,10 +34,10 @@ impl LogAppender for FileAppender {
for x in records {
buf.push_str(&x.formated);
}
log_file.write_all(buf.as_bytes());
let _= log_file.write_all(buf.as_bytes());
}

fn flush(&self) {
self.file.borrow_mut().flush();
let _= self.file.borrow_mut().flush();
}
}
5 changes: 2 additions & 3 deletions src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::appender::{FastLogRecord, LogAppender};
use crate::consts::LogSize;
use crate::error::LogError;
use crate::plugin::file_split::{FileSplitAppender, SplitFile};
use crate::plugin::keep::KeepNum;
use crate::plugin::file_split::{FileSplitAppender, KeepType, SplitFile};
use crate::plugin::packer::LogPacker;

/// Single logs are stored in rolling mode by capacity
Expand All @@ -16,7 +15,7 @@ impl<F: SplitFile> FileLoopAppender<F> {
file: FileSplitAppender::<F, LogPacker>::new(
log_file_path,
size,
KeepNum(1),
KeepType::KeepNum(1),
LogPacker {},
)?,
})
Expand Down
10 changes: 5 additions & 5 deletions src/plugin/file_mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ impl MmapFile {
let log_file_path = log_file_path.replace("\\", "/");
if let Some(right) = log_file_path.rfind("/") {
let path = &log_file_path[0..right];
std::fs::create_dir_all(path);
let _= std::fs::create_dir_all(path);
}
let file = OpenOptions::new()
.write(true)
.read(true)
.create(true)
.open(&log_file_path)?;
file.set_len(size.get_len() as u64);
file.set_len(size.get_len() as u64)?;
let mmap = unsafe {
MmapOptions::new().map(&file).map_err(|e| {
println!("e={}", e);
Expand Down Expand Up @@ -132,8 +132,8 @@ impl SplitFile for MmapFile {
fn truncate(&self) -> std::io::Result<()> {
let file = unsafe { &mut *self.file.get() };
file.set_len(0)?;
file.flush();
file.set_len(self.size.get_len() as u64);
file.flush()?;
file.set_len(self.size.get_len() as u64)?;
let mmap = unsafe {
MmapOptions::new()
.map(&file)
Expand All @@ -151,7 +151,7 @@ impl SplitFile for MmapFile {
}

fn flush(&self) {
self.bytes.borrow_mut().flush();
let _= self.bytes.borrow_mut().flush();
}

fn len(&self) -> usize {
Expand Down
Loading

0 comments on commit 5b88cc4

Please sign in to comment.