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