@@ -112,9 +112,25 @@ impl fmt::Debug for Formatter {
112
112
}
113
113
}
114
114
115
+ /// Formatting precision of timestamps.
116
+ ///
117
+ /// Seconds give precision of full seconds, milliseconds give thousands of a
118
+ /// second (3 decimal digits), microseconds are millionth of a second (6 decimal
119
+ /// digits) and nanoseconds are billionth of a second (9 decimal digits).
120
+ #[ derive( Copy , Clone , Debug ) ]
121
+ pub enum TimestampPrecision {
122
+ /// Full second precision (0 decimal digits)
123
+ Seconds ,
124
+ /// Millisecond precision (3 decimal digits)
125
+ Millis ,
126
+ /// Microsecond precision (6 decimal digits)
127
+ Micros ,
128
+ /// Nanosecond precision (9 decimal digits)
129
+ Nanos ,
130
+ }
131
+
115
132
pub ( crate ) struct Builder {
116
- pub default_format_timestamp : bool ,
117
- pub default_format_timestamp_nanos : bool ,
133
+ pub default_format_timestamp : Option < TimestampPrecision > ,
118
134
pub default_format_module_path : bool ,
119
135
pub default_format_level : bool ,
120
136
pub default_format_indent : Option < usize > ,
@@ -126,8 +142,7 @@ pub(crate) struct Builder {
126
142
impl Default for Builder {
127
143
fn default ( ) -> Self {
128
144
Builder {
129
- default_format_timestamp : true ,
130
- default_format_timestamp_nanos : false ,
145
+ default_format_timestamp : Some ( TimestampPrecision :: Seconds ) ,
131
146
default_format_module_path : true ,
132
147
default_format_level : true ,
133
148
default_format_indent : Some ( 4 ) ,
@@ -139,7 +154,7 @@ impl Default for Builder {
139
154
140
155
impl Builder {
141
156
/// Convert the format into a callable function.
142
- ///
157
+ ///
143
158
/// If the `custom_format` is `Some`, then any `default_format` switches are ignored.
144
159
/// If the `custom_format` is `None`, then a default format is returned.
145
160
/// Any `default_format` switches set to `false` won't be written by the format.
@@ -159,7 +174,6 @@ impl Builder {
159
174
Box :: new ( move |buf, record| {
160
175
let fmt = DefaultFormat {
161
176
timestamp : built. default_format_timestamp ,
162
- timestamp_nanos : built. default_format_timestamp_nanos ,
163
177
module_path : built. default_format_module_path ,
164
178
level : built. default_format_level ,
165
179
written_header_value : false ,
@@ -179,13 +193,12 @@ type SubtleStyle = StyledValue<'static, &'static str>;
179
193
type SubtleStyle = & ' static str ;
180
194
181
195
/// The default format.
182
- ///
196
+ ///
183
197
/// This format needs to work with any combination of crate features.
184
198
struct DefaultFormat < ' a > {
185
- timestamp : bool ,
199
+ timestamp : Option < TimestampPrecision > ,
186
200
module_path : bool ,
187
201
level : bool ,
188
- timestamp_nanos : bool ,
189
202
written_header_value : bool ,
190
203
indent : Option < usize > ,
191
204
buf : & ' a mut Formatter ,
@@ -251,22 +264,19 @@ impl<'a> DefaultFormat<'a> {
251
264
fn write_timestamp ( & mut self ) -> io:: Result < ( ) > {
252
265
#[ cfg( feature = "humantime" ) ]
253
266
{
254
- if !self . timestamp {
255
- return Ok ( ( ) )
256
- }
257
-
258
- if self . timestamp_nanos {
259
- let ts_nanos = self . buf . precise_timestamp ( ) ;
260
- self . write_header_value ( ts_nanos)
261
- } else {
262
- let ts = self . buf . timestamp ( ) ;
263
- self . write_header_value ( ts)
264
- }
267
+ use fmt:: TimestampPrecision :: * ;
268
+ let ts = match self . timestamp {
269
+ None => return Ok ( ( ) ) ,
270
+ Some ( Seconds ) => self . buf . timestamp_seconds ( ) ,
271
+ Some ( Millis ) => self . buf . timestamp_millis ( ) ,
272
+ Some ( Micros ) => self . buf . timestamp_micros ( ) ,
273
+ Some ( Nanos ) => self . buf . timestamp_nanos ( ) ,
274
+ } ;
275
+
276
+ self . write_header_value ( ts)
265
277
}
266
278
#[ cfg( not( feature = "humantime" ) ) ]
267
279
{
268
- let _ = self . timestamp ;
269
- let _ = self . timestamp_nanos ;
270
280
Ok ( ( ) )
271
281
}
272
282
}
@@ -294,7 +304,7 @@ impl<'a> DefaultFormat<'a> {
294
304
295
305
fn write_args ( & mut self , record : & Record ) -> io:: Result < ( ) > {
296
306
match self . indent {
297
-
307
+
298
308
// Fast path for no indentation
299
309
None => writeln ! ( self . buf, "{}" , record. args( ) ) ,
300
310
@@ -376,8 +386,7 @@ mod tests {
376
386
let mut f = Formatter :: new ( & writer) ;
377
387
378
388
let written = write ( DefaultFormat {
379
- timestamp : false ,
380
- timestamp_nanos : false ,
389
+ timestamp : None ,
381
390
module_path : true ,
382
391
level : true ,
383
392
written_header_value : false ,
@@ -397,8 +406,7 @@ mod tests {
397
406
let mut f = Formatter :: new ( & writer) ;
398
407
399
408
let written = write ( DefaultFormat {
400
- timestamp : false ,
401
- timestamp_nanos : false ,
409
+ timestamp : None ,
402
410
module_path : false ,
403
411
level : false ,
404
412
written_header_value : false ,
@@ -418,8 +426,7 @@ mod tests {
418
426
let mut f = Formatter :: new ( & writer) ;
419
427
420
428
let written = write ( DefaultFormat {
421
- timestamp : false ,
422
- timestamp_nanos : false ,
429
+ timestamp : None ,
423
430
module_path : true ,
424
431
level : true ,
425
432
written_header_value : false ,
@@ -439,8 +446,7 @@ mod tests {
439
446
let mut f = Formatter :: new ( & writer) ;
440
447
441
448
let written = write ( DefaultFormat {
442
- timestamp : false ,
443
- timestamp_nanos : false ,
449
+ timestamp : None ,
444
450
module_path : true ,
445
451
level : true ,
446
452
written_header_value : false ,
@@ -460,8 +466,7 @@ mod tests {
460
466
let mut f = Formatter :: new ( & writer) ;
461
467
462
468
let written = write ( DefaultFormat {
463
- timestamp : false ,
464
- timestamp_nanos : false ,
469
+ timestamp : None ,
465
470
module_path : false ,
466
471
level : false ,
467
472
written_header_value : false ,
0 commit comments