diff options
Diffstat (limited to 'vendor/tracing-tree')
-rw-r--r-- | vendor/tracing-tree/.cargo-checksum.json | 2 | ||||
-rw-r--r-- | vendor/tracing-tree/Cargo.lock | 128 | ||||
-rw-r--r-- | vendor/tracing-tree/Cargo.toml | 10 | ||||
-rw-r--r-- | vendor/tracing-tree/src/lib.rs | 54 | ||||
-rw-r--r-- | vendor/tracing-tree/src/time.rs | 119 | ||||
-rw-r--r-- | vendor/tracing-tree/tests/format_time.rs | 79 |
6 files changed, 340 insertions, 52 deletions
diff --git a/vendor/tracing-tree/.cargo-checksum.json b/vendor/tracing-tree/.cargo-checksum.json index 215b14ece..5c4c20385 100644 --- a/vendor/tracing-tree/.cargo-checksum.json +++ b/vendor/tracing-tree/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.lock":"a8a2a7ff05ca79c6ded4cf0625f6188d43625b8ba7358950eafa11b3af01dbda","Cargo.toml":"d883458574569e20468318abd1792c7312d9a31ba44dcc59203f6aa6b4bb3b01","LICENSE-APACHE":"c9c77d27bd737506eee1be90c11885fad935695714eafacd6725f41fff9ec8da","LICENSE-MIT":"d31139de68f7e19f9b32cd7de53dbb125b3c68522bbbb1d0c186c33112d34466","README.md":"589c2534836513b4edfa2811269767a9ac3c48da909c8ab936db3e646410dcf7","examples/basic.rs":"b56dad20aa283aae17a9c1364cc0326714cdc89d6a02a6329034e476b3597fd7","examples/basic.stdout":"49767873eb520f3e457b48d930fa86583d9ca5ef5571d67582b08dcb788ff6f2","examples/quiet.rs":"65a4cbb650cea4939ed274c2fd4d50c953151716fa6564b23393e1df6fb7afd7","examples/quiet.stdout":"d58a3240258279821b5d08e0d39ea60dee2dbd6a955f8639daf0d149ad0616fa","examples/stderr.rs":"ce0896f50be810908fb7083661bf39da6c9733ddef9188d40499313a40fe2c7a","examples/stderr.stderr":"8a0f6553fee903f159c6ea528dda6c5b193064ef9b58c765d1f3f60ecba5ac1f","examples/wraparound.rs":"7998f2e7b9e1f0dfc4b0cf11b1cdcf0f44c85aeaabbea9749113584ac7a3879f","examples/wraparound.stdout":"1c042231b3f8b20a843c18af063e43a51fcc852597dd1713f5878d4a8128271c","src/format.rs":"0dbfbe7a1939fefc3ade3ffa2113d382424f387130388fd3431210bbccef8b8b","src/lib.rs":"8a286a67ae3860d1c30cb336c1a6253920bf63b789e455a27d0e8b611228c1a6","tests/ui.rs":"32a02ef41ae5cbabe13164f8d665b287b0bb764b2dc5bcb80443cb551df5289a"},"package":"758e983ab7c54fee18403994507e7f212b9005e957ce7984996fac8d11facedb"}
\ No newline at end of file +{"files":{"Cargo.lock":"d584fa6e43dbd4c550f02b8644444902040f4ab6086507ac17bc98b0100af72b","Cargo.toml":"182256e2f9361a8277ee72b20146e33e2ecef5d1ed8f357e930bf9e56b1e60c3","LICENSE-APACHE":"c9c77d27bd737506eee1be90c11885fad935695714eafacd6725f41fff9ec8da","LICENSE-MIT":"d31139de68f7e19f9b32cd7de53dbb125b3c68522bbbb1d0c186c33112d34466","README.md":"589c2534836513b4edfa2811269767a9ac3c48da909c8ab936db3e646410dcf7","examples/basic.rs":"b56dad20aa283aae17a9c1364cc0326714cdc89d6a02a6329034e476b3597fd7","examples/basic.stdout":"49767873eb520f3e457b48d930fa86583d9ca5ef5571d67582b08dcb788ff6f2","examples/quiet.rs":"65a4cbb650cea4939ed274c2fd4d50c953151716fa6564b23393e1df6fb7afd7","examples/quiet.stdout":"d58a3240258279821b5d08e0d39ea60dee2dbd6a955f8639daf0d149ad0616fa","examples/stderr.rs":"ce0896f50be810908fb7083661bf39da6c9733ddef9188d40499313a40fe2c7a","examples/stderr.stderr":"8a0f6553fee903f159c6ea528dda6c5b193064ef9b58c765d1f3f60ecba5ac1f","examples/wraparound.rs":"7998f2e7b9e1f0dfc4b0cf11b1cdcf0f44c85aeaabbea9749113584ac7a3879f","examples/wraparound.stdout":"1c042231b3f8b20a843c18af063e43a51fcc852597dd1713f5878d4a8128271c","src/format.rs":"0dbfbe7a1939fefc3ade3ffa2113d382424f387130388fd3431210bbccef8b8b","src/lib.rs":"9c0ce624c037adec963e53deb011f32fa3d6ec5b2e764c91d574a3a451eeaf7f","src/time.rs":"af57c283b55cb84f1b4b3bdf1eb352fbcfd1d2492d65bec5bb5041bd6d12b1b0","tests/format_time.rs":"f119c668d01fca745acdf0fcc938ecc137c9c79b7fa72c668c006f509fef0db8","tests/ui.rs":"32a02ef41ae5cbabe13164f8d665b287b0bb764b2dc5bcb80443cb551df5289a"},"package":"4f9742d8df709837409dbb22aa25dd7769c260406f20ff48a2320b80a4a6aed0"}
\ No newline at end of file diff --git a/vendor/tracing-tree/Cargo.lock b/vendor/tracing-tree/Cargo.lock index 2d4ae7226..03f9a84d4 100644 --- a/vendor/tracing-tree/Cargo.lock +++ b/vendor/tracing-tree/Cargo.lock @@ -58,15 +58,15 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" [[package]] name = "either" -version = "1.6.1" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" [[package]] name = "glob" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "hermit-abi" @@ -79,14 +79,20 @@ dependencies = [ [[package]] name = "itertools" -version = "0.10.3" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -94,9 +100,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.126" +version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" [[package]] name = "log" @@ -124,10 +130,19 @@ dependencies = [ ] [[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + +[[package]] name = "once_cell" -version = "1.12.0" +version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "overload" @@ -143,9 +158,9 @@ checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" [[package]] name = "predicates" -version = "2.1.1" +version = "2.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c" +checksum = "59230a63c37f3e18569bdb90e4a89cbf5bf8b06fea0b84e65ea10cc4df47addd" dependencies = [ "difflib", "itertools", @@ -154,15 +169,15 @@ dependencies = [ [[package]] name = "predicates-core" -version = "1.0.3" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" [[package]] name = "predicates-tree" -version = "1.0.5" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" dependencies = [ "predicates-core", "termtree", @@ -170,18 +185,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.39" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.18" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -193,6 +208,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" [[package]] +name = "serde" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" + +[[package]] name = "sharded-slab" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -203,9 +224,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.95" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", @@ -214,24 +235,54 @@ dependencies = [ [[package]] name = "termtree" -version = "0.2.4" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "thread_local" -version = "1.1.4" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ + "cfg-if", "once_cell", ] [[package]] +name = "time" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +dependencies = [ + "itoa", + "libc", + "num_threads", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", +] + +[[package]] name = "tracing" -version = "0.1.34" +version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d0ecdcb44a79f0fe9844f0c4f33a342cbcbb5117de8001e6ba0dc2351327d09" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if", "pin-project-lite", @@ -241,9 +292,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.21" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc6b8ad3567499f98a1db7a752b07a7c8c7c7c34c332ec00effb2b0027974b7c" +checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" dependencies = [ "proc-macro2", "quote", @@ -252,11 +303,11 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.26" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f54c8ca710e81886d498c2fd3331b56c93aa248d49de2222ad2742247c60072f" +checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" dependencies = [ - "lazy_static", + "once_cell", "valuable", ] @@ -273,9 +324,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.11" +version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bc28f93baff38037f64e6f43d34cfa1605f27a49c34e8a04c5e78b0babf2596" +checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" dependencies = [ "sharded-slab", "thread_local", @@ -284,13 +335,14 @@ dependencies = [ [[package]] name = "tracing-tree" -version = "0.2.2" +version = "0.2.3" dependencies = [ "assert_cmd", "atty", "glob", "log", "nu-ansi-term", + "time", "tracing", "tracing-core", "tracing-log", @@ -299,9 +351,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.0" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" [[package]] name = "valuable" diff --git a/vendor/tracing-tree/Cargo.toml b/vendor/tracing-tree/Cargo.toml index 7e8799662..358b2d607 100644 --- a/vendor/tracing-tree/Cargo.toml +++ b/vendor/tracing-tree/Cargo.toml @@ -12,7 +12,7 @@ [package] edition = "2018" name = "tracing-tree" -version = "0.2.2" +version = "0.2.3" authors = [ "David Barsky <me@davidbarsky.com>", "Nathan Whitaker", @@ -32,6 +32,14 @@ version = "0.2" [dependencies.nu-ansi-term] version = "0.46.0" +[dependencies.time] +version = "0.3.20" +features = [ + "formatting", + "local-offset", +] +optional = true + [dependencies.tracing-core] version = "0.1" diff --git a/vendor/tracing-tree/src/lib.rs b/vendor/tracing-tree/src/lib.rs index 266523b73..ec3467493 100644 --- a/vendor/tracing-tree/src/lib.rs +++ b/vendor/tracing-tree/src/lib.rs @@ -1,6 +1,9 @@ pub(crate) mod format; +pub mod time; +use crate::time::FormatTime; use format::{Buffers, ColorLevel, Config, FmtEvent, SpanMode}; + use nu_ansi_term::{Color, Style}; use std::{ fmt::{self, Write as _}, @@ -43,13 +46,15 @@ impl Visit for Data { } } #[derive(Debug)] -pub struct HierarchicalLayer<W = fn() -> io::Stderr> +pub struct HierarchicalLayer<W = fn() -> io::Stderr, FT = ()> where W: for<'writer> MakeWriter<'writer> + 'static, + FT: FormatTime, { make_writer: W, bufs: Mutex<Buffers>, config: Config, + timer: FT, } impl Default for HierarchicalLayer { @@ -70,13 +75,15 @@ impl HierarchicalLayer<fn() -> io::Stderr> { make_writer: io::stderr, bufs: Mutex::new(Buffers::new()), config, + timer: (), } } } -impl<W> HierarchicalLayer<W> +impl<W, FT> HierarchicalLayer<W, FT> where W: for<'writer> MakeWriter<'writer> + 'static, + FT: FormatTime, { /// Enables terminal colors, boldness and italics. pub fn with_ansi(self, ansi: bool) -> Self { @@ -86,7 +93,7 @@ where } } - pub fn with_writer<W2>(self, make_writer: W2) -> HierarchicalLayer<W2> + pub fn with_writer<W2>(self, make_writer: W2) -> HierarchicalLayer<W2, FT> where W2: for<'writer> MakeWriter<'writer>, { @@ -94,6 +101,7 @@ where make_writer, config: self.config, bufs: self.bufs, + timer: self.timer, } } @@ -113,6 +121,16 @@ where } } + /// Specifies how to measure and format time at which event has occurred. + pub fn with_timer<FT2: FormatTime>(self, timer: FT2) -> HierarchicalLayer<W, FT2> { + HierarchicalLayer { + make_writer: self.make_writer, + config: self.config, + bufs: self.bufs, + timer, + } + } + /// Whether to render the event and span targets. Usually targets are the module path to the /// event/span macro invocation. pub fn with_targets(self, targets: bool) -> Self { @@ -274,10 +292,11 @@ where } } -impl<S, W> Layer<S> for HierarchicalLayer<W> +impl<S, W, FT> Layer<S> for HierarchicalLayer<W, FT> where S: Subscriber + for<'span> LookupSpan<'span>, W: for<'writer> MakeWriter<'writer> + 'static, + FT: FormatTime + 'static, { fn on_new_span(&self, attrs: &Attributes, id: &Id, ctx: Context<S>) { let span = ctx.span(id).expect("in new_span but span does not exist"); @@ -306,15 +325,26 @@ where let bufs = &mut *guard; let mut event_buf = &mut bufs.current_buf; + // Time. + + { + let prev_buffer_len = event_buf.len(); + + self.timer + .format_time(&mut event_buf) + .expect("Unable to write time to buffer"); + + // Something was written to the buffer, pad it with a space. + if prev_buffer_len < event_buf.len() { + write!(event_buf, " ").expect("Unable to write to buffer"); + } + } + // printing the indentation - let indent = if ctx.current_span().id().is_some() { - // size hint isn't implemented on Scope. - ctx.event_scope(event) - .expect("Unable to get span scope; this is a bug") - .count() - } else { - 0 - }; + let indent = ctx + .event_scope(event) + .map(|scope| scope.count()) + .unwrap_or(0); // check if this event occurred in the context of a span. // if it has, get the start time of this span. diff --git a/vendor/tracing-tree/src/time.rs b/vendor/tracing-tree/src/time.rs new file mode 100644 index 000000000..e0f81d47d --- /dev/null +++ b/vendor/tracing-tree/src/time.rs @@ -0,0 +1,119 @@ +/// A type that can measure and format the current time. +/// +/// This trait is used by [HierarchicalLayer] to include a timestamp with each +/// [Event] when it is logged. +/// +/// Notable default implementations of this trait are [LocalDateTime] and `()`. +/// The former prints the current time as reported by [time's OffsetDateTime] +/// (note that it requires a `time` feature to be enabled and may panic! +/// make sure to check out the docs for the [LocalDateTime]), +/// and the latter does not print the current time at all. +/// +/// Inspired by the [FormatTime] trait from [tracing-subscriber]. +/// +/// [HierarchicalLayer]: crate::HierarchicalLayer +/// [Event]: tracing_core::Event +/// [time's OffsetDateTime]: time::OffsetDateTime +/// [FormatTime]: tracing_subscriber::fmt::time::FormatTime +/// [tracing-subscriber]: tracing_subscriber +// NB: +// We can't use `tracing_subscriber::fmt::format::Writer` +// since it doesn't have a public constructor. +pub trait FormatTime { + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Default do-nothing time formatter. +impl FormatTime for () { + fn format_time(&self, _w: &mut impl std::fmt::Write) -> std::fmt::Result { + Ok(()) + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Retrieve and print the current wall-clock time in UTC timezone. +#[cfg(feature = "time")] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] +pub struct UtcDateTime; + +#[cfg(feature = "time")] +impl FormatTime for UtcDateTime { + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { + let time = time::OffsetDateTime::now_utc(); + write!(w, "{} {}", time.date(), time.time()) + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Retrieve and print the current wall-clock time. +/// +/// # Panics +/// +/// Panics if [time crate] cannot determine the local UTC offset. +/// +/// [time crate]: time +// NB: +// Can't use `tracing_subscriber::fmt::time::SystemTime` since it uses +// private `datetime` module to format the actual time. +#[cfg(feature = "time")] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] +pub struct LocalDateTime; + +#[cfg(feature = "time")] +impl FormatTime for LocalDateTime { + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { + let time = time::OffsetDateTime::now_local().expect("time offset cannot be determined"); + write!(w, "{}", time) + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Retrieve and print the relative elapsed wall-clock time since an epoch. +/// +/// The `Default` implementation for `Uptime` makes the epoch the current time. +// NB: Copy-pasted from `tracing-subscriber::fmt::time::Uptime`. +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub struct Uptime { + epoch: std::time::Instant, +} + +impl Default for Uptime { + fn default() -> Self { + Uptime { + epoch: std::time::Instant::now(), + } + } +} + +impl From<std::time::Instant> for Uptime { + fn from(epoch: std::time::Instant) -> Self { + Uptime { epoch } + } +} + +impl FormatTime for Uptime { + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { + let e = self.epoch.elapsed(); + write!(w, "{:4}.{:06}s", e.as_secs(), e.subsec_micros()) + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +impl<'a, F> FormatTime for &'a F +where + F: FormatTime, +{ + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { + (*self).format_time(w) + } +} + +// NB: +// Can't impl for `fn(&mut impl std::fmt::Write)` since impl trait is not allowed +// outside of function and inherent method return types for now. diff --git a/vendor/tracing-tree/tests/format_time.rs b/vendor/tracing-tree/tests/format_time.rs new file mode 100644 index 000000000..b24b16652 --- /dev/null +++ b/vendor/tracing-tree/tests/format_time.rs @@ -0,0 +1,79 @@ +use std::{ + fmt::Write, + sync::{ + atomic::{AtomicU64, Ordering}, + Arc, + }, +}; + +use tracing::{span, Level}; +use tracing_subscriber::{layer::SubscriberExt, Registry}; + +use tracing_tree::{time::FormatTime, HierarchicalLayer}; + +#[derive(Debug)] +struct FormatTimeCounter(Arc<AtomicU64>); + +impl FormatTime for FormatTimeCounter { + fn format_time(&self, _w: &mut impl Write) -> std::fmt::Result { + self.0.fetch_add(1, Ordering::Relaxed); + Ok(()) + } +} + +#[test] +fn format_time_num_calls() { + let num_called = Arc::new(AtomicU64::new(0)); + let format_time_counter = FormatTimeCounter(Arc::clone(&num_called)); + + let layer = HierarchicalLayer::default() + .with_writer(std::io::stdout) + .with_indent_lines(true) + .with_indent_amount(2) + .with_timer(format_time_counter) + .with_thread_names(true) + .with_thread_ids(true) + .with_verbose_exit(true) + .with_verbose_entry(true) + .with_targets(true); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + let test_span = span!(Level::TRACE, "format-time-num-calls-test", version = %0.1); + let _e = test_span.enter(); + + tracing::info!("first event"); + assert_eq!(num_called.load(Ordering::Relaxed), 1); + + std::thread::sleep(std::time::Duration::from_millis(100)); + tracing::info!("second event"); + assert_eq!(num_called.load(Ordering::Relaxed), 2); + + let nested_span = span!(Level::TRACE, "nested-span"); + nested_span.in_scope(|| { + tracing::debug!("nested event"); + assert_eq!(num_called.load(Ordering::Relaxed), 3); + + tracing::info!("important nested event"); + assert_eq!(num_called.load(Ordering::Relaxed), 4); + }); + drop(nested_span); + + instrumented_function(); + assert_eq!(num_called.load(Ordering::Relaxed), 6); + + tracing::info!("exiting"); + assert_eq!(num_called.load(Ordering::Relaxed), 7); +} + +#[tracing::instrument] +fn instrumented_function() { + tracing::info!("instrumented function"); + nested_instrumented_function(); +} + +#[tracing::instrument] +fn nested_instrumented_function() { + tracing::warn!("nested instrumented function"); +} |