[英]Can I get a “stack trace” that traces all async calls, with Dart?
Consider code like this: 考虑这样的代码:
import 'dart:async';
foo() {
new Timer(onesec, bar);
}
bar() {
throw "from bar";
}
const onesec = const Duration(seconds:1);
main() {
runZoned(() {
new Timer(onesec, foo);
},
onError: (e, stackTrace) => print(stackTrace));
}
How can I tell that bar
was "called" by foo
in the stackTrace
that I print out? 如何我可以告诉大家, bar
被“叫”,由foo
的stackTrace
,我打印出来?
I'd like to see something like: 我希望看到类似的东西:
bar
...
foo
...
main
Have a look at the stack_trace package . 看看stack_trace包 。 It uses zones to keep track of asynchronous callbacks. 它使用区域来跟踪异步回调。 Capturing stack traces for every asynchronous callback is expensive, but for debugging it is definitely worth it. 捕获每个异步回调的堆栈跟踪都很昂贵,但是对于调试来说它绝对值得。
Example output from the package: 包的示例输出:
http://dartlang.org/foo/bar.dart 10:11 Foo.<fn>.bar
http://dartlang.org/foo/baz.dart Foo.<fn>.bar
===== asynchronous gap ===========================
http://dartlang.org/foo/bang.dart 10:11 Foo.<fn>.bar
http://dartlang.org/foo/quux.dart Foo.<fn>.bar
According to the doc , the easiest way to get these traces is to use Chain.capture
. 根据文档 ,获取这些痕迹的最简单方法是使用Chain.capture
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.