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

feature: add support for chrono #18

Merged
merged 10 commits into from Dec 15, 2023
34 changes: 25 additions & 9 deletions .github/workflows/ci.yml
Expand Up @@ -7,8 +7,8 @@ jobs:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
Expand All @@ -21,8 +21,8 @@ jobs:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
Expand All @@ -35,8 +35,8 @@ jobs:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
Expand All @@ -51,8 +51,8 @@ jobs:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
Expand All @@ -61,4 +61,20 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
args: -- -D warnings

cargo-hack:
needs: check
name: cargo check (feature combinations)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
- uses: taiki-e/install-action@cargo-hack
with:
command: cargo
args: hack check --feature-powerset --no-dev-deps
8 changes: 7 additions & 1 deletion Cargo.toml
Expand Up @@ -11,8 +11,9 @@ documentation = "https://docs.rs/tracing-glog"

[dependencies]
tracing = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.3.3", features = ["std", "fmt", "registry", "time", "local-time"], default-features = false }
tracing-subscriber = { version = "0.3.18", features = ["std", "fmt", "chrono", "registry", "time", "local-time", "chrono"], default-features = false }
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
time = { version = "0.3.9", features = ["formatting"] }
chrono = { version = "0.4.20", optional = true }
nu-ansi-term = { version = "0.46", optional = true }
tracing-log = { version = "0.1", optional = true }

Expand All @@ -27,6 +28,11 @@ tokio = { version = "1.21", features = ["full"] }
default = ["ansi"]
ansi = ["nu-ansi-term", "tracing-subscriber/ansi"]
tracing-log = ["dep:tracing-log"]
chrono = ["dep:chrono", "tracing-subscriber/chrono"]

[[example]]
name = "tokio"
required-features = ["chrono", "ansi"]

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 2 additions & 2 deletions examples/tokio.rs
@@ -1,7 +1,7 @@
use anyhow::Error;
use tokio::task::JoinSet;
use tracing::{debug, info, instrument, span, Instrument as _, Level};
use tracing_glog::{Glog, GlogFields, UtcTime};
use tracing_glog::{ChronoLocalTime, Glog, GlogFields};

#[instrument]
async fn parent_task(subtasks: usize) -> Result<(), Error> {
Expand Down Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<(), Error> {
Glog::default()
.with_target(false)
.with_thread_names(false)
.with_timer(UtcTime::default()),
.with_timer(ChronoLocalTime::default()),
)
.fmt_fields(GlogFields::default())
.init();
Expand Down
80 changes: 80 additions & 0 deletions src/format.rs
Expand Up @@ -5,6 +5,9 @@ use time::{format_description::FormatItem, formatting::Formattable, OffsetDateTi
use tracing::{Level, Metadata};
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};

#[cfg(feature = "chrono")]
use tracing_subscriber::fmt::time::{ChronoLocal, ChronoUtc};

/// A bridge between `fmt::Write` and `io::Write`.
///
/// This is used by the timestamp formatting implementation for the `time`
Expand Down Expand Up @@ -134,6 +137,45 @@ impl Default for UtcTime {
}
}

/// Formats the current [UTC time] using [`chrono` crate].
///
/// To format the current local time instead, use the [`ChronoLocalTime`]
/// or the [`LocalTime`] type.
///
/// [UTC time]: ChronoUtc
/// [`chrono` crate]: chrono
#[cfg(feature = "chrono")]
#[derive(Clone, Debug)]
pub struct ChronoUtcTime {
time: ChronoUtc,
}

#[cfg(feature = "chrono")]
impl FormatTime for ChronoUtcTime {
fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result {
#[cfg(feature = "ansi")]
if w.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(w, "{}", style.prefix())?;
self.time.format_time(w)?;
write!(w, "{}", style.suffix())?;
return Ok(());
}

self.time.format_time(w)
}
}

#[cfg(feature = "chrono")]
impl Default for ChronoUtcTime {
fn default() -> Self {
let fmt_string = String::from("%m%d %H:%M:%S%.6f");
Self {
time: ChronoUtc::new(fmt_string),
}
}
}

/// Formats the current [local time] using a [formatter] from the [`time` crate].
///
/// To format the current [UTC time] instead, use the [`UtcTime`] type.
Expand Down Expand Up @@ -189,6 +231,44 @@ where
}
}

/// Formats the current [`local time`] using [`chrono` crate].
///
/// To format the UTC time instead, use the [`ChronoUtcTime`]
/// or the [`UtcTime`] type.
///
/// [`local time`]: ChronoLocal
/// [`chrono` crate]: chrono
#[cfg(feature = "chrono")]
pub struct ChronoLocalTime {
time: ChronoLocal,
}

#[cfg(feature = "chrono")]
impl FormatTime for ChronoLocalTime {
fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result {
#[cfg(feature = "ansi")]
if w.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(w, "{}", style.prefix())?;
self.time.format_time(w)?;
write!(w, "{}", style.suffix())?;
return Ok(());
}

self.time.format_time(w)
}
}

#[cfg(feature = "chrono")]
impl Default for ChronoLocalTime {
fn default() -> Self {
let fmt_string = String::from("%m%d %H:%M:%S%.6f");
Self {
time: ChronoLocal::new(fmt_string),
}
}
}

fn format_datetime(
into: &mut Writer<'_>,
now: OffsetDateTime,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -124,6 +124,8 @@ mod nu_ansi_term {

use crate::nu_ansi_term::Style;
use format::FmtLevel;
#[cfg(feature = "chrono")]
pub use format::{ChronoLocalTime, ChronoUtcTime};
pub use format::{LocalTime, UtcTime};
use std::fmt;
use tracing::{
Expand Down