【问题标题】:Is there support for predefined macros in DART是否支持 DART 中的预定义宏
【发布时间】:2018-08-04 17:48:10
【问题描述】:

DART 是否支持预定义的宏,例如:

__LINE__

__FUNCTION__

询问的原因是转换器 DART2JS 使控制台日志没有用,因为所有日志都显示:js_primitives.dart:30

[更新基础]

使用转换器 dart2js 时,print("hello world"); 会导致:

JS('void', r'console.log(#)', "hello world);

从函数调用:printString(String string) 驻留在图书馆 dart2js._js_primitives

这导致console.log 消息总是一遍又一遍地包含相同的行号,无论在DART 代码中使用print(); 的任何位置。 (因为 console.log 会自动将文件名和行号添加到位于 dart2js._js_primitives 中的包装函数的控制台显示中) 由于当前在 console.log 消息中添加文件名和行号的实现是无用的,如果有另一种方法可以显示附加信息,那就太好了。

例如,print("hello world" __FUNCTION__ __LINE__); 会产生更有用的附加调试信息。

【问题讨论】:

  • 不,没有,但我不知道现有(或不)如何适用于您关于控制台日志记录的问题。你可以试着问一下你到底想看到什么改变吗?
  • 刚刚在原始问题中添加了更多文字。

标签: dart angular-dart dart-webui


【解决方案1】:

你可以使用

void main() {
  print(StackTrace.current);
}

以获得有关错误来源的更好信息

DartPad example

您还可以在自定义区域中运行代码并为该区域定义自定义打印方法。另见https://api.dartlang.org/stable/1.24.3/dart-async/Zone/print.html

【讨论】:

  • 您好,Gunter,刚刚尝试了您的建议。它会产生大量信息。实际上在第二行它打印文件名和行号。 Object.StackTrace_current (core_patch.dart:680) at testCase_readWrite.dart:59 at _wrapJsFunctionForAsync_closure.$protected (async_patch.dart:213) 出错 ... ...
  • 您可以使用stack_tracestackoverflow.com/a/29013592/217408 中的terse 函数来降低噪音。它可能会导致一些内存使用和性能开销,但我发现它通常对调试很有用。
  • 通过使用:print(new Trace.from(StackTrace.current).terse); 将给出:main.dart.js 13338:40 Object.StackTrace_current main.dart.js 68378:44 <fn> main.dart.js 6883:15 _wrapJsFunctionForAsync_closure.$protected 它剥离了函数名
  • 使用:print(new Trace.from(StackTrace.current).vmTrace); 结果:#1 Object.StackTrace_current (http://localhost:64250/main.dart.js:13338:40) #2 <anonymous closure> (http://localhost:64250/main.dart.js:68380:44) #3 _wrapJsFunctionForAsync_closure.$protected (http://localhost:64250/main.dart.js:6883:15) 它还删除了函数名。
  • 使用:print(new Trace.from(StackTrace.current).original); 会给我最好的结果。 Error at Object.StackTrace_current (core_patch.dart:680) at testCase_readWrite.dart:66 at _wrapJsFunctionForAsync_closure.$protected (async_patch.dart:213)
【解决方案2】:

您正在寻找的似乎是 dart2js 创建的源映射,其中包含从 javascript 位置重新创建原始 dart 文件中的行号所需的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多