Skip to content

Commit

Permalink
#1 add timeFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
susatthi committed May 1, 2022
1 parent b437672 commit d0d192e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
14 changes: 9 additions & 5 deletions lib/src/printers/single_pretty_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SinglePrettyPrinter extends LogPrinter {
Map<Level, AnsiColor>? levelColors,
this.levelEmojis = defaultLevelEmojis,
this.levelLabels = defaultLevelLabels,
this.timeFormatter = formatTime,
}) : _levelColors = levelColors ?? defaultLevelColors;

/// If specified, it will be output at the beginning of the log.
Expand Down Expand Up @@ -62,6 +63,9 @@ class SinglePrettyPrinter extends LogPrinter {
/// String for each log level.
final Map<Level, String> levelLabels;

/// Formats the current time.
final TimeFormatter timeFormatter;

/// Path to this file.
static final selfPath = _getSelfPath();

Expand Down Expand Up @@ -206,9 +210,7 @@ class SinglePrettyPrinter extends LogPrinter {
}

@visibleForTesting
String getCurrentTime({
DateTime? dateTime,
}) {
static String formatTime(DateTime now) {
String _threeDigits(int n) {
if (n >= 100) {
return '$n';
Expand All @@ -226,7 +228,6 @@ class SinglePrettyPrinter extends LogPrinter {
return '0$n';
}

final now = dateTime ?? DateTime.now();
final h = _twoDigits(now.hour);
final min = _twoDigits(now.minute);
final sec = _twoDigits(now.second);
Expand Down Expand Up @@ -290,7 +291,7 @@ class SinglePrettyPrinter extends LogPrinter {
buffer.add(levelLabels[level]!);
}
if (printTime) {
buffer.add(getCurrentTime());
buffer.add(timeFormatter(DateTime.now()));
}
if (printCaller) {
final caller = getCaller();
Expand All @@ -301,3 +302,6 @@ class SinglePrettyPrinter extends LogPrinter {
return buffer.isNotEmpty ? '${buffer.join(' ')}: ' : '';
}
}

/// Function to format the current time
typedef TimeFormatter = String Function(DateTime now);
25 changes: 19 additions & 6 deletions test/printers/single_pretty_printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,22 @@ void main() {
);
});
});
group('timeFormatter', () {
test('timeFormatter is specified', () {
const expectedText = 'expectedText';
final printer = SinglePrettyPrinter(
timeFormatter: (now) {
return expectedText;
},
);
_wrapPropertyTest(printer, expectedText, true);
});
test('timeFormatter is not specified', () {
const expectedText = 'expectedText';
final printer = SinglePrettyPrinter();
_wrapPropertyTest(printer, expectedText, false);
});
});
group('any properties is specified', () {
test('very simple', () {
final printer = SinglePrettyPrinter(
Expand Down Expand Up @@ -551,8 +567,8 @@ packages/flutter_sample_custom_logger/main.dart 5:10 main
);
});
});
group('getCurrentTime()', () {
test('getCurrentTime()', () {
group('formatTime()', () {
test('formatTime()', () {
_wrapCurrentTimeTest(
DateTime(2022, 1, 1, 0, 0, 0, 0),
'00:00:00.000',
Expand Down Expand Up @@ -614,9 +630,6 @@ void _wrapCurrentTimeTest(
DateTime dateTime,
String? expectTime,
) {
final printer = SinglePrettyPrinter();
final actualTime = printer.getCurrentTime(
dateTime: dateTime,
);
final actualTime = SinglePrettyPrinter.formatTime(dateTime);
expect(actualTime, expectTime);
}

0 comments on commit d0d192e

Please sign in to comment.