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 import 'dart:math'; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'frame.dart'; | 10 import 'frame.dart'; |
11 | 11 |
12 final _patchRegExp = new RegExp(r"-patch$"); | 12 final _patchRegExp = new RegExp(r"-patch$"); |
13 | 13 |
14 /// A stack trace, comprised of a list of stack frames. | 14 /// A stack trace, comprised of a list of stack frames. |
15 class Trace implements StackTrace { | 15 class Trace implements StackTrace { |
16 // TODO(nweiz): make this read-only once issue 8321 is fixed. | 16 // TODO(nweiz): make this read-only once issue 8321 is fixed. |
17 /// The stack frames that comprise this stack trace. | 17 /// The stack frames that comprise this stack trace. |
18 final List<Frame> frames; | 18 final List<Frame> frames; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 110 } |
111 | 111 |
112 return new Trace(newFrames.reversed); | 112 return new Trace(newFrames.reversed); |
113 } | 113 } |
114 | 114 |
115 /// Returns a human-readable string representation of [this]. | 115 /// Returns a human-readable string representation of [this]. |
116 String toString() { | 116 String toString() { |
117 if (frames.length == '') return ''; | 117 if (frames.length == '') return ''; |
118 | 118 |
119 // Figure out the longest path so we know how much to pad. | 119 // Figure out the longest path so we know how much to pad. |
120 var longest = frames.map((frame) => frame.location.length).reduce(max); | 120 var longest = frames.map((frame) => frame.location.length).reduce(math.max); |
121 | 121 |
122 // Print out the stack trace nicely formatted. | 122 // Print out the stack trace nicely formatted. |
123 return frames.map((frame) { | 123 return frames.map((frame) { |
124 return '${_padRight(frame.location, longest)} ${frame.member}\n'; | 124 return '${_padRight(frame.location, longest)} ${frame.member}\n'; |
125 }).join(); | 125 }).join(); |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 /// Returns [string] with enough spaces added to the end to make it [length] | 129 /// Returns [string] with enough spaces added to the end to make it [length] |
130 /// characters long. | 130 /// characters long. |
131 String _padRight(String string, int length) { | 131 String _padRight(String string, int length) { |
132 if (string.length >= length) return string; | 132 if (string.length >= length) return string; |
133 | 133 |
134 var result = new StringBuffer(); | 134 var result = new StringBuffer(); |
135 result.write(string); | 135 result.write(string); |
136 for (var i = 0; i < length - string.length; i++) { | 136 for (var i = 0; i < length - string.length; i++) { |
137 result.write(' '); | 137 result.write(' '); |
138 } | 138 } |
139 | 139 |
140 return result.toString(); | 140 return result.toString(); |
141 } | 141 } |
OLD | NEW |