我可以得到一个“堆栈跟踪”跟踪所有异步调用,与Dart? [英] Can I get a "stack trace" that traces all async calls, with Dart?

查看:388
本文介绍了我可以得到一个“堆栈跟踪”跟踪所有异步调用,与Dart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这样的代码:

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));
}

如何判断 bar stackTrace 中被 foo 调用了吗?

How can I tell that bar was "called" by foo in the stackTrace that I print out?

我想看到如下:

bar
...
foo
...
main


推荐答案

请查看 stack_trace包。它使用区域来跟踪异步回调。

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.

包中的输出示例:

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

根据doc ,获取这些跟踪的最简单方法是使用 Chain.capture

According to the doc, the easiest way to get these traces is to use Chain.capture.

这篇关于我可以得到一个“堆栈跟踪”跟踪所有异步调用,与Dart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