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 'frame.dart'; | 10 import 'frame.dart'; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 newFrames.add(new Frame( | 136 newFrames.add(new Frame( |
137 frame.uri, frame.line, frame.column, frame.member)); | 137 frame.uri, frame.line, frame.column, frame.member)); |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 return new Trace(newFrames.reversed); | 141 return new Trace(newFrames.reversed); |
142 } | 142 } |
143 | 143 |
144 /// Returns a human-readable string representation of [this]. | 144 /// Returns a human-readable string representation of [this]. |
145 String toString() { | 145 String toString() { |
146 if (frames.length == '') return ''; | |
147 | |
148 // Figure out the longest path so we know how much to pad. | 146 // Figure out the longest path so we know how much to pad. |
149 var longest = frames.map((frame) => frame.location.length).reduce(math.max); | 147 var longest = frames.map((frame) => frame.location.length) |
| 148 .fold(0, math.max); |
150 | 149 |
151 // Print out the stack trace nicely formatted. | 150 // Print out the stack trace nicely formatted. |
152 return frames.map((frame) { | 151 return frames.map((frame) { |
153 return '${_padRight(frame.location, longest)} ${frame.member}\n'; | 152 return '${_padRight(frame.location, longest)} ${frame.member}\n'; |
154 }).join(); | 153 }).join(); |
155 } | 154 } |
156 } | 155 } |
157 | 156 |
158 /// Returns [string] with enough spaces added to the end to make it [length] | 157 /// Returns [string] with enough spaces added to the end to make it [length] |
159 /// characters long. | 158 /// characters long. |
160 String _padRight(String string, int length) { | 159 String _padRight(String string, int length) { |
161 if (string.length >= length) return string; | 160 if (string.length >= length) return string; |
162 | 161 |
163 var result = new StringBuffer(); | 162 var result = new StringBuffer(); |
164 result.write(string); | 163 result.write(string); |
165 for (var i = 0; i < length - string.length; i++) { | 164 for (var i = 0; i < length - string.length; i++) { |
166 result.write(' '); | 165 result.write(' '); |
167 } | 166 } |
168 | 167 |
169 return result.toString(); | 168 return result.toString(); |
170 } | 169 } |
OLD | NEW |