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 import 'dart:collection'; | 5 import 'dart:collection'; |
6 import 'dart:math' as math; | 6 import 'dart:math' as math; |
7 | 7 |
8 import 'chain.dart'; | 8 import 'chain.dart'; |
9 import 'frame.dart'; | 9 import 'frame.dart'; |
10 import 'lazy_trace.dart'; | 10 import 'lazy_trace.dart'; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 /// | 74 /// |
75 /// By default, the first frame of this trace will be the line where | 75 /// By default, the first frame of this trace will be the line where |
76 /// [Trace.current] is called. If [level] is passed, the trace will start that | 76 /// [Trace.current] is called. If [level] is passed, the trace will start that |
77 /// many frames up instead. | 77 /// many frames up instead. |
78 factory Trace.current([int level=0]) { | 78 factory Trace.current([int level=0]) { |
79 if (level < 0) { | 79 if (level < 0) { |
80 throw new ArgumentError("Argument [level] must be greater than or equal " | 80 throw new ArgumentError("Argument [level] must be greater than or equal " |
81 "to 0."); | 81 "to 0."); |
82 } | 82 } |
83 | 83 |
84 try { | 84 var trace = new Trace.from(StackTrace.current); |
85 throw ''; | 85 return new LazyTrace(() => new Trace(trace.frames.skip(level + 1))); |
86 } catch (_, nativeTrace) { | |
87 var trace = new Trace.from(nativeTrace); | |
88 return new LazyTrace(() => new Trace(trace.frames.skip(level + 1))); | |
89 } | |
90 } | 86 } |
91 | 87 |
92 /// Returns a new stack trace containing the same data as [trace]. | 88 /// Returns a new stack trace containing the same data as [trace]. |
93 /// | 89 /// |
94 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's | 90 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's |
95 /// a [Trace], it will be returned as-is. | 91 /// a [Trace], it will be returned as-is. |
96 factory Trace.from(StackTrace trace) { | 92 factory Trace.from(StackTrace trace) { |
97 // Normally explicitly validating null arguments is bad Dart style, but here | 93 // Normally explicitly validating null arguments is bad Dart style, but here |
98 // the natural failure will only occur when the LazyTrace is materialized, | 94 // the natural failure will only occur when the LazyTrace is materialized, |
99 // and we want to provide an error that's more local to the actual problem. | 95 // and we want to provide an error that's more local to the actual problem. |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 var longest = frames.map((frame) => frame.location.length) | 285 var longest = frames.map((frame) => frame.location.length) |
290 .fold(0, math.max); | 286 .fold(0, math.max); |
291 | 287 |
292 // Print out the stack trace nicely formatted. | 288 // Print out the stack trace nicely formatted. |
293 return frames.map((frame) { | 289 return frames.map((frame) { |
294 if (frame is UnparsedFrame) return "$frame\n"; | 290 if (frame is UnparsedFrame) return "$frame\n"; |
295 return '${padRight(frame.location, longest)} ${frame.member}\n'; | 291 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
296 }).join(); | 292 }).join(); |
297 } | 293 } |
298 } | 294 } |
OLD | NEW |