/build/cargo-vendor-dir/env_logger-0.10.2/src/fmt/humantime.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::fmt; |
2 | | use std::time::SystemTime; |
3 | | |
4 | | use humantime::{ |
5 | | format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds, |
6 | | }; |
7 | | |
8 | | use crate::fmt::{Formatter, TimestampPrecision}; |
9 | | |
10 | | impl Formatter { |
11 | | /// Get a [`Timestamp`] for the current date and time in UTC. |
12 | | /// |
13 | | /// # Examples |
14 | | /// |
15 | | /// Include the current timestamp with the log record: |
16 | | /// |
17 | | /// ``` |
18 | | /// use std::io::Write; |
19 | | /// |
20 | | /// let mut builder = env_logger::Builder::new(); |
21 | | /// |
22 | | /// builder.format(|buf, record| { |
23 | | /// let ts = buf.timestamp(); |
24 | | /// |
25 | | /// writeln!(buf, "{}: {}: {}", ts, record.level(), record.args()) |
26 | | /// }); |
27 | | /// ``` |
28 | | /// |
29 | | /// [`Timestamp`]: struct.Timestamp.html |
30 | 0 | pub fn timestamp(&self) -> Timestamp { |
31 | 0 | Timestamp { |
32 | 0 | time: SystemTime::now(), |
33 | 0 | precision: TimestampPrecision::Seconds, |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | | /// Get a [`Timestamp`] for the current date and time in UTC with full |
38 | | /// second precision. |
39 | 0 | pub fn timestamp_seconds(&self) -> Timestamp { |
40 | 0 | Timestamp { |
41 | 0 | time: SystemTime::now(), |
42 | 0 | precision: TimestampPrecision::Seconds, |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | /// Get a [`Timestamp`] for the current date and time in UTC with |
47 | | /// millisecond precision. |
48 | 0 | pub fn timestamp_millis(&self) -> Timestamp { |
49 | 0 | Timestamp { |
50 | 0 | time: SystemTime::now(), |
51 | 0 | precision: TimestampPrecision::Millis, |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | | /// Get a [`Timestamp`] for the current date and time in UTC with |
56 | | /// microsecond precision. |
57 | 0 | pub fn timestamp_micros(&self) -> Timestamp { |
58 | 0 | Timestamp { |
59 | 0 | time: SystemTime::now(), |
60 | 0 | precision: TimestampPrecision::Micros, |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | /// Get a [`Timestamp`] for the current date and time in UTC with |
65 | | /// nanosecond precision. |
66 | 0 | pub fn timestamp_nanos(&self) -> Timestamp { |
67 | 0 | Timestamp { |
68 | 0 | time: SystemTime::now(), |
69 | 0 | precision: TimestampPrecision::Nanos, |
70 | 0 | } |
71 | 0 | } |
72 | | } |
73 | | |
74 | | /// An [RFC3339] formatted timestamp. |
75 | | /// |
76 | | /// The timestamp implements [`Display`] and can be written to a [`Formatter`]. |
77 | | /// |
78 | | /// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt |
79 | | /// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html |
80 | | /// [`Formatter`]: struct.Formatter.html |
81 | | pub struct Timestamp { |
82 | | time: SystemTime, |
83 | | precision: TimestampPrecision, |
84 | | } |
85 | | |
86 | | impl fmt::Debug for Timestamp { |
87 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
88 | 0 | /// A `Debug` wrapper for `Timestamp` that uses the `Display` implementation. |
89 | 0 | struct TimestampValue<'a>(&'a Timestamp); |
90 | 0 |
|
91 | 0 | impl<'a> fmt::Debug for TimestampValue<'a> { |
92 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
93 | 0 | fmt::Display::fmt(&self.0, f) |
94 | 0 | } |
95 | 0 | } |
96 | 0 |
|
97 | 0 | f.debug_tuple("Timestamp") |
98 | 0 | .field(&TimestampValue(self)) |
99 | 0 | .finish() |
100 | 0 | } |
101 | | } |
102 | | |
103 | | impl fmt::Display for Timestamp { |
104 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
105 | 0 | let formatter = match self.precision { |
106 | 0 | TimestampPrecision::Seconds => format_rfc3339_seconds, |
107 | 0 | TimestampPrecision::Millis => format_rfc3339_millis, |
108 | 0 | TimestampPrecision::Micros => format_rfc3339_micros, |
109 | 0 | TimestampPrecision::Nanos => format_rfc3339_nanos, |
110 | | }; |
111 | | |
112 | 0 | formatter(self.time).fmt(f) |
113 | 0 | } |
114 | | } |