File tree Expand file tree Collapse file tree 2 files changed +42
-15
lines changed Expand file tree Collapse file tree 2 files changed +42
-15
lines changed Original file line number Diff line number Diff line change 1
1
import 'dart:convert' ;
2
+ import 'dart:math' ;
2
3
3
4
import 'package:logger/src/ansi_color.dart' ;
4
5
import 'package:logger/src/log_printer.dart' ;
@@ -151,23 +152,25 @@ class PrettyPrinter extends LogPrinter {
151
152
}
152
153
153
154
String ? formatStackTrace (StackTrace ? stackTrace, int methodCount) {
154
- var lines = stackTrace.toString ().split ('\n ' );
155
- if (stackTraceBeginIndex > 0 && stackTraceBeginIndex < lines.length - 1 ) {
156
- lines = lines.sublist (stackTraceBeginIndex);
157
- }
158
- var formatted = < String > [];
159
- var count = 0 ;
160
- for (var line in lines) {
161
- if (_discardDeviceStacktraceLine (line) ||
162
- _discardWebStacktraceLine (line) ||
163
- _discardBrowserStacktraceLine (line) ||
164
- line.isEmpty) {
155
+ List <String > lines = stackTrace
156
+ .toString ()
157
+ .split ('\n ' )
158
+ .where (
159
+ (line) =>
160
+ ! _discardDeviceStacktraceLine (line) &&
161
+ ! _discardWebStacktraceLine (line) &&
162
+ ! _discardBrowserStacktraceLine (line) &&
163
+ line.isNotEmpty,
164
+ )
165
+ .toList ();
166
+ List <String > formatted = [];
167
+
168
+ for (int count = 0 ; count < min (lines.length, methodCount); count++ ) {
169
+ var line = lines[count];
170
+ if (count < stackTraceBeginIndex) {
165
171
continue ;
166
172
}
167
173
formatted.add ('#$count ${line .replaceFirst (RegExp (r'#\d+\s+' ), '' )}' );
168
- if (++ count == methodCount) {
169
- break ;
170
- }
171
174
}
172
175
173
176
if (formatted.isEmpty) {
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import 'package:test/test.dart';
3
3
4
4
void main () {
5
5
String readMessage (List <String > log) {
6
- return log.reduce ((acc, val) => acc + val);
6
+ return log.reduce ((acc, val) => "$ acc \n $ val " );
7
7
}
8
8
9
9
final prettyPrinter = PrettyPrinter (printEmojis: false );
@@ -95,4 +95,28 @@ void main() {
95
95
contains (expectedMessage),
96
96
);
97
97
});
98
+
99
+ test ('stackTraceBeginIndex' , () {
100
+ final prettyPrinter = PrettyPrinter (
101
+ stackTraceBeginIndex: 2 ,
102
+ );
103
+ final withFunction = LogEvent (
104
+ Level .debug,
105
+ "some message" ,
106
+ 'some error' ,
107
+ StackTrace .current,
108
+ );
109
+
110
+ final actualLog = prettyPrinter.log (withFunction);
111
+ final actualLogString = readMessage (actualLog);
112
+
113
+ expect (
114
+ actualLogString,
115
+ allOf ([
116
+ isNot (contains ("#0 " )),
117
+ isNot (contains ("#1 " )),
118
+ contains ("#2 " ),
119
+ ]),
120
+ );
121
+ });
98
122
}
You can’t perform that action at this time.
0 commit comments