| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library stack_trace.chain; | 5 library stack_trace.chain; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'frame.dart'; |
| 10 import 'stack_zone_specification.dart'; | 11 import 'stack_zone_specification.dart'; |
| 11 import 'trace.dart'; | 12 import 'trace.dart'; |
| 12 import 'utils.dart'; | 13 import 'utils.dart'; |
| 13 | 14 |
| 14 /// A function that handles errors in the zone wrapped by [Chain.capture]. | 15 /// A function that handles errors in the zone wrapped by [Chain.capture]. |
| 15 typedef void ChainHandler(error, Chain chain); | 16 typedef void ChainHandler(error, Chain chain); |
| 16 | 17 |
| 17 /// A chain of stack traces. | 18 /// A chain of stack traces. |
| 18 /// | 19 /// |
| 19 /// A stack chain is a collection of one or more stack traces that collectively | 20 /// A stack chain is a collection of one or more stack traces that collectively |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 166 |
| 166 // If all the traces contain only internal processing, preserve the last | 167 // If all the traces contain only internal processing, preserve the last |
| 167 // (top-most) one so that the chain isn't empty. | 168 // (top-most) one so that the chain isn't empty. |
| 168 if (nonEmptyTraces.isEmpty && terseTraces.isNotEmpty) { | 169 if (nonEmptyTraces.isEmpty && terseTraces.isNotEmpty) { |
| 169 return new Chain([terseTraces.last]); | 170 return new Chain([terseTraces.last]); |
| 170 } | 171 } |
| 171 | 172 |
| 172 return new Chain(nonEmptyTraces); | 173 return new Chain(nonEmptyTraces); |
| 173 } | 174 } |
| 174 | 175 |
| 176 /// Returns a new [Chain] based on [this] where multiple stack frames matching |
| 177 /// [predicate] are folded together. |
| 178 /// |
| 179 /// This means that whenever there are multiple frames in a row that match |
| 180 /// [predicate], only the last one is kept. In addition, traces that are |
| 181 /// composed entirely of frames matching [predicate] are omitted. |
| 182 /// |
| 183 /// This is useful for limiting the amount of library code that appears in a |
| 184 /// stack trace by only showing user code and code that's called by user code. |
| 185 Chain foldFrames(bool predicate(Frame frame)) { |
| 186 var foldedTraces = traces.map((trace) => trace.foldFrames(predicate)); |
| 187 var nonEmptyTraces = foldedTraces.where((trace) { |
| 188 // Ignore traces that contain only folded frames. These traces will be |
| 189 // folded into a single frame each. |
| 190 return trace.frames.length > 1; |
| 191 }); |
| 192 |
| 193 // If all the traces contain only internal processing, preserve the last |
| 194 // (top-most) one so that the chain isn't empty. |
| 195 if (nonEmptyTraces.isEmpty && foldedTraces.isNotEmpty) { |
| 196 return new Chain([foldedTraces.last]); |
| 197 } |
| 198 |
| 199 return new Chain(nonEmptyTraces); |
| 200 } |
| 201 |
| 175 /// Converts [this] to a [Trace]. | 202 /// Converts [this] to a [Trace]. |
| 176 /// | 203 /// |
| 177 /// The trace version of a chain is just the concatenation of all the traces | 204 /// The trace version of a chain is just the concatenation of all the traces |
| 178 /// in the chain. | 205 /// in the chain. |
| 179 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames))); | 206 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames))); |
| 180 | 207 |
| 181 String toString() => traces.join(_GAP); | 208 String toString() => traces.join(_GAP); |
| 182 } | 209 } |
| OLD | NEW |