This repository was archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathtracing.dart
186 lines (174 loc) · 4.6 KB
/
tracing.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Tracing for AngularDart framework and applications.
*
* The tracing API hooks up to either [WTF](http://google.github.io/tracing-framework/) or
* [Dart Observatory](https://www.dartlang.org/tools/observatory/).
*/
library angular.tracing;
import "dart:developer";
bool traceEnabled = false;
dynamic /* JsObject */ _trace;
dynamic /* JsObject */ _events;
dynamic /* JsFunction */ _createScope;
dynamic /* JsFunction */ _enterScope;
dynamic /* JsFunction */ _leaveScope;
dynamic /* JsFunction */ _beginTimeRange;
dynamic /* JsFunction */ _endTimeRange;
final List _arg1 = [null];
final List _arg2 = [null, null];
/**
* Use this method to detect if [WTF](http://google.github.io/tracing-framework/) has been enabled.
*
* To make sure that this library can be used DartVM where no JavaScript is available this
* method needs to be called with JavaScript context.
*
* If the method is not called or if WTF has not been detected that the tracing defaults to
* Dart Observatory.
*
* import "dart:js" show context;
*
* detectWTF(context);
*/
traceDetectWTF(dynamic /* JsObject */ context) {
if (context.hasProperty('wtf')) {
dynamic /* JsObject */ wtf = context['wtf'];
if (wtf.hasProperty('trace')) {
traceEnabled = true;
_trace = wtf['trace'];
_events = _trace['events'];
_createScope = _events['createScope'];
_enterScope = _trace['enterScope'];
_leaveScope = _trace['leaveScope'];
_beginTimeRange = _trace['beginTimeRange'];
_endTimeRange = _trace['endTimeRange'];
}
}
}
/**
* Create trace scope. Scopes must be strictly nested and are analogous to stack frames, but
* do not have to follow the stack frames. Instead it is recommended that they follow logical
* nesting.
*/
dynamic /* JsFunction */ traceCreateScope(signature, [flags]) {
if (traceEnabled) {
_arg2[0] = signature;
_arg2[1] = flags;
return _createScope.apply(_arg2, thisArg: _events);
} else {
return new UserTag(signature);
}
}
/**
* Used to mark scope entry.
*
* final myScope = traceCreateScope('myMethod');
*
* someMethod() {
* var s = traceEnter(myScope);
* try {
* // do something
* } finally {
* traceLeave(s);
* }
* }
*/
dynamic /* JsObject */ traceEnter(dynamic /* JsFunction */ scope) {
if (traceEnabled) {
return scope.apply(const []);
} else {
return scope.makeCurrent();
}
}
/**
* Used to mark scope entry which logs single argument.
*
* final myScope = traceCreateScope('myMethod');
*
* someMethod() {
* var s = traceEnter(myScope);
* try {
* // do something
* } finally {
* traceLeave(s);
* }
* }
*/
dynamic /* JsObject */ traceEnter1(dynamic /* JsFunction */ scope, arg1) {
if (traceEnabled) {
_arg1[0] = arg1;
return scope.apply(_arg1);
} else {
return scope.makeCurrent();
}
}
/**
* Used to mark scope exit.
*
* var myScope = traceCreateScope('myMethod');
*
* someMethod() {
* var s = traceEnter(myScope);
* try {
* // do something
* } finally {
* traceLeave(s);
* }
* }
*/
dynamic /* JsObject */ traceLeave(dynamic /* JsObject */ scope) {
if (traceEnabled) {
_arg1[0] = scope;
_leaveScope.apply(_arg1, thisArg: _trace);
} else {
scope.makeCurrent();
}
}
/**
* Used to mark scope exit.
*
* var myScope = traceCreateScope('myMethod');
*
* someMethod() {
* var s = traceEnter(myScope);
* try {
* // do something
* } finally {
* traceLeave(s);
* }
* }
*/
dynamic /* JsObject */ traceLeaveVal(dynamic /* JsObject */ scope, dynamic returnValue) {
if (traceEnabled) {
_arg2[0] = scope;
_arg2[1] = returnValue;
_leaveScope.apply(_arg2, thisArg: _trace);
} else {
scope.makeCurrent();
}
}
/**
* Used to mark Async start. Async are similar to scope but they don't have to be strictly nested.
* Async ranges only work if WTF has been enabled.
*
* someMethod() {
* var s = traceAsyncStart('HTTP:GET', 'some.url');
* var future = new Future.delay(5).then((_) {
* traceAsyncEnd(s);
* });
* }
*/
dynamic /* JsObject */ traceAsyncStart(String rangeType, String action) {
if (traceEnabled) {
_arg2[0] = rangeType;
_arg2[1] = action;
return _beginTimeRange.apply(_arg2, thisArg: _trace);
}
return null;
}
dynamic /* JsObject */ traceAsyncEnd(dynamic /* JsObject */ range) {
if (traceEnabled) {
_arg1[0] = range;
return _endTimeRange.apply(_arg1, thisArg: _trace);
}
return null;
}