| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 return new LazyTrace(() => new Trace.parse(trace.toString())); | 75 return new LazyTrace(() => new Trace.parse(trace.toString())); |
| 76 } | 76 } |
| 77 | 77 |
| 78 /// Parses a string representation of a stack trace. | 78 /// Parses a string representation of a stack trace. |
| 79 /// | 79 /// |
| 80 /// [trace] should be formatted in the same way as a Dart VM or browser stack | 80 /// [trace] should be formatted in the same way as a Dart VM or browser stack |
| 81 /// trace. | 81 /// trace. |
| 82 factory Trace.parse(String trace) { | 82 factory Trace.parse(String trace) { |
| 83 try { | 83 try { |
| 84 if (trace.isEmpty) return new Trace(<Frame>[]); | 84 if (trace.isEmpty) return new Trace(<Frame>[]); |
| 85 if (trace.contains(_v8Trace)) return new Trace.parseV8(trace); | 85 if (trace.startsWith("Error\n")) return new Trace.parseV8(trace); |
| 86 // Valid Safari traces are a superset of valid Firefox traces. |
| 86 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); | 87 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); |
| 87 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace); | 88 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace); |
| 88 | 89 |
| 89 // Default to parsing the stack trace as a VM trace. This is also hit on | 90 // Default to parsing the stack trace as a VM trace. This is also hit on |
| 90 // IE and Safari, where the stack trace is just an empty string (issue | 91 // IE and Safari, where the stack trace is just an empty string (issue |
| 91 // 11257). | 92 // 11257). |
| 92 return new Trace.parseVM(trace); | 93 return new Trace.parseVM(trace); |
| 93 } on FormatException catch (error) { | 94 } on FormatException catch (error) { |
| 94 throw new FormatException('${error.message}\nStack trace:\n$trace'); | 95 throw new FormatException('${error.message}\nStack trace:\n$trace'); |
| 95 } | 96 } |
| 96 } | 97 } |
| 97 | 98 |
| 98 /// Parses a string representation of a Dart VM stack trace. | 99 /// Parses a string representation of a Dart VM stack trace. |
| 99 Trace.parseVM(String trace) | 100 Trace.parseVM(String trace) |
| 100 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); | 101 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); |
| 101 | 102 |
| 102 /// Parses a string representation of a Chrome/V8 stack trace. | 103 /// Parses a string representation of a Chrome/V8 stack trace. |
| 103 Trace.parseV8(String trace) | 104 Trace.parseV8(String trace) |
| 104 : this(trace.split("\n").skip(1) | 105 : this(trace.split("\n").skip(1) |
| 105 // It's possible that an Exception's description contains a line that | 106 // It's possible that an Exception's description contains a line that |
| 106 // looks like a V8 trace line, which will screw this up. | 107 // looks like a V8 trace line, which will screw this up. |
| 107 // Unfortunately, that's impossible to detect. | 108 // Unfortunately, that's impossible to detect. |
| 108 .skipWhile((line) => !line.startsWith(" at ")) | 109 .skipWhile((line) => !line.startsWith(" at ")) |
| 109 .map((line) => new Frame.parseV8(line))); | 110 .map((line) => new Frame.parseV8(line))); |
| 110 | 111 |
| 112 /// Parses a string representation of an Internet Explorer stack trace. |
| 113 /// |
| 114 /// IE10+ traces look just like V8 traces. Prior to IE10, stack traces can't |
| 115 /// be retrieved. |
| 116 Trace.parseIE(String trace) |
| 117 : this.parseV8(trace); |
| 118 |
| 111 /// Parses a string representation of a Firefox stack trace. | 119 /// Parses a string representation of a Firefox stack trace. |
| 112 Trace.parseFirefox(String trace) | 120 Trace.parseFirefox(String trace) |
| 113 : this(trace.trim().split("\n") | 121 : this(trace.trim().split("\n") |
| 114 .map((line) => new Frame.parseFirefox(line))); | 122 .map((line) => new Frame.parseFirefox(line))); |
| 115 | 123 |
| 124 /// Parses a string representation of a Safari stack trace. |
| 125 /// |
| 126 /// Safari 6+ stack traces look just like Firefox traces, except that they |
| 127 /// sometimes (e.g. in isolates) have a "[native code]" frame. We just ignore |
| 128 /// this frame to make the stack format more consistent between browsers. |
| 129 /// Prior to Safari 6, stack traces can't be retrieved. |
| 130 Trace.parseSafari(String trace) |
| 131 : this(trace.trim().split("\n") |
| 132 .where((line) => line != '[native code]') |
| 133 .map((line) => new Frame.parseFirefox(line))); |
| 134 |
| 116 /// Parses this package's a string representation of a stack trace. | 135 /// Parses this package's a string representation of a stack trace. |
| 117 Trace.parseFriendly(String trace) | 136 Trace.parseFriendly(String trace) |
| 118 : this(trace.trim().split("\n") | 137 : this(trace.trim().split("\n") |
| 119 .map((line) => new Frame.parseFriendly(line))); | 138 .map((line) => new Frame.parseFriendly(line))); |
| 120 | 139 |
| 121 /// Returns a new [Trace] comprised of [frames]. | 140 /// Returns a new [Trace] comprised of [frames]. |
| 122 Trace(Iterable<Frame> frames) | 141 Trace(Iterable<Frame> frames) |
| 123 : frames = new UnmodifiableListView<Frame>(frames.toList()); | 142 : frames = new UnmodifiableListView<Frame>(frames.toList()); |
| 124 | 143 |
| 125 /// Returns a VM-style [StackTrace] object. | 144 /// Returns a VM-style [StackTrace] object. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 // Figure out the longest path so we know how much to pad. | 187 // Figure out the longest path so we know how much to pad. |
| 169 var longest = frames.map((frame) => frame.location.length) | 188 var longest = frames.map((frame) => frame.location.length) |
| 170 .fold(0, math.max); | 189 .fold(0, math.max); |
| 171 | 190 |
| 172 // Print out the stack trace nicely formatted. | 191 // Print out the stack trace nicely formatted. |
| 173 return frames.map((frame) { | 192 return frames.map((frame) { |
| 174 return '${padRight(frame.location, longest)} ${frame.member}\n'; | 193 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
| 175 }).join(); | 194 }).join(); |
| 176 } | 195 } |
| 177 } | 196 } |
| OLD | NEW |