Skip to content

Commit

Permalink
Fixed stackTrace count when using stackTraceBeginIndex, addresses s…
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Feb 25, 2023
1 parent 9a8da9c commit f7f4779
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
31 changes: 17 additions & 14 deletions lib/src/printers/pretty_printer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:math';

import 'package:logger/src/ansi_color.dart';
import 'package:logger/src/log_printer.dart';
Expand Down Expand Up @@ -151,23 +152,25 @@ class PrettyPrinter extends LogPrinter {
}

String? formatStackTrace(StackTrace? stackTrace, int methodCount) {
var lines = stackTrace.toString().split('\n');
if (stackTraceBeginIndex > 0 && stackTraceBeginIndex < lines.length - 1) {
lines = lines.sublist(stackTraceBeginIndex);
}
var formatted = <String>[];
var count = 0;
for (var line in lines) {
if (_discardDeviceStacktraceLine(line) ||
_discardWebStacktraceLine(line) ||
_discardBrowserStacktraceLine(line) ||
line.isEmpty) {
List<String> lines = stackTrace
.toString()
.split('\n')
.where(
(line) =>
!_discardDeviceStacktraceLine(line) &&
!_discardWebStacktraceLine(line) &&
!_discardBrowserStacktraceLine(line) &&
line.isNotEmpty,
)
.toList();
List<String> formatted = [];

for (int count = 0; count < min(lines.length, methodCount); count++) {
var line = lines[count];
if (count < stackTraceBeginIndex) {
continue;
}
formatted.add('#$count ${line.replaceFirst(RegExp(r'#\d+\s+'), '')}');
if (++count == methodCount) {
break;
}
}

if (formatted.isEmpty) {
Expand Down
26 changes: 25 additions & 1 deletion test/printers/pretty_printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:test/test.dart';

void main() {
String readMessage(List<String> log) {
return log.reduce((acc, val) => acc + val);
return log.reduce((acc, val) => "$acc\n$val");
}

final prettyPrinter = PrettyPrinter(printEmojis: false);
Expand Down Expand Up @@ -95,4 +95,28 @@ void main() {
contains(expectedMessage),
);
});

test('stackTraceBeginIndex', () {
final prettyPrinter = PrettyPrinter(
stackTraceBeginIndex: 2,
);
final withFunction = LogEvent(
Level.debug,
"some message",
'some error',
StackTrace.current,
);

final actualLog = prettyPrinter.log(withFunction);
final actualLogString = readMessage(actualLog);

expect(
actualLogString,
allOf([
isNot(contains("#0 ")),
isNot(contains("#1 ")),
contains("#2 "),
]),
);
});
}

0 comments on commit f7f4779

Please sign in to comment.