问题
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