简体   繁体   中英

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?

I'd like to see something like:

bar
...
foo
...
main

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM