fix rotate error
This commit is contained in:
parent
9ccbb32a41
commit
0078de5ffe
@ -8,7 +8,7 @@ fn do_log() {
|
|||||||
debug!("debug");
|
debug!("debug");
|
||||||
}
|
}
|
||||||
fn criterion_benchmark(c: &mut Criterion) {
|
fn criterion_benchmark(c: &mut Criterion) {
|
||||||
let _guard = default();
|
let _guard = default(false, false);
|
||||||
c.bench_function("log", |b| b.iter(|| do_log()));
|
c.bench_function("log", |b| b.iter(|| do_log()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,6 @@ use rolling_file::default;
|
|||||||
mod submod;
|
mod submod;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _guard = default();
|
let _guard = default(false, false);
|
||||||
submod::do_record();
|
submod::do_record();
|
||||||
}
|
}
|
||||||
|
|||||||
63
src/lib.rs
63
src/lib.rs
@ -76,13 +76,18 @@ impl FileRoller {
|
|||||||
for entry in fs::read_dir(&self.base_dir)? {
|
for entry in fs::read_dir(&self.base_dir)? {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let metadata = entry.metadata()?;
|
let metadata = entry.metadata()?;
|
||||||
if metadata.is_dir() {
|
|
||||||
|
if !metadata.is_file() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(filename) = entry.file_name().to_str() {
|
if let Some(filename) = entry.file_name().to_str() {
|
||||||
let fnames: Vec<_> = filename.split(".").collect();
|
let fnames: Vec<_> = filename.split(".").collect();
|
||||||
if fnames.len() == 2 {
|
if fnames.len() == 2 && fnames[1] == "log" {
|
||||||
if let Ok(dt) = NaiveDateTime::parse_from_str(fnames[0], "%Y%m%d%H%M%S") {
|
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 dt = dt + duration;
|
||||||
let created_at = get_gap_with_time(&self.period, dt);
|
let created_at = get_gap_with_time(&self.period, dt);
|
||||||
if self.current_gap >= created_at {
|
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 {
|
impl io::Write for FileRoller {
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
if let Err(e) = self.rollover() {
|
if let Err(e) = self.rollover() {
|
||||||
@ -207,14 +223,16 @@ pub fn new_log<P: AsRef<Path>>(
|
|||||||
maxfile: usize,
|
maxfile: usize,
|
||||||
period: PeriodGap,
|
period: PeriodGap,
|
||||||
level: tracing::Level,
|
level: tracing::Level,
|
||||||
|
with_filename: bool,
|
||||||
|
with_line_number: bool,
|
||||||
) -> tracing_appender::non_blocking::WorkerGuard {
|
) -> tracing_appender::non_blocking::WorkerGuard {
|
||||||
let timefmt = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
|
let timefmt = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
|
||||||
let timer = OffsetTime::new(offset!(+8), timefmt);
|
let timer = OffsetTime::new(offset!(+8), timefmt);
|
||||||
let roller = FileRoller::new(path, maxfile, period);
|
let roller = FileRoller::new(path, maxfile, period);
|
||||||
let (writer, guard) = tracing_appender::non_blocking(roller);
|
let (writer, guard) = tracing_appender::non_blocking(roller);
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_line_number(true)
|
.with_line_number(with_line_number)
|
||||||
.with_file(true)
|
.with_file(with_filename)
|
||||||
.with_writer(writer)
|
.with_writer(writer)
|
||||||
.with_max_level(level)
|
.with_max_level(level)
|
||||||
.with_timer(timer)
|
.with_timer(timer)
|
||||||
@ -224,23 +242,42 @@ pub fn new_log<P: AsRef<Path>>(
|
|||||||
guard
|
guard
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default() -> tracing_appender::non_blocking::WorkerGuard {
|
pub fn default(
|
||||||
new_log("./.output", 7, PeriodGap::Daily, tracing::Level::DEBUG)
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::io::Write;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() -> io::Result<()> {
|
fn it_works() -> io::Result<()> {
|
||||||
let mut roller = FileRoller::new("output", 8, PeriodGap::Secondly);
|
let _guard = default_second();
|
||||||
for _ in 0..200 {
|
for _ in 0..10 {
|
||||||
let _ = roller.write_all("hello\n".as_bytes())?;
|
tracing::debug!("hello1");
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(1000));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user