Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appender: replace chrono with time #1652

Merged
merged 14 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tracing-appender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ edition = "2018"

[dependencies]
crossbeam-channel = "0.5.0"
chrono = { version = "0.4.16", default-features = false, features = ["clock", "std"] }
time = { version = "0.3", default-features = false, features = ["formatting", "macros"] }
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved

[dependencies.tracing-subscriber]
path = "../tracing-subscriber"
Expand Down
19 changes: 11 additions & 8 deletions tracing-appender/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ use std::io::{BufWriter, Write};
use std::{fs, io};

use crate::rolling::Rotation;
use chrono::prelude::*;
use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::path::Path;
use time::OffsetDateTime;

#[derive(Debug)]
pub(crate) struct InnerAppender {
log_directory: String,
log_filename_prefix: String,
writer: BufWriter<File>,
next_date: DateTime<Utc>,
next_date: Option<OffsetDateTime>,
rotation: Rotation,
}

impl io::Write for InnerAppender {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let now = Utc::now();
let now = OffsetDateTime::now_utc();
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
self.write_timestamped(buf, now)
}

Expand All @@ -32,7 +32,7 @@ impl InnerAppender {
log_directory: &Path,
log_filename_prefix: &Path,
rotation: Rotation,
now: DateTime<Utc>,
now: OffsetDateTime,
) -> io::Result<Self> {
let log_directory = log_directory.to_str().unwrap();
let log_filename_prefix = log_filename_prefix.to_str().unwrap();
Expand All @@ -49,15 +49,15 @@ impl InnerAppender {
})
}

fn write_timestamped(&mut self, buf: &[u8], date: DateTime<Utc>) -> io::Result<usize> {
fn write_timestamped(&mut self, buf: &[u8], date: OffsetDateTime) -> io::Result<usize> {
// Even if refresh_writer fails, we still have the original writer. Ignore errors
// and proceed with the write.
let buf_len = buf.len();
self.refresh_writer(date);
self.writer.write_all(buf).map(|_| buf_len)
}

fn refresh_writer(&mut self, now: DateTime<Utc>) {
fn refresh_writer(&mut self, now: OffsetDateTime) {
if self.should_rollover(now) {
let filename = self.rotation.join_date(&self.log_filename_prefix, &now);

Expand All @@ -75,8 +75,11 @@ impl InnerAppender {
}
}

fn should_rollover(&self, date: DateTime<Utc>) -> bool {
date >= self.next_date
fn should_rollover(&self, date: OffsetDateTime) -> bool {
match self.next_date {
None => false,
Some(next_date) => date >= next_date,
}
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading