OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import '../common.dart'; | 5 import '../common.dart'; |
6 import '../util/util.dart'; | 6 import '../util/util.dart'; |
7 | 7 |
8 /// Function signature for [trace]. | |
9 typedef void Trace(String message, | |
10 {bool condition(String stackTrace), int limit, bool throwOnPrint}); | |
11 | |
12 /** | 8 /** |
13 * Helper method for printing stack traces for debugging. | 9 * Helper method for printing stack traces for debugging. |
14 * | 10 * |
15 * [message] is printed as the header of the stack trace. | 11 * [message] is printed as the header of the stack trace. |
16 * | 12 * |
17 * If [condition] is provided, the stack trace is only printed if [condition] | 13 * If [condition] is provided, the stack trace is only printed if [condition] |
18 * returns [:true:] on the stack trace text. This can be used to filter the | 14 * returns [:true:] on the stack trace text. This can be used to filter the |
19 * printed stack traces based on their content. For instance only print stack | 15 * printed stack traces based on their content. For instance only print stack |
20 * traces that contain specific paths. | 16 * traces that contain specific paths. |
21 * | 17 * |
22 * If [limit] is provided, the stack trace is limited to [limit] entries. | 18 * If [limit] is provided, the stack trace is limited to [limit] entries. |
23 * | 19 * |
24 * If [throwOnPrint] is `true`, [message] will be thrown after the stack trace | 20 * If [throwOnPrint] is `true`, [message] will be thrown after the stack trace |
25 * has been printed. Together with [condition] this can be used to discover | 21 * has been printed. Together with [condition] this can be used to discover |
26 * unknown call-sites in tests by filtering known call-sites and throwning | 22 * unknown call-sites in tests by filtering known call-sites and throwning |
27 * otherwise. | 23 * otherwise. |
28 */ | 24 */ |
29 Trace get trace { | 25 (String message, {(String stackTrace) -> bool condition, int limit, |
| 26 bool throwOnPrint}) -> void |
| 27 get trace { |
30 enableDebugMode(); | 28 enableDebugMode(); |
31 return _trace; | 29 return _trace; |
32 } | 30 } |
33 | 31 |
34 void _trace(String message, | 32 void _trace(String message, |
35 {bool condition(String stackTrace), int limit, bool throwOnPrint: false}) { | 33 {(String stackTrace) -> bool condition, int limit, |
| 34 bool throwOnPrint: false}) { |
36 try { | 35 try { |
37 throw ''; | 36 throw ''; |
38 } catch (e, s) { | 37 } catch (e, s) { |
39 String stackTrace; | 38 String stackTrace; |
40 try { | 39 try { |
41 stackTrace = prettifyStackTrace(s, | 40 stackTrace = prettifyStackTrace(s, |
42 rangeStart: 1, rangeEnd: limit, filePrefix: stackTraceFilePrefix); | 41 rangeStart: 1, rangeEnd: limit, filePrefix: stackTraceFilePrefix); |
43 } catch (e) { | 42 } catch (e) { |
44 print(e); | 43 print(e); |
45 stackTrace = '$s'; | 44 stackTrace = '$s'; |
46 } | 45 } |
47 if (condition != null) { | 46 if (condition != null) { |
48 if (!condition(stackTrace)) return; | 47 if (!condition(stackTrace)) return; |
49 } | 48 } |
50 print('$message\n$stackTrace'); | 49 print('$message\n$stackTrace'); |
51 if (throwOnPrint) throw message; | 50 if (throwOnPrint) throw message; |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 /// Creates a function to use as an `condition` argument in [trace] that filters | 54 /// Creates a function to use as an `condition` argument in [trace] that filters |
56 /// stack traces that contains any of the [exceptions]. | 55 /// stack traces that contains any of the [exceptions]. |
57 traceExceptions(List<String> exceptions) { | 56 traceExceptions(List<String> exceptions) { |
58 return (String stackTrace) => !exceptions.any(stackTrace.contains); | 57 return (String stackTrace) => !exceptions.any(stackTrace.contains); |
59 } | 58 } |
60 | 59 |
61 /// Function signature of [traceAndReport]. | 60 /// Function signature of [traceAndReport]. |
62 typedef void TraceAndReport( | 61 typedef TraceAndReport = ( |
63 DiagnosticReporter reporter, Spannable node, String message, | 62 DiagnosticReporter, Spannable node, String message, |
64 {bool condition(String stackTrace), int limit, bool throwOnPrint}); | 63 {(String stackTrace) -> bool condition, int limit, bool throwOnPrint}) |
| 64 -> void; |
65 | 65 |
66 /// Calls [reportHere] and [trace] with the same message. | 66 /// Calls [reportHere] and [trace] with the same message. |
67 TraceAndReport get traceAndReport { | 67 TraceAndReport get traceAndReport { |
68 enableDebugMode(); | 68 enableDebugMode(); |
69 return _traceAndReport; | 69 return _traceAndReport; |
70 } | 70 } |
71 | 71 |
72 /// Calls [reportHere] and [trace] with the same message. | 72 /// Calls [reportHere] and [trace] with the same message. |
73 TraceAndReport get reportAndTrace => traceAndReport; | 73 TraceAndReport get reportAndTrace => traceAndReport; |
74 | 74 |
75 /// Implementation of [traceAndReport]. | 75 /// Implementation of [traceAndReport]. |
76 void _traceAndReport( | 76 void _traceAndReport( |
77 DiagnosticReporter reporter, Spannable node, String message, | 77 DiagnosticReporter reporter, Spannable node, String message, |
78 {bool condition(String stackTrace), int limit, bool throwOnPrint: false}) { | 78 {(String stackTrace) -> bool condition, int limit, |
| 79 bool throwOnPrint: false}) { |
79 trace(message, limit: limit, throwOnPrint: throwOnPrint, | 80 trace(message, limit: limit, throwOnPrint: throwOnPrint, |
80 condition: (String stackTrace) { | 81 condition: (String stackTrace) { |
81 bool result = condition != null ? condition(stackTrace) : true; | 82 bool result = condition != null ? condition(stackTrace) : true; |
82 if (result) { | 83 if (result) { |
83 reportHere(reporter, node, message); | 84 reportHere(reporter, node, message); |
84 } | 85 } |
85 return result; | 86 return result; |
86 }); | 87 }); |
87 } | 88 } |
88 | 89 |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 } | 340 } |
340 for (int index = text.length; index < intendedLength; index++) { | 341 for (int index = text.length; index < intendedLength; index++) { |
341 int dotsIndex = index % dotsLength; | 342 int dotsIndex = index % dotsLength; |
342 sb.write(dots.substring(dotsIndex, dotsIndex + 1)); | 343 sb.write(dots.substring(dotsIndex, dotsIndex + 1)); |
343 } | 344 } |
344 if (padLeft) { | 345 if (padLeft) { |
345 sb.write(text); | 346 sb.write(text); |
346 } | 347 } |
347 return sb.toString(); | 348 return sb.toString(); |
348 } | 349 } |
OLD | NEW |