Skip to content

Commit

Permalink
Use web-time on Wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Oct 13, 2023
1 parent 2297580 commit 96fee77
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
4 changes: 4 additions & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
registry = ["sharded-slab", "thread_local", "std"]
json = ["tracing-serde", "serde", "serde_json"]
wasm-bindgen = ["web-time"]

# Enables support for local time when using the `time` crate timestamp
# formatters.
Expand Down Expand Up @@ -65,6 +66,9 @@ chrono = { version = "0.4.26", default-features = false, features = ["clock", "s
sharded-slab = { version = "0.1.4", optional = true }
thread_local = { version = "1.1.4", optional = true }

[target.'cfg(all(target_family = "wasm", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
web-time = { version = "0.2", optional = true }

[dev-dependencies]
tracing = { path = "../tracing", version = "0.2" }
tracing-mock = { path = "../tracing-mock", features = ["tracing-subscriber"] }
Expand Down
13 changes: 12 additions & 1 deletion tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ use crate::{
subscribe::{self, Context},
};
use format::{FmtSpan, TimingDisplay};
#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
use web_time::Instant;
#[cfg(not(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
)))]
use std::time::Instant;
use std::{
any::TypeId, cell::RefCell, env, fmt, io, marker::PhantomData, ops::Deref, ptr::NonNull,
time::Instant,
};
use tracing_core::{
field,
Expand Down
20 changes: 16 additions & 4 deletions tracing-subscriber/src/fmt/time/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,19 @@
// obstacle to adoption, that text has been removed.


#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
use web_time as time;
use std::fmt;
#[cfg(not(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
)))]
use std::time;

/// A date/time type which exists primarily to convert `SystemTime` timestamps into an ISO 8601
/// formatted string.
Expand Down Expand Up @@ -243,9 +255,9 @@ impl fmt::Display for DateTime {
}
}

impl From<std::time::SystemTime> for DateTime {
fn from(timestamp: std::time::SystemTime) -> DateTime {
let (t, nanos) = match timestamp.duration_since(std::time::UNIX_EPOCH) {
impl From<time::SystemTime> for DateTime {
fn from(timestamp: time::SystemTime) -> DateTime {
let (t, nanos) = match timestamp.duration_since(time::UNIX_EPOCH) {
Ok(duration) => {
debug_assert!(duration.as_secs() <= std::i64::MAX as u64);
(duration.as_secs() as i64, duration.subsec_nanos())
Expand Down Expand Up @@ -333,7 +345,7 @@ impl From<std::time::SystemTime> for DateTime {
#[cfg(test)]
mod tests {
use std::i32;
use std::time::{Duration, UNIX_EPOCH};
use super::time::{Duration, UNIX_EPOCH};

use super::*;

Expand Down
23 changes: 17 additions & 6 deletions tracing-subscriber/src/fmt/time/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
//! Formatters for event timestamps.
use crate::fmt::format::Writer;
use std::fmt;
use std::time::Instant;
#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
use web_time as time;
#[cfg(not(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
)))]
use std::time as time;

mod datetime;

Expand Down Expand Up @@ -112,19 +123,19 @@ pub struct SystemTime;
/// The `Default` implementation for `Uptime` makes the epoch the current time.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Uptime {
epoch: Instant,
epoch: time::Instant,
}

impl Default for Uptime {
fn default() -> Self {
Uptime {
epoch: Instant::now(),
epoch: time::Instant::now(),
}
}
}

impl From<Instant> for Uptime {
fn from(epoch: Instant) -> Self {
impl From<time::Instant> for Uptime {
fn from(epoch: time::Instant) -> Self {
Uptime { epoch }
}
}
Expand All @@ -134,7 +145,7 @@ impl FormatTime for SystemTime {
write!(
w,
"{}",
datetime::DateTime::from(std::time::SystemTime::now())
datetime::DateTime::from(time::SystemTime::now())
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions tracing-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
//! than `Vec`) as a performance optimization. Enabled by default.
//! - [`parking_lot`]: Use the `parking_lot` crate's `RwLock` implementation
//! rather than the Rust standard library's implementation.
//! - `wasm-bindgen`: Uses the [`web-time`] crate to allow `Uptime` and `SystemTime` to
//! be used in browsers.
//!
//! ### `no_std` Support
//!
Expand Down Expand Up @@ -129,6 +131,7 @@
//! [`time` crate]: https://crates.io/crates/time
//! [`liballoc`]: https://doc.rust-lang.org/alloc/index.html
//! [`libstd`]: https://doc.rust-lang.org/std/index.html
//! [`web-time`]: https://crates.io/crates/web-time
#![doc(
html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
html_favicon_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/favicon.ico",
Expand Down

0 comments on commit 96fee77

Please sign in to comment.