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

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

Issue 13102003: Add a library for manipulating stack traces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 8 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/frame.dart ('k') | pkg/stack_trace/lib/stack_trace.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library trace;
6
7 import 'dart:uri';
8
9 import 'frame.dart';
10
11 final _patchRegExp = new RegExp(r"-patch$");
12
13 /// A stack trace, comprised of a list of stack frames.
14 class Trace implements StackTrace {
15 // TODO(nweiz): make this read-only once issue 8321 is fixed.
16 /// The stack frames that comprise this stack trace.
17 final List<Frame> frames;
18
19 /// Returns a human-readable representation of [stackTrace]. If [terse] is
20 /// set, this folds together multiple stack frames from the Dart core
21 /// libraries, so that only the core library method directly called from user
22 /// code is visible (see [Trace.terse]).
23 static String format(StackTrace stackTrace, {bool terse: true}) {
24 var trace = new Trace.from(stackTrace);
25 if (terse) trace = trace.terse;
26 return trace.toString();
27 }
28
29 /// Returns the current stack trace.
30 ///
31 /// By default, the first frame of this trace will be the line where
32 /// [Trace.current] is called. If [level] is passed, the trace will start that
33 /// many frames up instead.
34 factory Trace.current([int level=0]) {
35 if (level < 0) {
36 throw new ArgumentError("Argument [level] must be greater than or equal "
37 "to 0.");
38 }
39
40 try {
41 throw '';
42 } catch (_, nativeTrace) {
43 var trace = new Trace.from(nativeTrace);
44 return new Trace(trace.frames.skip(level + 1));
45 }
46 }
47
48 /// Returns a new stack trace containing the same data as [trace].
49 ///
50 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's
51 /// a [Trace], it will be returned as-is.
52 factory Trace.from(StackTrace trace) {
53 if (trace is Trace) return trace;
54 return new Trace.parse(trace.fullStackTrace);
55 }
56
57 /// Parses a string representation of a stack trace.
58 ///
59 /// [trace] should be formatted in the same way as native stack traces.
60 Trace.parse(String trace)
61 : this(trace.trim().split("\n").map((line) => new Frame.parse(line)));
62
63 /// Returns a new [Trace] comprised of [frames].
64 Trace(Iterable<Frame> frames)
65 : frames = frames.toList();
66
67 // TODO(nweiz): Keep track of which [Frame]s are part of the partial stack
68 // trace and only print them.
69 /// Returns a string representation of this stack trace.
70 ///
71 /// This is identical to [toString]. It will not be formatted in the manner of
72 /// native stack traces.
73 String get stackTrace => toString();
74
75 /// Returns a string representation of this stack trace.
76 ///
77 /// This is identical to [toString]. It will not be formatted in the manner of
78 /// native stack traces.
79 String get fullStackTrace => toString();
80
81 /// Returns a terser version of [this]. This is accomplished by folding
82 /// together multiple stack frames from the core library. If multiple such
83 /// frames appear in a row, only the last (the one directly called by user
84 /// code) is kept. Core library patches are also renamed to remove their
85 /// `-patch` suffix.
86 Trace get terse {
87 var newFrames = <Frame>[];
88 for (var frame in frames.reversed) {
89 if (!frame.isCore) {
90 newFrames.add(frame);
91 } else if (newFrames.isEmpty || !newFrames.last.isCore) {
92 var library = frame.library.replaceAll(_patchRegExp, '');
93 newFrames.add(new Frame(
94 Uri.parse(library), frame.line, frame.column, frame.member));
95 }
96 }
97
98 return new Trace(newFrames.reversed);
99 }
100
101 /// Returns a human-readable string representation of [this].
102 String toString() {
103 if (frames.length == '') return '';
104
105 // Figure out the longest path so we know how much to pad.
106 var longest = frames.map((frame) => frame.location.length).max();
107
108 // Print out the stack trace nicely formatted.
109 return frames.map((frame) {
110 return '${_padRight(frame.location, longest)} ${frame.member}\n';
111 }).join();
112 }
113 }
114
115 /// Returns [string] with enough spaces added to the end to make it [length]
116 /// characters long.
117 String _padRight(String string, int length) {
118 if (string.length >= length) return string;
119
120 var result = new StringBuffer();
121 result.write(string);
122 for (var i = 0; i < length - string.length; i++) {
123 result.write(' ');
124 }
125
126 return result.toString();
127 }
OLDNEW
« no previous file with comments | « pkg/stack_trace/lib/src/frame.dart ('k') | pkg/stack_trace/lib/stack_trace.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698