3

Does DART support predefined macros such as:

__LINE__

or

__FUNCTION__

The reason for asking is that the transformer DART2JS makes the console log not useful as all the logs shows: js_primitives.dart:30

[update BasE]

When using the transformer dart2js, print("hello world"); will result in:

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

to be invoked from function: printString(String string) residing in the library dart2js._js_primitives

This results that the console.log message always contains the same line number over and over again wherever in the DART code a print(); is used. (As console.log will add automatically the filename and line-number to the console display of the wrapper function residing in dart2js._js_primitives) As the current implementation of adding file-name and line-number to the console.log message is useless, it would have been nice if there would be another method that allows to display additional information.

As example, print("hello world" __FUNCTION__ __LINE__); would result in additional debug information that can be more useful.

Bas E
  • 96
  • 1
  • 7
  • 2
    No, there is not, but I don't see how that existing (or not) applies to your question about console logging. Can you try asking what exactly you want to see changed instead? – matanlurey Feb 24 '18 at 23:46
  • Just added more text to the original question. – Bas E Feb 25 '18 at 08:46

2 Answers2

2

You could use

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

to get better information about the source of the error

DartPad example

You can also run your code in a custom zone and define a custom print method for that zone. See also https://api.dartlang.org/stable/1.24.3/dart-async/Zone/print.html

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Hi Gunter, Just tried your suggestion. It generates lots of information. and indeed on the second line it prints the filename and line number. Error at Object.StackTrace_current (core_patch.dart:680) at testCase_readWrite.dart:59 at _wrapJsFunctionForAsync_closure.$protected (async_patch.dart:213) ... ... – Bas E Feb 25 '18 at 17:29
  • You might be able to reduce the noise using the `terse` function from the `stack_trace` package https://stackoverflow.com/a/29013592/217408. It might cause some overhead in memory usage and performance, but I find it often useful for debugging. – Günter Zöchbauer Feb 25 '18 at 17:34
  • By using: `print(new Trace.from(StackTrace.current).terse);` will give: `main.dart.js 13338:40 Object.StackTrace_current main.dart.js 68378:44 main.dart.js 6883:15 _wrapJsFunctionForAsync_closure.$protected` It strips out the function name – Bas E Feb 25 '18 at 17:54
  • Using: `print(new Trace.from(StackTrace.current).vmTrace);` results in: `#1 Object.StackTrace_current (http://localhost:64250/main.dart.js:13338:40) #2 (http://localhost:64250/main.dart.js:68380:44) #3 _wrapJsFunctionForAsync_closure.$protected (http://localhost:64250/main.dart.js:6883:15)` It also removed the function name. – Bas E Feb 25 '18 at 17:57
  • 1
    Using: `print(new Trace.from(StackTrace.current).original);` will give the best result for me. `Error at Object.StackTrace_current (core_patch.dart:680) at testCase_readWrite.dart:66 at _wrapJsFunctionForAsync_closure.$protected (async_patch.dart:213)` – Bas E Feb 25 '18 at 17:58
0

Seems what you are looking for is source maps that dart2js creates, that contain the information needed to recreate line numbers in original dart files, from the javascript locations.

rkj
  • 8,787
  • 2
  • 29
  • 35