Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1391)

Side by Side Diff: pkg/stack_trace/lib/src/trace.dart

Issue 18029023: Add a couple functions to package:stack_trace. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/stack_trace/lib/src/lazy_trace.dart ('k') | pkg/stack_trace/lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'utils.dart';
13 import 'vm_trace.dart';
12 14
13 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); 15 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$");
14 16
15 /// A RegExp to match Firefox's stack traces. 17 /// A RegExp to match Firefox's stack traces.
16 /// 18 ///
17 /// Firefox's trace frames start with the name of the function in which the 19 /// Firefox's trace frames start with the name of the function in which the
18 /// error occurred, possibly including its parameters inside `()`. For example, 20 /// error occurred, possibly including its parameters inside `()`. For example,
19 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. 21 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`.
20 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]*|\(.*\))*@"); 22 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]*|\(.*\))*@");
21 23
24 /// A RegExp to match this package's stack traces.
25 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+:\d+)?\s+[^\s]+($|\n)");
26
22 /// A stack trace, comprised of a list of stack frames. 27 /// A stack trace, comprised of a list of stack frames.
23 class Trace implements StackTrace { 28 class Trace implements StackTrace {
24 /// The stack frames that comprise this stack trace. 29 /// The stack frames that comprise this stack trace.
25 final List<Frame> frames; 30 final List<Frame> frames;
26 31
27 /// Returns a human-readable representation of [stackTrace]. If [terse] is 32 /// Returns a human-readable representation of [stackTrace]. If [terse] is
28 /// set, this folds together multiple stack frames from the Dart core 33 /// set, this folds together multiple stack frames from the Dart core
29 /// libraries, so that only the core library method directly called from user 34 /// libraries, so that only the core library method directly called from user
30 /// code is visible (see [Trace.terse]). 35 /// code is visible (see [Trace.terse]).
31 static String format(StackTrace stackTrace, {bool terse: true}) { 36 static String format(StackTrace stackTrace, {bool terse: true}) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 } 68 }
64 69
65 /// Parses a string representation of a stack trace. 70 /// Parses a string representation of a stack trace.
66 /// 71 ///
67 /// [trace] should be formatted in the same way as a Dart VM or browser stack 72 /// [trace] should be formatted in the same way as a Dart VM or browser stack
68 /// trace. 73 /// trace.
69 factory Trace.parse(String trace) { 74 factory Trace.parse(String trace) {
70 if (trace.isEmpty) return new Trace(<Frame>[]); 75 if (trace.isEmpty) return new Trace(<Frame>[]);
71 if (trace.startsWith("Error\n")) return new Trace.parseV8(trace); 76 if (trace.startsWith("Error\n")) return new Trace.parseV8(trace);
72 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); 77 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace);
78 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace);
73 79
74 // Default to parsing the stack trace as a VM trace. This is also hit on IE 80 // Default to parsing the stack trace as a VM trace. This is also hit on IE
75 // and Safari, where the stack trace is just an empty string (issue 11257). 81 // and Safari, where the stack trace is just an empty string (issue 11257).
76 return new Trace.parseVM(trace); 82 return new Trace.parseVM(trace);
77 } 83 }
78 84
79 /// Parses a string representation of a Dart VM stack trace. 85 /// Parses a string representation of a Dart VM stack trace.
80 Trace.parseVM(String trace) 86 Trace.parseVM(String trace)
81 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); 87 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line)));
82 88
83 /// Parses a string representation of a Chrome/V8 stack trace. 89 /// Parses a string representation of a Chrome/V8 stack trace.
84 Trace.parseV8(String trace) 90 Trace.parseV8(String trace)
85 : this(trace.split("\n").skip(1).map((line) => new Frame.parseV8(line))); 91 : this(trace.split("\n").skip(1).map((line) => new Frame.parseV8(line)));
86 92
87 /// Parses a string representation of a Firefox stack trace. 93 /// Parses a string representation of a Firefox stack trace.
88 Trace.parseFirefox(String trace) 94 Trace.parseFirefox(String trace)
89 : this(trace.trim().split("\n") 95 : this(trace.trim().split("\n")
90 .map((line) => new Frame.parseFirefox(line))); 96 .map((line) => new Frame.parseFirefox(line)));
91 97
98 /// Parses this package's a string representation of a stack trace.
99 Trace.parseFriendly(String trace)
100 : this(trace.trim().split("\n")
101 .map((line) => new Frame.parseFriendly(line)));
102
92 /// Returns a new [Trace] comprised of [frames]. 103 /// Returns a new [Trace] comprised of [frames].
93 Trace(Iterable<Frame> frames) 104 Trace(Iterable<Frame> frames)
94 : frames = new UnmodifiableListView<Frame>(frames.toList()); 105 : frames = new UnmodifiableListView<Frame>(frames.toList());
95 106
96 // TODO(nweiz): Keep track of which [Frame]s are part of the partial stack 107 /// Returns a VM-style [StackTrace] object.
97 // trace and only print them.
98 /// Returns a string representation of this stack trace.
99 /// 108 ///
100 /// This is identical to [toString]. It will not be formatted in the manner of 109 /// The return value's [toString] method will always return a string
101 /// native stack traces. 110 /// representation in the Dart VM's stack trace format, regardless of what
102 String get stackTrace => toString(); 111 /// platform is being used.
103 112 StackTrace get vmTrace => new VMTrace(frames);
104 /// Returns a string representation of this stack trace.
105 ///
106 /// This is identical to [toString]. It will not be formatted in the manner of
107 /// native stack traces.
108 String get fullStackTrace => toString();
109 113
110 /// Returns a terser version of [this]. 114 /// Returns a terser version of [this].
111 /// 115 ///
112 /// This is accomplished by folding together multiple stack frames from the 116 /// This is accomplished by folding together multiple stack frames from the
113 /// core library, as in [foldFrames]. Remaining core library frames have their 117 /// core library, as in [foldFrames]. Remaining core library frames have their
114 /// libraries, "-patch" suffixes, and line numbers removed. 118 /// libraries, "-patch" suffixes, and line numbers removed.
115 Trace get terse { 119 Trace get terse {
116 return new Trace(foldFrames((frame) => frame.isCore).frames.map((frame) { 120 return new Trace(foldFrames((frame) => frame.isCore).frames.map((frame) {
117 if (!frame.isCore) return frame; 121 if (!frame.isCore) return frame;
118 var library = frame.library.replaceAll(_terseRegExp, ''); 122 var library = frame.library.replaceAll(_terseRegExp, '');
(...skipping 23 matching lines...) Expand all
142 } 146 }
143 147
144 /// Returns a human-readable string representation of [this]. 148 /// Returns a human-readable string representation of [this].
145 String toString() { 149 String toString() {
146 // Figure out the longest path so we know how much to pad. 150 // Figure out the longest path so we know how much to pad.
147 var longest = frames.map((frame) => frame.location.length) 151 var longest = frames.map((frame) => frame.location.length)
148 .fold(0, math.max); 152 .fold(0, math.max);
149 153
150 // Print out the stack trace nicely formatted. 154 // Print out the stack trace nicely formatted.
151 return frames.map((frame) { 155 return frames.map((frame) {
152 return '${_padRight(frame.location, longest)} ${frame.member}\n'; 156 return '${padRight(frame.location, longest)} ${frame.member}\n';
153 }).join(); 157 }).join();
154 } 158 }
155 } 159 }
156
157 /// Returns [string] with enough spaces added to the end to make it [length]
158 /// characters long.
159 String _padRight(String string, int length) {
160 if (string.length >= length) return string;
161
162 var result = new StringBuffer();
163 result.write(string);
164 for (var i = 0; i < length - string.length; i++) {
165 result.write(' ');
166 }
167
168 return result.toString();
169 }
OLDNEW
« no previous file with comments | « pkg/stack_trace/lib/src/lazy_trace.dart ('k') | pkg/stack_trace/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698