Skip to content

Commit

Permalink
bump v1.7.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Nov 13, 2024
1 parent fd32d6e commit 8160e72
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ members = [

[package]
name = "fast_log"
version = "1.7.4"
version = "1.7.5"
description = "Rust async log High-performance asynchronous logging"
readme = "Readme.md"
readme = "README.md"
authors = ["ce <[email protected]>"]
repository = "https://github.com/rbatis/fast_log"
edition = "2021"
Expand Down
23 changes: 18 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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, CanRollingPack, Keep, Packer, RawFile, SplitFile};
use crate::plugin::file_split::{
CanRollingPack, FileSplitAppender, Keep, Packer, RawFile, SplitFile,
};
use crate::FastLogFormat;
use dark_std::sync::SyncVec;
use log::LevelFilter;
Expand Down Expand Up @@ -106,7 +108,7 @@ impl Config {
R: CanRollingPack + 'static,
K: Keep + 'static,
P: Packer + Sync + 'static,
>(
>(
self,
file_path: &str,
rolling: R,
Expand All @@ -120,7 +122,7 @@ impl Config {
Box::new(keeper),
Box::new(packer),
)
.unwrap(),
.expect("new split file fail"),
)));
self
}
Expand All @@ -146,15 +148,26 @@ impl Config {
/// );
/// }
/// ```
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static, H: CanRollingPack + 'static>(
pub fn split<
F: SplitFile + 'static,
R: Keep + 'static,
P: Packer + Sync + 'static,
H: CanRollingPack + 'static,
>(
self,
file_path: &str,
keeper: R,
packer: P,
how_pack: H,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<F>(file_path, Box::new(how_pack), Box::new(keeper), Box::new(packer)).unwrap(),
FileSplitAppender::new::<F>(
file_path,
Box::new(how_pack),
Box::new(keeper),
Box::new(packer),
)
.expect("new split file fail"),
)));
self
}
Expand Down
78 changes: 46 additions & 32 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub static LOGGER: OnceLock<Logger> = OnceLock::new();

/// get Logger,but you must call `fast_log::init`
pub fn logger() -> &'static Logger {
LOGGER.get_or_init(|| { Logger::default() })
LOGGER.get_or_init(|| Logger::default())
}

