Ivá*_*oed 3 logging dart flutter
我想知道哪个类调用了特定的函数。我一直在为此查找文档,但没有成功。我已经知道如何获取类的名称,但这与我正在寻找的不同。我已经找到了一些与 java 相关的东西,但对于 dart 我还没有。也许我错过了一些东西。
举例来说,我有一个打印函数,如下所示:
class A {
void printSomethingAndTellWhereYouDidIt() {
// Here I would also include the class where this function is
// being called. For instance:
print('you called the function at: ...');
//This dot-dot-dot is where maybe should go what I'm looking for.
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
Run Code Online (Sandbox Code Playgroud)
输出应该类似于:
you called the function at: B
Run Code Online (Sandbox Code Playgroud)
请告诉我是否有办法实现这一目标。背后的想法是将其与记录器一起使用,例如logging包。先感谢您。
您可以StackTrace.current随时使用它来获取堆栈跟踪,这是发生异常时打印的对象。其中包含导致调用的调用链的行号,它应该提供您需要的信息。
class A {
void printSomethingAndTellWhereYouDidIt() {
print(StackTrace.current);
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您这样做是为了调试目的,您还可以设置一个断点printSomethingAndTellWhereYouDidIt来检查它是从哪里调用的。
| 归档时间: |
|
| 查看次数: |
2112 次 |
| 最近记录: |