| 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:uri'; | 7 import 'dart:uri'; |
| 8 | 8 |
| 9 import 'frame.dart'; | 9 import 'frame.dart'; |
| 10 | 10 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 /// native stack traces. | 72 /// native stack traces. |
| 73 String get stackTrace => toString(); | 73 String get stackTrace => toString(); |
| 74 | 74 |
| 75 /// Returns a string representation of this stack trace. | 75 /// Returns a string representation of this stack trace. |
| 76 /// | 76 /// |
| 77 /// This is identical to [toString]. It will not be formatted in the manner of | 77 /// This is identical to [toString]. It will not be formatted in the manner of |
| 78 /// native stack traces. | 78 /// native stack traces. |
| 79 String get fullStackTrace => toString(); | 79 String get fullStackTrace => toString(); |
| 80 | 80 |
| 81 /// Returns a terser version of [this]. This is accomplished by folding | 81 /// Returns a terser version of [this]. This is accomplished by folding |
| 82 /// together multiple stack frames from the core library. If multiple such | 82 /// together multiple stack frames from the core library, as in [foldFrames]. |
| 83 /// frames appear in a row, only the last (the one directly called by user | 83 /// Core library patches are also renamed to remove their `-patch` suffix. |
| 84 /// code) is kept. Core library patches are also renamed to remove their | |
| 85 /// `-patch` suffix. | |
| 86 Trace get terse { | 84 Trace get terse { |
| 85 return new Trace(foldFrames((frame) => frame.isCore).frames.map((frame) { |
| 86 if (!frame.isCore) return frame; |
| 87 var library = frame.library.replaceAll(_patchRegExp, ''); |
| 88 return new Frame( |
| 89 Uri.parse(library), frame.line, frame.column, frame.member); |
| 90 })); |
| 91 } |
| 92 |
| 93 /// Returns a new [Trace] based on [this] where multiple stack frames matching |
| 94 /// [predicate] are folded together. This means that whenever there are |
| 95 /// multiple frames in a row that match [predicate], only the last one is |
| 96 /// kept. |
| 97 /// |
| 98 /// This is useful for limiting the amount of library code that appears in a |
| 99 /// stack trace by only showing user code and code that's called by user code. |
| 100 Trace foldFrames(bool predicate(frame)) { |
| 87 var newFrames = <Frame>[]; | 101 var newFrames = <Frame>[]; |
| 88 for (var frame in frames.reversed) { | 102 for (var frame in frames.reversed) { |
| 89 if (!frame.isCore) { | 103 if (!predicate(frame)) { |
| 90 newFrames.add(frame); | 104 newFrames.add(frame); |
| 91 } else if (newFrames.isEmpty || !newFrames.last.isCore) { | 105 } else if (newFrames.isEmpty || !predicate(newFrames.last)) { |
| 92 var library = frame.library.replaceAll(_patchRegExp, ''); | |
| 93 newFrames.add(new Frame( | 106 newFrames.add(new Frame( |
| 94 Uri.parse(library), frame.line, frame.column, frame.member)); | 107 frame.uri, frame.line, frame.column, frame.member)); |
| 95 } | 108 } |
| 96 } | 109 } |
| 97 | 110 |
| 98 return new Trace(newFrames.reversed); | 111 return new Trace(newFrames.reversed); |
| 99 } | 112 } |
| 100 | 113 |
| 101 /// Returns a human-readable string representation of [this]. | 114 /// Returns a human-readable string representation of [this]. |
| 102 String toString() { | 115 String toString() { |
| 103 if (frames.length == '') return ''; | 116 if (frames.length == '') return ''; |
| 104 | 117 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 118 if (string.length >= length) return string; | 131 if (string.length >= length) return string; |
| 119 | 132 |
| 120 var result = new StringBuffer(); | 133 var result = new StringBuffer(); |
| 121 result.write(string); | 134 result.write(string); |
| 122 for (var i = 0; i < length - string.length; i++) { | 135 for (var i = 0; i < length - string.length; i++) { |
| 123 result.write(' '); | 136 result.write(' '); |
| 124 } | 137 } |
| 125 | 138 |
| 126 return result.toString(); | 139 return result.toString(); |
| 127 } | 140 } |
| OLD | NEW |