Can I get a “stack trace” that traces all async calls, with Dart?

China☆狼群 提交于 2019-12-07 04:15:10

问题


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?

I'd like to see something like:

bar
...
foo
...
main

回答1:


Have a look at the stack_trace package. 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.



来源:https://stackoverflow.com/questions/22056647/can-i-get-a-stack-trace-that-traces-all-async-calls-with-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!