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 5 commits
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 .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --all
args: --all --exclude=tracing-appender
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also update the MSRV in tracing-appender's documentation & readme?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup!


check:
# Run `cargo check` first to ensure that the pushed code at least compiles.
Expand Down
5 changes: 3 additions & 2 deletions tracing-appender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ 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"] }

[dependencies.tracing-subscriber]
path = "../tracing-subscriber"
version = "0.3"
default-features = false
features = ["fmt"]
features = ["fmt", "std"]

[dev-dependencies]
tracing = { path = "../tracing", version = "0.2" }
time = { version = "0.3", default-features = false, features = ["formatting", "parsing"] }
tempfile = "3"
20 changes: 12 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,12 @@ impl InnerAppender {
}
}

fn should_rollover(&self, date: DateTime<Utc>) -> bool {
date >= self.next_date
fn should_rollover(&self, date: OffsetDateTime) -> bool {
// the `None` case means that the `InnerAppender` *never* rorates log files.
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