@@ -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,22 @@ 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
{
280
+ // Trick the compiler to think we have used self.timestamp
281
+ // Workaround for "field is never used: `timestamp`" compiler nag.
268
282
let _ = self . timestamp ;
269
- let _ = self . timestamp_nanos ;
270
283
Ok ( ( ) )
271
284
}
272
285
}
@@ -294,7 +307,7 @@ impl<'a> DefaultFormat<'a> {
294
307
295
308
fn write_args ( & mut self , record : & Record ) -> io:: Result < ( ) > {
296
309
match self . indent {
297
-
310
+
298
311
// Fast path for no indentation
299
312
None => writeln ! ( self . buf, "{}" , record. args( ) ) ,
300
313
@@ -376,8 +389,7 @@ mod tests {
376
389
let mut f = Formatter :: new ( & writer) ;
377
390
378
391
let written = write ( DefaultFormat {
379
- timestamp : false ,
380
- timestamp_nanos : false ,
392
+ timestamp : None ,
381
393
module_path : true ,
382
394
level : true ,
383
395
written_header_value : false ,
@@ -397,8 +409,7 @@ mod tests {
397
409
let mut f = Formatter :: new ( & writer) ;
398
410
399
411
let written = write ( DefaultFormat {
400
- timestamp : false ,
401
- timestamp_nanos : false ,
412
+ timestamp : None ,
402
413
module_path : false ,
403
414
level : false ,
404
415
written_header_value : false ,
@@ -418,8 +429,7 @@ mod tests {
418
429
let mut f = Formatter :: new ( & writer) ;
419
430
420
431
let written = write ( DefaultFormat {
421
- timestamp : false ,
422
- timestamp_nanos : false ,
432
+ timestamp : None ,
423
433
module_path : true ,
424
434
level : true ,
425
435
written_header_value : false ,
@@ -439,8 +449,7 @@ mod tests {
439
449
let mut f = Formatter :: new ( & writer) ;
440
450
441
451
let written = write ( DefaultFormat {
442
- timestamp : false ,
443
- timestamp_nanos : false ,
452
+ timestamp : None ,
444
453
module_path : true ,
445
454
level : true ,
446
455
written_header_value : false ,
@@ -460,8 +469,7 @@ mod tests {
460
469
let mut f = Formatter :: new ( & writer) ;
461
470
462
471
let written = write ( DefaultFormat {
463
- timestamp : false ,
464
- timestamp_nanos : false ,
472
+ timestamp : None ,
465
473
module_path : false ,
466
474
level : false ,
467
475
written_header_value : false ,
0 commit comments