pub struct Logger {
Expand Down Expand Up @@ -103,21 +103,27 @@ 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(|_| LogError::from("set fail"))?;
logger().recv.set(r).map_err(|_| 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(|_| LogError::from("set fail="))?;
//main recv data
log::set_logger(logger())
.map(|()| log::set_max_level(logger().cfg.get().unwrap().level))
.map(|()| log::set_max_level(logger().cfg.get().expect("logger cfg is none").level))
.map_err(|e| LogError::from(e))?;

let mut receiver_vec = vec![];
let mut sender_vec: Vec<Sender<Arc<Vec<FastLogRecord>>>> = vec![];
let cfg = logger().cfg.get().unwrap();
let cfg = logger().cfg.get().expect("logger cfg is none");
for a in cfg.appends.iter() {
let (s, r) = chan(cfg.chan_len);
sender_vec.push(s);
Expand Down Expand Up @@ -172,39 +178,47 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> {
let senders = sender_vec.clone();
spawn(move || {
loop {
let recv = logger().recv.get().unwrap();
let mut remain = Vec::with_capacity(recv.len());
//recv
if recv.len() == 0 {
if let Ok(item) = recv.recv() {
remain.push(item);
if let Some(recv) = logger().recv.get() {
let mut remain = Vec::with_capacity(recv.len());
//recv
if recv.len() == 0 {
if let Ok(item) = recv.recv() {
remain.push(item);
}
}
}
//merge log
loop {
match recv.try_recv() {
Ok(v) => {
remain.push(v);
//merge log
loop {
match recv.try_recv() {
Ok(v) => {
remain.push(v);
}
Err(_) => {
break;
}
}
Err(_) => {
break;
}
let mut exit = false;
for x in &mut remain {
if x.formated.is_empty() {
logger()
.cfg
.get()
.expect("logger cfg is none")
.format
.do_format(x);
}
if x.command.eq(&Command::CommandExit) {
exit = true;
}
}
}
let mut exit = false;
for x in &mut remain {
if x.formated.is_empty() {
logger().cfg.get().unwrap().format.do_format(x);
let data = Arc::new(remain);
for x in senders.iter() {
let _ = x.send(data.clone());
}
if x.command.eq(&Command::CommandExit) {
exit = true;
if exit {
break;
}
}
let data = Arc::new(remain);
for x in senders.iter() {
let _ = x.send(data.clone());
}
if exit {
} else {
break;
}
}
Expand Down
96 changes: 52 additions & 44 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ impl Packer for Box<dyn Packer> {
pub trait CanRollingPack: Send {
/// is it can do rolling? just return Some(log_file_name).notice the log_file_name must have prefix of temp_name.
/// if you return None, it's not do rolling now.
fn can(&mut self, appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String>;
fn can(
&mut self,
appender: &dyn Packer,
temp_name: &str,
temp_size: usize,
arg: &FastLogRecord,
) -> Option<String>;
}

/// keep logs, for example keep by log num or keep by log create time.
Expand Down Expand Up @@ -91,7 +97,6 @@ pub trait SplitFile: Send {
fn offset(&self) -> usize;
}


///only use File
pub struct RawFile {
pub inner: RefCell<File>,
Expand Down Expand Up @@ -156,7 +161,6 @@ impl SplitFile for RawFile {
}
}


pub enum DateType {
Sec,
Hour,
Expand All @@ -172,7 +176,6 @@ impl Default for DateType {
}
}


#[allow(dead_code)]
pub struct DurationType {
last: DateTime,
Expand All @@ -191,7 +194,6 @@ impl DurationType {
}
}


pub struct Rolling {
last: SystemTime,
pub how: RollingType,
Expand All @@ -214,38 +216,35 @@ pub enum RollingType {
}

impl CanRollingPack for Rolling {
fn can(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String> {
fn can(
&mut self,
_appender: &dyn Packer,
temp_name: &str,
temp_size: usize,
arg: &FastLogRecord,
) -> Option<String> {
let last_time = self.last.clone();
self.last = arg.now.clone();
return match &mut self.how {
RollingType::ByDate(date_type) => {
let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec());
let log_time = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let diff = match date_type {
DateType::Sec => {
log_time.sec() != last_time.sec()
}
DateType::Hour => {
log_time.hour() != last_time.hour()
}
DateType::Minute => {
log_time.minute() != last_time.minute()
}
DateType::Day => {
log_time.day() != last_time.day()
}
DateType::Month => {
log_time.mon() != last_time.mon()
}
DateType::Year => {
log_time.year() != last_time.year()
}
DateType::Sec => log_time.sec() != last_time.sec(),
DateType::Hour => log_time.hour() != last_time.hour(),
DateType::Minute => log_time.minute() != last_time.minute(),
DateType::Day => log_time.day() != last_time.day(),
DateType::Month => log_time.mon() != last_time.mon(),
DateType::Year => log_time.year() != last_time.year(),
};
if diff {
let log_name = {
if let Some(idx) = temp_name.rfind(".") {
let suffix = &temp_name[idx..];
temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)))
temp_name.replace(
suffix,
&last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)),
)
} else {
let mut temp_name = temp_name.to_string();
temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000"));
Expand All @@ -260,10 +259,14 @@ impl CanRollingPack for Rolling {
RollingType::BySize(limit) => {
if temp_size >= limit.get_len() {
let log_name = {
let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec());
let last_time =
DateTime::from_system_time(last_time, fastdate::offset_sec());
if let Some(idx) = temp_name.rfind(".") {
let suffix = &temp_name[idx..];
temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)))
temp_name.replace(
suffix,
&last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)),
)
} else {
let mut temp_name = temp_name.to_string();
temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000"));
Expand All @@ -284,7 +287,10 @@ impl CanRollingPack for Rolling {
let log_name = {
if let Some(idx) = temp_name.rfind(".") {
let suffix = &temp_name[idx..];
temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)))
temp_name.replace(
suffix,
&last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)),
)
} else {
let mut temp_name = temp_name.to_string();
temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000"));
Expand Down Expand Up @@ -350,12 +356,7 @@ impl FileSplitAppender {
let _ = file.seek(SeekFrom::Start(temp_bytes.load(Ordering::Relaxed) as u64));
let (sender, receiver) = chan(None);
let arc_packer = Arc::new(packer);
spawn_saver(
temp_name.clone(),
receiver,
keeper,
arc_packer.clone(),
);
spawn_saver(temp_name.clone(), receiver, keeper, arc_packer.clone());
Ok(Self {
temp_bytes,
dir_path: dir_path.to_string(),
Expand Down Expand Up @@ -412,15 +413,12 @@ impl LogPack {
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",
log_file_path
)));
}
.open(log_file_path)
.map_err(|e| {
LogError::from(format!("open(log_file_path={}) fail={}", log_file_path, e))
})?;
//make
let r = packer.do_pack(log_file.unwrap(), log_file_path);
let r = packer.do_pack(log_file, log_file_path);
if r.is_err() && packer.retry() > 0 {
let mut retry = 1;
while let Err(_packs) = self.do_pack(packer) {
Expand Down Expand Up @@ -506,7 +504,12 @@ impl LogAppender for FileSplitAppender {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed)
+ temp.as_bytes().len()
+ x.formated.as_bytes().len();
if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) {
if let Some(new_log_name) = self.can_pack.can(
self.packer.deref(),
&self.temp_name,
current_temp_size,
x,
) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand All @@ -526,7 +529,12 @@ impl LogAppender for FileSplitAppender {
Command::CommandExit => {}
Command::CommandFlush(ref w) => {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed);
if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) {
if let Some(new_log_name) = self.can_pack.can(
self.packer.deref(),
&self.temp_name,
current_temp_size,
x,
) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand Down
Loading

0 comments on commit 8160e72

Please sign in to comment.