Skip to content

Remove old warnings, use more efficient methods #3237

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ once_cell = { optional = true, version = "1.13.0" }
# fmt
tracing-log = { path = "../tracing-log", version = "0.2", optional = true, default-features = false, features = ["log-tracer", "std"] }
nu-ansi-term = { version = "0.46.0", optional = true }
time = { version = "0.3.2", features = ["formatting"], optional = true }
time = { version = "0.3.38", features = ["formatting"], optional = true }

# only required by the json feature
serde_json = { version = "1.0.82", optional = true }
Expand All @@ -76,7 +76,7 @@ regex = { version = "1.6.0", default-features = false, features = ["std"] }
tracing-futures = { path = "../tracing-futures", version = "0.3", default-features = false, features = ["std-future", "std"] }
tokio = { version = "1.20.0", features = ["rt", "macros"] }
# Enable the `time` crate's `macros` feature, for examples.
time = { version = "0.3.2", features = ["formatting", "macros"] }
time = { version = "0.3.38", features = ["formatting", "macros"] }

[badges]
maintenance = { status = "experimental" }
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod time_crate;
pub use time_crate::UtcTime;

#[cfg(feature = "local-time")]
#[cfg_attr(docsrs, doc(cfg(all(unsound_local_offset, feature = "local-time"))))]
#[cfg_attr(docsrs, doc(cfg(feature = "local-time")))]
pub use time_crate::LocalTime;

/// [`chrono`]-based implementation for [`FormatTime`].
Expand Down
50 changes: 17 additions & 33 deletions tracing-subscriber/src/fmt/time/time_crate.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
use crate::fmt::{format::Writer, time::FormatTime, writer::WriteAdaptor};
use std::fmt;
use time::{format_description::well_known, formatting::Formattable, OffsetDateTime};
use time::{format_description::well_known, formatting::Formattable, OffsetDateTime, UtcDateTime};

/// Formats the current [local time] using a [formatter] from the [`time` crate].
///
/// To format the current [UTC time] instead, use the [`UtcTime`] type.
///
/// <div class="example-wrap" style="display:inline-block">
/// <pre class="compile_fail" style="white-space:normal;font:inherit;">
/// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"><code>time</code>
/// crate</a> must be compiled with <code>--cfg unsound_local_offset</code> in order to use
/// local timestamps. When this cfg is not enabled, local timestamps cannot be recorded, and
/// events will be logged without timestamps.
///
/// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"><code>time</code>
/// documentation</a> for more details.
/// </pre></div>
///
/// [local time]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_local
/// [UTC time]: https://docs.rs/time/0.3/time/struct.OffsetDateTime.html#method.now_utc
/// [formatter]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html
/// [`time` crate]: https://docs.rs/time/0.3/time/
#[derive(Clone, Debug)]
#[cfg_attr(
docsrs,
doc(cfg(all(unsound_local_offset, feature = "time", feature = "local-time")))
)]
#[cfg_attr(docsrs, doc(cfg(all(feature = "time", feature = "local-time"))))]
#[cfg(feature = "local-time")]
pub struct LocalTime<F> {
format: F,
Expand Down Expand Up @@ -76,19 +62,6 @@ impl<F: Formattable> LocalTime<F> {
/// [`time` crate] with the provided provided format. The format may be any
/// type that implements the [`Formattable`] trait.
///
///
/// <div class="example-wrap" style="display:inline-block">
/// <pre class="compile_fail" style="white-space:normal;font:inherit;">
/// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/">
/// <code>time</code> crate</a> must be compiled with <code>--cfg
/// unsound_local_offset</code> in order to use local timestamps. When this
/// cfg is not enabled, local timestamps cannot be recorded, and
/// events will be logged without timestamps.
///
/// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags">
/// <code>time</code> documentation</a> for more details.
/// </pre></div>
///
/// Typically, the format will be a format description string, or one of the
/// `time` crate's [well-known formats].
///
Expand Down Expand Up @@ -163,7 +136,7 @@ where
{
fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result {
let now = OffsetDateTime::now_local().map_err(|_| fmt::Error)?;
format_datetime(now, w, &self.format)
format_offsetdatetime(now, w, &self.format)
}
}

Expand Down Expand Up @@ -261,7 +234,7 @@ impl<F: Formattable> UtcTime<F> {
/// # drop(collector);
/// ```
///
/// [UTC time]: https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.now_utc
/// [UTC time]: https://docs.rs/time/latest/time/struct.UtcDateTime.html#method.now
/// [`time` crate]: https://docs.rs/time/0.3/time/
/// [`Formattable`]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html
/// [well-known formats]: https://docs.rs/time/0.3/time/format_description/well_known/index.html
Expand All @@ -278,7 +251,7 @@ where
F: Formattable,
{
fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result {
format_datetime(OffsetDateTime::now_utc(), w, &self.format)
format_utcdatetime(UtcDateTime::now(), w, &self.format)
}
}

Expand All @@ -291,7 +264,7 @@ where
}
}

fn format_datetime(
fn format_offsetdatetime(
now: OffsetDateTime,
into: &mut Writer<'_>,
fmt: &impl Formattable,
Expand All @@ -301,3 +274,14 @@ fn format_datetime(
.map_err(|_| fmt::Error)
.map(|_| ())
}

fn format_utcdatetime(
now: UtcDateTime,
into: &mut Writer<'_>,
fmt: &impl Formattable,
) -> fmt::Result {
let mut into = WriteAdaptor::new(into);
now.format_into(&mut into, fmt)
.map_err(|_| fmt::Error)
.map(|_| ())
}
Loading