Skip to content

Commit

Permalink
Added DateTimeFormatter to PrettyPrinter, closes #80
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Jul 15, 2024
1 parent 4d8d595 commit 901b07b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ var logger = Logger(
lineLength: 120, // Width of the output
colors: true, // Colorful log messages
printEmojis: true, // Print an emoji for each log message
printTime: false // Should each log print contain a timestamp
// Should each log print contain a timestamp
dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
),
);
```
Expand Down
70 changes: 70 additions & 0 deletions lib/src/date_time_format.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'printers/pretty_printer.dart';

typedef DateTimeFormatter = String Function(DateTime time);

class DateTimeFormat {
/// Omits the date and time completely.
static const DateTimeFormatter none = _none;

/// Prints only the time.
///
/// Example:
/// * `12:30:40.550`
static const DateTimeFormatter onlyTime = _onlyTime;

/// Prints only the time including the difference since [PrettyPrinter.startTime].
///
/// Example:
/// * `12:30:40.550 (+0:00:00.060700)`
static const DateTimeFormatter onlyTimeAndSinceStart = _onlyTimeAndSinceStart;

/// Prints only the date.
///
/// Example:
/// * `2019-06-04`
static const DateTimeFormatter onlyDate = _onlyDate;

/// Prints date and time (combines [onlyDate] and [onlyTime]).
///
/// Example:
/// * `2019-06-04 12:30:40.550`
static const DateTimeFormatter dateAndTime = _dateAndTime;

DateTimeFormat._();

static String _none(DateTime t) => throw UnimplementedError();

static String _onlyTime(DateTime t) {
String threeDigits(int n) {
if (n >= 100) return '$n';
if (n >= 10) return '0$n';
return '00$n';
}

String twoDigits(int n) {
if (n >= 10) return '$n';
return '0$n';
}

var now = t;
var h = twoDigits(now.hour);
var min = twoDigits(now.minute);
var sec = twoDigits(now.second);
var ms = threeDigits(now.millisecond);
return '$h:$min:$sec.$ms';
}

static String _onlyTimeAndSinceStart(DateTime t) {
var timeSinceStart = t.difference(PrettyPrinter.startTime!).toString();
return '${onlyTime(t)} (+$timeSinceStart)';
}

static String _onlyDate(DateTime t) {
String isoDate = t.toIso8601String();
return isoDate.substring(0, isoDate.indexOf("T"));
}

static String _dateAndTime(DateTime t) {
return "${_onlyDate(t)} ${_onlyTime(t)}";
}
}
47 changes: 24 additions & 23 deletions lib/src/printers/pretty_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:math';

import '../ansi_color.dart';
import '../date_time_format.dart';
import '../log_event.dart';
import '../log_level.dart';
import '../log_printer.dart';
Expand Down Expand Up @@ -64,7 +65,7 @@ class PrettyPrinter extends LogPrinter {
static final _browserStackTraceRegex =
RegExp(r'^(?:package:)?(dart:\S+|\S+)');

static DateTime? _startTime;
static DateTime? startTime;

/// The index at which the stack trace should start.
///
Expand Down Expand Up @@ -113,7 +114,11 @@ class PrettyPrinter extends LogPrinter {
final bool printEmojis;

/// Whether [LogEvent.time] is printed.
final bool printTime;
@Deprecated("Use `dateTimeFormat` instead.")
bool get printTime => dateTimeFormat != DateTimeFormat.none;

/// Controls the format of [LogEvent.time].
final DateTimeFormatter dateTimeFormat;

/// Controls the ascii 'boxing' of different [Level]s.
///
Expand Down Expand Up @@ -191,14 +196,25 @@ class PrettyPrinter extends LogPrinter {
this.lineLength = 120,
this.colors = true,
this.printEmojis = true,
this.printTime = false,
@Deprecated(
"Use `dateTimeFormat` with `DateTimeFormat.onlyTimeAndSinceStart` or `DateTimeFormat.none` instead.")
bool? printTime,
DateTimeFormatter dateTimeFormat = DateTimeFormat.none,
this.excludeBox = const {},
this.noBoxingByDefault = false,
this.excludePaths = const [],
this.levelColors,
this.levelEmojis,
}) {
_startTime ??= DateTime.now();
}) : assert(
(printTime != null && dateTimeFormat == DateTimeFormat.none) ||
printTime == null,
"Don't set printTime when using dateTimeFormat"),
dateTimeFormat = printTime == null
? dateTimeFormat
: (printTime
? DateTimeFormat.onlyTimeAndSinceStart
: DateTimeFormat.none) {
startTime ??= DateTime.now();

var doubleDividerLine = StringBuffer();
var singleDividerLine = StringBuffer();
Expand Down Expand Up @@ -241,6 +257,8 @@ class PrettyPrinter extends LogPrinter {
var errorStr = event.error?.toString();

String? timeStr;
// Keep backwards-compatibility to `printTime` check
// ignore: deprecated_member_use_from_same_package
if (printTime) {
timeStr = getTime(event.time);
}
Expand Down Expand Up @@ -332,24 +350,7 @@ class PrettyPrinter extends LogPrinter {
}

String getTime(DateTime time) {
String threeDigits(int n) {
if (n >= 100) return '$n';
if (n >= 10) return '0$n';
return '00$n';
}

String twoDigits(int n) {
if (n >= 10) return '$n';
return '0$n';
}

var now = time;
var h = twoDigits(now.hour);
var min = twoDigits(now.minute);
var sec = twoDigits(now.second);
var ms = threeDigits(now.millisecond);
var timeSinceStart = now.difference(_startTime!).toString();
return '$h:$min:$sec.$ms (+$timeSinceStart)';
return dateTimeFormat(time);
}

// Handles any object that is causing JsonEncoder() problems
Expand Down
1 change: 1 addition & 0 deletions lib/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
library web;

export 'src/ansi_color.dart';
export 'src/date_time_format.dart';
export 'src/filters/development_filter.dart';
export 'src/filters/production_filter.dart';
export 'src/log_event.dart';
Expand Down

0 comments on commit 901b07b

Please sign in to comment.