@@ -2,6 +2,7 @@ import 'dart:convert';
22import 'dart:math' ;
33
44import '../ansi_color.dart' ;
5+ import '../date_time_format.dart' ;
56import '../log_event.dart' ;
67import '../log_level.dart' ;
78import '../log_printer.dart' ;
@@ -64,7 +65,7 @@ class PrettyPrinter extends LogPrinter {
6465 static final _browserStackTraceRegex =
6566 RegExp (r'^(?:package:)?(dart:\S+|\S+)' );
6667
67- static DateTime ? _startTime ;
68+ static DateTime ? startTime ;
6869
6970 /// The index at which the stack trace should start.
7071 ///
@@ -113,7 +114,11 @@ class PrettyPrinter extends LogPrinter {
113114 final bool printEmojis;
114115
115116 /// 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;
117122
118123 /// Controls the ascii 'boxing' of different [Level] s.
119124 ///
@@ -191,14 +196,25 @@ class PrettyPrinter extends LogPrinter {
191196 this .lineLength = 120 ,
192197 this .colors = true ,
193198 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,
195203 this .excludeBox = const {},
196204 this .noBoxingByDefault = false ,
197205 this .excludePaths = const [],
198206 this .levelColors,
199207 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 ();
202218
203219 var doubleDividerLine = StringBuffer ();
204220 var singleDividerLine = StringBuffer ();
@@ -241,6 +257,8 @@ class PrettyPrinter extends LogPrinter {
241257 var errorStr = event.error? .toString ();
242258
243259 String ? timeStr;
260+ // Keep backwards-compatibility to `printTime` check
261+ // ignore: deprecated_member_use_from_same_package
244262 if (printTime) {
245263 timeStr = getTime (event.time);
246264 }
@@ -332,24 +350,7 @@ class PrettyPrinter extends LogPrinter {
332350 }
333351
334352 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);
353354 }
354355
355356 // Handles any object that is causing JsonEncoder() problems
0 commit comments