|
1 | 1 | #[cfg(feature = "ansi")]
|
2 |
| -use crate::nu_ansi_term::{Color, Style}; |
3 |
| -use std::{fmt, io}; |
4 |
| -use time::{format_description::FormatItem, formatting::Formattable, OffsetDateTime}; |
| 2 | +use nu_ansi_term::{Color, Style}; |
| 3 | +use std::fmt; |
5 | 4 | use tracing::{Level, Metadata};
|
6 | 5 | use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
|
7 | 6 |
|
8 |
| -/// A bridge between `fmt::Write` and `io::Write`. |
9 |
| -/// |
10 |
| -/// This is used by the timestamp formatting implementation for the `time` |
11 |
| -/// crate and by the JSON formatter. In both cases, this is needed because |
12 |
| -/// `tracing-subscriber`'s `FormatEvent`/`FormatTime` traits expect a |
13 |
| -/// `fmt::Write` implementation, while `serde_json::Serializer` and `time`'s |
14 |
| -/// `format_into` methods expect an `io::Write`. |
15 |
| -pub(crate) struct WriteAdaptor<'a> { |
16 |
| - fmt_write: &'a mut dyn fmt::Write, |
17 |
| -} |
18 |
| - |
19 |
| -impl<'a> WriteAdaptor<'a> { |
20 |
| - pub(crate) fn new(fmt_write: &'a mut dyn fmt::Write) -> Self { |
21 |
| - Self { fmt_write } |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -impl<'a> io::Write for WriteAdaptor<'a> { |
26 |
| - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
27 |
| - let s = |
28 |
| - std::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
29 |
| - |
30 |
| - self.fmt_write |
31 |
| - .write_str(s) |
32 |
| - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; |
33 |
| - |
34 |
| - Ok(s.as_bytes().len()) |
35 |
| - } |
36 |
| - |
37 |
| - fn flush(&mut self) -> io::Result<()> { |
38 |
| - Ok(()) |
39 |
| - } |
40 |
| -} |
41 |
| - |
42 |
| -impl<'a> fmt::Debug for WriteAdaptor<'a> { |
43 |
| - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
44 |
| - f.pad("WriteAdaptor { .. }") |
45 |
| - } |
46 |
| -} |
| 7 | +use tracing_subscriber::fmt::time::{ChronoLocal, ChronoUtc}; |
47 | 8 |
|
48 | 9 | pub(crate) struct FmtLevel {
|
49 | 10 | pub level: Level,
|
@@ -91,113 +52,75 @@ impl fmt::Display for FmtLevel {
|
91 | 52 | }
|
92 | 53 | }
|
93 | 54 |
|
94 |
| -/// Formats the current [UTC time] using a [formatter] from the [`time` crate]. |
| 55 | +/// Formats the current [UTC time] using [`chrono` crate]. |
95 | 56 | ///
|
96 |
| -/// To format the current [local time] instead, use the [`LocalTime`] type. |
| 57 | +/// To format the current local time instead, use the [`LocalTime`] |
| 58 | +/// or the [`LocalTime`] type. |
97 | 59 | ///
|
98 |
| -/// [UTC time]: time::OffsetDateTime::now_utc |
99 |
| -/// [formatter]: time::formatting::Formattable |
100 |
| -/// [`time` crate]: time |
101 |
| -/// [local time]: time::OffsetDateTime::now_local |
| 60 | +/// [UTC time]: ChronoUtc |
| 61 | +/// [`chrono` crate]: chrono |
102 | 62 | #[derive(Clone, Debug)]
|
103 |
| -pub struct UtcTime<F = Vec<FormatItem<'static>>> { |
104 |
| - format: F, |
| 63 | +pub struct UtcTime { |
| 64 | + time: ChronoUtc, |
105 | 65 | }
|
106 | 66 |
|
107 |
| -impl<F> FormatTime for UtcTime<F> |
108 |
| -where |
109 |
| - F: Formattable, |
110 |
| -{ |
111 |
| - fn format_time(&self, writer: &mut Writer<'_>) -> fmt::Result { |
112 |
| - let now = OffsetDateTime::now_utc(); |
113 |
| - |
| 67 | +impl FormatTime for UtcTime { |
| 68 | + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { |
114 | 69 | #[cfg(feature = "ansi")]
|
115 |
| - if writer.has_ansi_escapes() { |
| 70 | + if w.has_ansi_escapes() { |
116 | 71 | let style = Style::new().dimmed();
|
117 |
| - write!(writer, "{}", style.prefix())?; |
118 |
| - format_datetime(writer, now, &self.format)?; |
119 |
| - write!(writer, "{}", style.suffix())?; |
| 72 | + write!(w, "{}", style.prefix())?; |
| 73 | + self.time.format_time(w)?; |
| 74 | + write!(w, "{}", style.suffix())?; |
120 | 75 | return Ok(());
|
121 | 76 | }
|
122 | 77 |
|
123 |
| - format_datetime(writer, now, &self.format) |
| 78 | + self.time.format_time(w) |
124 | 79 | }
|
125 | 80 | }
|
126 | 81 |
|
127 | 82 | impl Default for UtcTime {
|
128 | 83 | fn default() -> Self {
|
129 |
| - let format: Vec<FormatItem> = time::format_description::parse( |
130 |
| - "[month][day] [hour]:[minute]:[second].[subsecond digits:6]", |
131 |
| - ) |
132 |
| - .expect("Unable to make time formatter"); |
133 |
| - Self { format } |
| 84 | + let fmt_string = String::from("%m%d %H:%M:%S%.6f"); |
| 85 | + Self { |
| 86 | + time: ChronoUtc::new(fmt_string), |
| 87 | + } |
134 | 88 | }
|
135 | 89 | }
|
136 | 90 |
|
137 |
| -/// Formats the current [local time] using a [formatter] from the [`time` crate]. |
138 |
| -/// |
139 |
| -/// To format the current [UTC time] instead, use the [`UtcTime`] type. |
140 |
| -/// |
141 |
| -/// <div class="example-wrap" style="display:inline-block"> |
142 |
| -/// <pre class="compile_fail" style="white-space:normal;font:inherit;"> |
143 |
| -/// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"><code>time</code> |
144 |
| -/// crate</a> must be compiled with <code>--cfg unsound_local_offset</code> in order to use |
145 |
| -/// local timestamps. When this cfg is not enabled, local timestamps cannot be recorded, and |
146 |
| -/// events will be logged without timestamps. |
| 91 | +/// Formats the current [`local time`] using [`chrono` crate]. |
147 | 92 | ///
|
148 |
| -/// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"><code>time</code> |
149 |
| -/// documentation</a> for more details. |
150 |
| -/// </pre></div> |
| 93 | +/// To format the UTC time instead, use the [`UtcTime`] |
| 94 | +/// or the [`crate::time_crate::UtcTime`] type. |
151 | 95 | ///
|
152 |
| -/// [local time]: time::OffsetDateTime::now_local |
153 |
| -/// [formatter]: time::formatting::Formattable |
154 |
| -/// [`time` crate]: time |
155 |
| -/// [UTC time]: time::OffsetDateTime::now_utc |
156 |
| -#[derive(Clone, Debug)] |
157 |
| -pub struct LocalTime<F = Vec<FormatItem<'static>>> { |
158 |
| - format: F, |
| 96 | +/// [`local time`]: tracing_subscriber::fmt::time::ChronoLocal |
| 97 | +/// [`chrono` crate]: chrono |
| 98 | +pub struct LocalTime { |
| 99 | + time: ChronoLocal, |
159 | 100 | }
|
160 | 101 |
|
161 |
| -impl Default for LocalTime { |
162 |
| - fn default() -> Self { |
163 |
| - let format: Vec<FormatItem> = time::format_description::parse( |
164 |
| - "[month][day] [hour]:[minute]:[second].[subsecond digits:6]", |
165 |
| - ) |
166 |
| - .expect("Unable to make time formatter"); |
167 |
| - Self { format } |
168 |
| - } |
169 |
| -} |
170 |
| - |
171 |
| -impl<F> FormatTime for LocalTime<F> |
172 |
| -where |
173 |
| - F: Formattable, |
174 |
| -{ |
175 |
| - fn format_time(&self, writer: &mut Writer<'_>) -> fmt::Result { |
176 |
| - let now = OffsetDateTime::now_local().map_err(|_| fmt::Error)?; |
177 |
| - |
| 102 | +impl FormatTime for LocalTime { |
| 103 | + fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { |
178 | 104 | #[cfg(feature = "ansi")]
|
179 |
| - if writer.has_ansi_escapes() { |
| 105 | + if w.has_ansi_escapes() { |
180 | 106 | let style = Style::new().dimmed();
|
181 |
| - write!(writer, "{}", style.prefix())?; |
182 |
| - format_datetime(writer, now, &self.format)?; |
183 |
| - // necessary to provide space between the time and the PID |
184 |
| - write!(writer, "{}", style.suffix())?; |
| 107 | + write!(w, "{}", style.prefix())?; |
| 108 | + self.time.format_time(w)?; |
| 109 | + write!(w, "{}", style.suffix())?; |
185 | 110 | return Ok(());
|
186 | 111 | }
|
187 | 112 |
|
188 |
| - format_datetime(writer, now, &self.format) |
| 113 | + self.time.format_time(w) |
189 | 114 | }
|
190 | 115 | }
|
191 | 116 |
|
192 |
| -fn format_datetime( |
193 |
| - into: &mut Writer<'_>, |
194 |
| - now: OffsetDateTime, |
195 |
| - fmt: &impl Formattable, |
196 |
| -) -> fmt::Result { |
197 |
| - let mut into = WriteAdaptor::new(into); |
198 |
| - now.format_into(&mut into, fmt) |
199 |
| - .map_err(|_| fmt::Error) |
200 |
| - .map(|_| ()) |
| 117 | +impl Default for LocalTime { |
| 118 | + fn default() -> Self { |
| 119 | + let fmt_string = String::from("%m%d %H:%M:%S%.6f"); |
| 120 | + Self { |
| 121 | + time: ChronoLocal::new(fmt_string), |
| 122 | + } |
| 123 | + } |
201 | 124 | }
|
202 | 125 |
|
203 | 126 | pub(crate) struct FormatProcessData<'a> {
|
|
0 commit comments