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'; |
11 import 'lazy_trace.dart'; | 11 import 'lazy_trace.dart'; |
12 | 12 |
13 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); | 13 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); |
14 | 14 |
15 /// A RegExp to match Firefox's stack traces. | |
16 /// | |
17 /// Firefox's trace frames start with the name of the function in which the | |
18 /// error occurred, possibly including its parameters inside `()`. For example, | |
19 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. | |
20 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]*|\(.*\))*@"); | |
21 | |
22 /// A stack trace, comprised of a list of stack frames. | 15 /// A stack trace, comprised of a list of stack frames. |
23 class Trace implements StackTrace { | 16 class Trace implements StackTrace { |
24 /// The stack frames that comprise this stack trace. | 17 /// The stack frames that comprise this stack trace. |
25 final List<Frame> frames; | 18 final List<Frame> frames; |
26 | 19 |
27 /// Returns a human-readable representation of [stackTrace]. If [terse] is | 20 /// Returns a human-readable representation of [stackTrace]. If [terse] is |
28 /// set, this folds together multiple stack frames from the Dart core | 21 /// set, this folds together multiple stack frames from the Dart core |
29 /// libraries, so that only the core library method directly called from user | 22 /// libraries, so that only the core library method directly called from user |
30 /// code is visible (see [Trace.terse]). | 23 /// code is visible (see [Trace.terse]). |
31 static String format(StackTrace stackTrace, {bool terse: true}) { | 24 static String format(StackTrace stackTrace, {bool terse: true}) { |
(...skipping 25 matching lines...) Expand all Loading... |
57 /// | 50 /// |
58 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's | 51 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's |
59 /// a [Trace], it will be returned as-is. | 52 /// a [Trace], it will be returned as-is. |
60 factory Trace.from(StackTrace trace) { | 53 factory Trace.from(StackTrace trace) { |
61 if (trace is Trace) return trace; | 54 if (trace is Trace) return trace; |
62 return new LazyTrace(() => new Trace.parse(trace.toString())); | 55 return new LazyTrace(() => new Trace.parse(trace.toString())); |
63 } | 56 } |
64 | 57 |
65 /// Parses a string representation of a stack trace. | 58 /// Parses a string representation of a stack trace. |
66 /// | 59 /// |
67 /// [trace] should be formatted in the same way as a Dart VM or browser stack | 60 /// [trace] should be formatted in the same way as native stack traces. |
68 /// trace. | 61 Trace.parse(String trace) |
69 factory Trace.parse(String trace) { | 62 : this(trace.trim().split("\n").map((line) => new Frame.parse(line))); |
70 if (trace.startsWith("Error\n")) return new Trace.parseV8(trace); | |
71 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); | |
72 | |
73 // Default to parsing the stack trace as a VM trace. This is also hit on IE | |
74 // and Safari, where the stack trace is just an empty string (issue 11257). | |
75 return new Trace.parseVM(trace); | |
76 } | |
77 | |
78 /// Parses a string representation of a Dart VM stack trace. | |
79 Trace.parseVM(String trace) | |
80 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); | |
81 | |
82 /// Parses a string representation of a Chrome/V8 stack trace. | |
83 Trace.parseV8(String trace) | |
84 : this(trace.split("\n").skip(1).map((line) => new Frame.parseV8(line))); | |
85 | |
86 /// Parses a string representation of a Firefox stack trace. | |
87 Trace.parseFirefox(String trace) | |
88 : this(trace.trim().split("\n") | |
89 .map((line) => new Frame.parseFirefox(line))); | |
90 | 63 |
91 /// Returns a new [Trace] comprised of [frames]. | 64 /// Returns a new [Trace] comprised of [frames]. |
92 Trace(Iterable<Frame> frames) | 65 Trace(Iterable<Frame> frames) |
93 : frames = new UnmodifiableListView<Frame>(frames.toList()); | 66 : frames = new UnmodifiableListView<Frame>(frames.toList()); |
94 | 67 |
95 // TODO(nweiz): Keep track of which [Frame]s are part of the partial stack | 68 // TODO(nweiz): Keep track of which [Frame]s are part of the partial stack |
96 // trace and only print them. | 69 // trace and only print them. |
97 /// Returns a string representation of this stack trace. | 70 /// Returns a string representation of this stack trace. |
98 /// | 71 /// |
99 /// This is identical to [toString]. It will not be formatted in the manner of | 72 /// This is identical to [toString]. It will not be formatted in the manner of |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 if (string.length >= length) return string; | 133 if (string.length >= length) return string; |
161 | 134 |
162 var result = new StringBuffer(); | 135 var result = new StringBuffer(); |
163 result.write(string); | 136 result.write(string); |
164 for (var i = 0; i < length - string.length; i++) { | 137 for (var i = 0; i < length - string.length; i++) { |
165 result.write(' '); | 138 result.write(' '); |
166 } | 139 } |
167 | 140 |
168 return result.toString(); | 141 return result.toString(); |
169 } | 142 } |
OLD | NEW |