@@ -2,6 +2,7 @@ import 'dart:convert';
2
2
import 'dart:math' ;
3
3
4
4
import '../ansi_color.dart' ;
5
+ import '../date_time_format.dart' ;
5
6
import '../log_event.dart' ;
6
7
import '../log_level.dart' ;
7
8
import '../log_printer.dart' ;
@@ -64,7 +65,7 @@ class PrettyPrinter extends LogPrinter {
64
65
static final _browserStackTraceRegex =
65
66
RegExp (r'^(?:package:)?(dart:\S+|\S+)' );
66
67
67
- static DateTime ? _startTime ;
68
+ static DateTime ? startTime ;
68
69
69
70
/// The index at which the stack trace should start.
70
71
///
@@ -113,7 +114,11 @@ class PrettyPrinter extends LogPrinter {
113
114
final bool printEmojis;
114
115
115
116
/// Whether [LogEvent.time] is printed.
116
- final bool printTime;
117
+ @Deprecated ("Use `dateTimeFormat` instead." )
118
+ bool get printTime => dateTimeFormat != DateTimeFormat .none;
119
+
120
+ /// Controls the format of [LogEvent.time] .
121
+ final DateTimeFormatter dateTimeFormat;
117
122
118
123
/// Controls the ascii 'boxing' of different [Level] s.
119
124
///
@@ -191,14 +196,25 @@ class PrettyPrinter extends LogPrinter {
191
196
this .lineLength = 120 ,
192
197
this .colors = true ,
193
198
this .printEmojis = true ,
194
- this .printTime = false ,
199
+ @Deprecated (
200
+ "Use `dateTimeFormat` with `DateTimeFormat.onlyTimeAndSinceStart` or `DateTimeFormat.none` instead." )
201
+ bool ? printTime,
202
+ DateTimeFormatter dateTimeFormat = DateTimeFormat .none,
195
203
this .excludeBox = const {},
196
204
this .noBoxingByDefault = false ,
197
205
this .excludePaths = const [],
198
206
this .levelColors,
199
207
this .levelEmojis,
200
- }) {
201
- _startTime ?? = DateTime .now ();
208
+ }) : assert (
209
+ (printTime != null && dateTimeFormat == DateTimeFormat .none) ||
210
+ printTime == null ,
211
+ "Don't set printTime when using dateTimeFormat" ),
212
+ dateTimeFormat = printTime == null
213
+ ? dateTimeFormat
214
+ : (printTime
215
+ ? DateTimeFormat .onlyTimeAndSinceStart
216
+ : DateTimeFormat .none) {
217
+ startTime ?? = DateTime .now ();
202
218
203
219
var doubleDividerLine = StringBuffer ();
204
220
var singleDividerLine = StringBuffer ();
@@ -241,6 +257,8 @@ class PrettyPrinter extends LogPrinter {
241
257
var errorStr = event.error? .toString ();
242
258
243
259
String ? timeStr;
260
+ // Keep backwards-compatibility to `printTime` check
261
+ // ignore: deprecated_member_use_from_same_package
244
262
if (printTime) {
245
263
timeStr = getTime (event.time);
246
264
}
@@ -332,24 +350,7 @@ class PrettyPrinter extends LogPrinter {
332
350
}
333
351
334
352
String getTime (DateTime time) {
335
- String threeDigits (int n) {
336
- if (n >= 100 ) return '$n ' ;
337
- if (n >= 10 ) return '0$n ' ;
338
- return '00$n ' ;
339
- }
340
-
341
- String twoDigits (int n) {
342
- if (n >= 10 ) return '$n ' ;
343
- return '0$n ' ;
344
- }
345
-
346
- var now = time;
347
- var h = twoDigits (now.hour);
348
- var min = twoDigits (now.minute);
349
- var sec = twoDigits (now.second);
350
- var ms = threeDigits (now.millisecond);
351
- var timeSinceStart = now.difference (_startTime! ).toString ();
352
- return '$h :$min :$sec .$ms (+$timeSinceStart )' ;
353
+ return dateTimeFormat (time);
353
354
}
354
355
355
356
// Handles any object that is causing JsonEncoder() problems
0 commit comments