| 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 trace; | 5 library trace; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 | 9 |
| 10 import 'chain.dart'; | 10 import 'chain.dart'; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 /// The return value's [toString] method will always return a string | 195 /// The return value's [toString] method will always return a string |
| 196 /// representation in the Dart VM's stack trace format, regardless of what | 196 /// representation in the Dart VM's stack trace format, regardless of what |
| 197 /// platform is being used. | 197 /// platform is being used. |
| 198 StackTrace get vmTrace => new VMTrace(frames); | 198 StackTrace get vmTrace => new VMTrace(frames); |
| 199 | 199 |
| 200 /// Returns a terser version of [this]. | 200 /// Returns a terser version of [this]. |
| 201 /// | 201 /// |
| 202 /// This is accomplished by folding together multiple stack frames from the | 202 /// This is accomplished by folding together multiple stack frames from the |
| 203 /// core library or from this package, as in [foldFrames]. Remaining core | 203 /// core library or from this package, as in [foldFrames]. Remaining core |
| 204 /// library frames have their libraries, "-patch" suffixes, and line numbers | 204 /// library frames have their libraries, "-patch" suffixes, and line numbers |
| 205 /// removed. | 205 /// removed. If the outermost frame of the stack trace is a core library |
| 206 /// frame, it's removed entirely. |
| 206 /// | 207 /// |
| 207 /// For custom folding, see [foldFrames]. | 208 /// For custom folding, see [foldFrames]. |
| 208 Trace get terse => foldFrames((_) => false, terse: true); | 209 Trace get terse => foldFrames((_) => false, terse: true); |
| 209 | 210 |
| 210 /// Returns a new [Trace] based on [this] where multiple stack frames matching | 211 /// Returns a new [Trace] based on [this] where multiple stack frames matching |
| 211 /// [predicate] are folded together. | 212 /// [predicate] are folded together. |
| 212 /// | 213 /// |
| 213 /// This means that whenever there are multiple frames in a row that match | 214 /// This means that whenever there are multiple frames in a row that match |
| 214 /// [predicate], only the last one is kept. This is useful for limiting the | 215 /// [predicate], only the last one is kept. This is useful for limiting the |
| 215 /// amount of library code that appears in a stack trace by only showing user | 216 /// amount of library code that appears in a stack trace by only showing user |
| 216 /// code and code that's called by user code. | 217 /// code and code that's called by user code. |
| 217 /// | 218 /// |
| 218 /// If [terse] is true, this will also fold together frames from the core | 219 /// If [terse] is true, this will also fold together frames from the core |
| 219 /// library or from this package, and simplify core library frames as in | 220 /// library or from this package, simplify core library frames, and |
| 220 /// [Trace.terse]. | 221 /// potentially remove the outermost frame as in [Trace.terse]. |
| 221 Trace foldFrames(bool predicate(Frame frame), {bool terse: false}) { | 222 Trace foldFrames(bool predicate(Frame frame), {bool terse: false}) { |
| 222 if (terse) { | 223 if (terse) { |
| 223 var oldPredicate = predicate; | 224 var oldPredicate = predicate; |
| 224 predicate = (frame) { | 225 predicate = (frame) { |
| 225 if (oldPredicate(frame)) return true; | 226 if (oldPredicate(frame)) return true; |
| 226 | 227 |
| 227 if (frame.isCore) return true; | 228 if (frame.isCore) return true; |
| 228 if (frame.package == 'stack_trace') return true; | 229 if (frame.package == 'stack_trace') return true; |
| 229 | 230 |
| 230 // Ignore async stack frames without any line or column information. | 231 // Ignore async stack frames without any line or column information. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 248 frame.uri, frame.line, frame.column, frame.member)); | 249 frame.uri, frame.line, frame.column, frame.member)); |
| 249 } | 250 } |
| 250 } | 251 } |
| 251 | 252 |
| 252 if (terse) { | 253 if (terse) { |
| 253 newFrames = newFrames.map((frame) { | 254 newFrames = newFrames.map((frame) { |
| 254 if (!frame.isCore) return frame; | 255 if (!frame.isCore) return frame; |
| 255 var library = frame.library.replaceAll(_terseRegExp, ''); | 256 var library = frame.library.replaceAll(_terseRegExp, ''); |
| 256 return new Frame(Uri.parse(library), null, null, frame.member); | 257 return new Frame(Uri.parse(library), null, null, frame.member); |
| 257 }).toList(); | 258 }).toList(); |
| 259 if (newFrames.first.isCore && newFrames.length > 1) newFrames.removeAt(0); |
| 258 } | 260 } |
| 259 | 261 |
| 260 return new Trace(newFrames.reversed); | 262 return new Trace(newFrames.reversed); |
| 261 } | 263 } |
| 262 | 264 |
| 263 /// Returns a human-readable string representation of [this]. | 265 /// Returns a human-readable string representation of [this]. |
| 264 String toString() { | 266 String toString() { |
| 265 // Figure out the longest path so we know how much to pad. | 267 // Figure out the longest path so we know how much to pad. |
| 266 var longest = frames.map((frame) => frame.location.length) | 268 var longest = frames.map((frame) => frame.location.length) |
| 267 .fold(0, math.max); | 269 .fold(0, math.max); |
| 268 | 270 |
| 269 // Print out the stack trace nicely formatted. | 271 // Print out the stack trace nicely formatted. |
| 270 return frames.map((frame) { | 272 return frames.map((frame) { |
| 271 return '${padRight(frame.location, longest)} ${frame.member}\n'; | 273 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
| 272 }).join(); | 274 }).join(); |
| 273 } | 275 } |
| 274 } | 276 } |
| OLD | NEW |