fix rotate error

This commit is contained in:
asxalex 2024-02-29 22:09:13 +08:00
parent 9ccbb32a41
commit 0078de5ffe
3 changed files with 52 additions and 15 deletions

View File

@ -8,7 +8,7 @@ fn do_log() {
debug!("debug");
}
fn criterion_benchmark(c: &mut Criterion) {
let _guard = default();
let _guard = default(false, false);
c.bench_function("log", |b| b.iter(|| do_log()));
}

View File

@ -2,6 +2,6 @@ use rolling_file::default;
mod submod;
fn main() {
let _guard = default();
let _guard = default(false, false);
submod::do_record();
}

View File

@ -76,13 +76,18 @@ impl FileRoller {
for entry in fs::read_dir(&self.base_dir)? {
let entry = entry?;
let metadata = entry.metadata()?;
if metadata.is_dir() {
if !metadata.is_file() {
continue;
}
if let Some(filename) = entry.file_name().to_str() {
let fnames: Vec<_> = filename.split(".").collect();
if fnames.len() == 2 {
if let Ok(dt) = NaiveDateTime::parse_from_str(fnames[0], "%Y%m%d%H%M%S") {
if fnames.len() == 2 && fnames[1] == "log" {
if let Ok(dt) = NaiveDateTime::parse_from_str(
&get_filename_format(fnames[0], &self.period),
"%Y%m%d%H%M%S",
) {
let dt = dt + duration;
let created_at = get_gap_with_time(&self.period, dt);
if self.current_gap >= created_at {
@ -114,6 +119,17 @@ impl FileRoller {
}
}
// change the filename to the "20060102000000" format
fn get_filename_format(fname: &str, period: &PeriodGap) -> String {
match *period {
PeriodGap::Secondly => fname.to_string(),
PeriodGap::Minutely => format!("{}00", fname),
PeriodGap::Hourly => format!("{}0000", fname),
PeriodGap::Daily => format!("{}000000", fname),
PeriodGap::Monthly => format!("{}01000000", fname),
}
}
impl io::Write for FileRoller {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if let Err(e) = self.rollover() {
@ -207,14 +223,16 @@ pub fn new_log<P: AsRef<Path>>(
maxfile: usize,
period: PeriodGap,
level: tracing::Level,
with_filename: bool,
with_line_number: bool,
) -> tracing_appender::non_blocking::WorkerGuard {
let timefmt = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
let timer = OffsetTime::new(offset!(+8), timefmt);
let roller = FileRoller::new(path, maxfile, period);
let (writer, guard) = tracing_appender::non_blocking(roller);
tracing_subscriber::fmt()
.with_line_number(true)
.with_file(true)
.with_line_number(with_line_number)
.with_file(with_filename)
.with_writer(writer)
.with_max_level(level)
.with_timer(timer)
@ -224,23 +242,42 @@ pub fn new_log<P: AsRef<Path>>(
guard
}
pub fn default() -> tracing_appender::non_blocking::WorkerGuard {
new_log("./.output", 7, PeriodGap::Daily, tracing::Level::DEBUG)
pub fn default(
with_filename: bool,
with_line_number: bool,
) -> tracing_appender::non_blocking::WorkerGuard {
new_log(
"./.output",
7,
PeriodGap::Daily,
tracing::Level::DEBUG,
with_filename,
with_line_number,
)
}
pub fn default_second() -> tracing_appender::non_blocking::WorkerGuard {
new_log(
"./.output",
7,
PeriodGap::Secondly,
tracing::Level::DEBUG,
false,
false,
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use std::thread;
use std::time::Duration;
#[test]
fn it_works() -> io::Result<()> {
let mut roller = FileRoller::new("output", 8, PeriodGap::Secondly);
for _ in 0..200 {
let _ = roller.write_all("hello\n".as_bytes())?;
thread::sleep(Duration::from_millis(100));
let _guard = default_second();
for _ in 0..10 {
tracing::debug!("hello1");
thread::sleep(Duration::from_millis(1000));
}
Ok(())
}