-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3.inlog.js
241 lines (215 loc) · 7.03 KB
/
d3.inlog.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(function() {
var att, createReplacementFunction, defaults, inject, isFunction, logFunctionCall, logFunctionCallAlt, logTrace, maintrace, settings, subtrace, thisType, tracedepth;
defaults = {
enabled: false,
maxDepth: 10,
indent: true,
thisValue: true,
returnValue: true
};
settings = {};
for (att in defaults) {
if (defaults[att] != null) settings[att] = defaults[att];
}
maintrace = null;
subtrace = null;
tracedepth = 0;
isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};
d3.inlog = function(a, obj) {
var att, _results;
if (a === true || a === false) {
if (obj != null) {
return inject(obj);
} else {
return settings.enabled = a;
}
} else {
_results = [];
for (att in a) {
if (a[att] != null) _results.push(settings[att] = a[att]);
}
return _results;
}
};
/*
# Outputs a function call with all parameters passed.
#
# @param funcName A human readable name of the function called
# @param origArguments The original "arguments" property inside the function
# @param origReturn The original return value of the function
# @param origThis The original context the function was running in
# @returns undefined
*/
logFunctionCall = function(funcName, origArguments, origReturn, origThis) {
var formatString, i, paramFormatStrings, params;
if (funcName === 'inlog') return;
params = [];
paramFormatStrings = [];
formatString = "";
i = 0;
while (i < origArguments.length) {
if (origArguments[i] === undefined) break;
params.push(origArguments[i]);
paramFormatStrings.push("%o");
i++;
}
if (settings.thisValue) {
formatString = "(%o) # ";
params.unshift(origThis);
}
params.unshift(formatString + funcName + "(" + paramFormatStrings.join(", ") + ")");
if (settings.returnValue) {
params[0] += " => %o";
params.push(origReturn);
}
return console.log.apply(console, params);
};
logFunctionCallAlt = function(funcName, origArguments, origReturn, origThis, trace) {
var formatString, i, package_name, paramFormatStrings, params;
if (funcName === 'inlog') return;
params = [];
paramFormatStrings = [];
formatString = "";
i = 0;
package_name = false;
console.groupCollapsed("" + (thisType(origThis)) + ": " + funcName);
if (settings.thisValue) {
console.groupCollapsed('this');
console.log.apply(console, ["%o", origThis]);
console.groupEnd();
}
while (i < origArguments.length) {
if (origArguments[i] === undefined) break;
params.push(origArguments[i]);
paramFormatStrings.push("%o");
i++;
}
params.unshift(formatString + funcName + "(" + paramFormatStrings.join(", ") + ")");
console.group('params');
console.log.apply(console, params);
console.groupEnd();
if (settings.returnValue) {
console.group('returns');
console.log.apply(console, ["%o", origReturn]);
console.groupEnd();
}
if (trace["sub"].length) {
if (settings.indent) console.groupCollapsed('subtraces');
i = 0;
while (i < trace["sub"].length) {
logTrace(trace["sub"][i]);
i++;
}
if (settings.indent) console.groupEnd();
}
console.log.apply(console, ["trace depth: %i", trace['tracedepth']]);
return console.groupEnd(funcName);
};
thisType = function(_this) {
if (_this === window) return 'window';
if (_this === d3) return 'd3';
if (_this === d3.scale) return 'd3.scale';
if (_this === d3.csv) return 'd3.csv';
if ((_this.domain != null) && (_this.range != null) && (_this.ticks != null)) {
return 'd3.scale.linear';
}
if ((_this.classed != null) && (_this.data != null) && (_this.enter != null)) {
return 'd3.selection(update)';
}
if ((_this.classed != null) && (_this.data != null)) return 'd3.selection';
if ((_this.delay != null) && (_this.duration != null)) return 'd3.transition';
return 'Function Call';
};
/*
# Outputs the stack trace to console.
# Basically simple tree traversing.
#
# @param trace The object with the trace info
# @returns undefined
*/
logTrace = function(trace) {
return logFunctionCallAlt(trace["function"], trace["arguments"], trace["return"], trace["this"], trace);
};
/*
# Creates a Function which calls the "origFunction"
# and logs the call with the function as called "funcName".
#
# @param funcName The name of the original function. Human readable.
# @param origFunction A reference to the original function getting wrapped.
# @returns A function, which calls the original function sourended by log calls.
*/
createReplacementFunction = function(funcName, origFunction) {
return function() {
var isFirst, parenttrace, ret, _trace;
if (settings.enabled === false) return origFunction.apply(this, arguments);
if (settings.maxDepth !== -1 && tracedepth > settings.maxDepth) {
return origFunction.apply(this, arguments);
}
_trace = {
"function": funcName,
"this": this,
arguments: arguments,
sub: [],
tracedepth: tracedepth
};
parenttrace = void 0;
isFirst = tracedepth === 0;
tracedepth++;
if (isFirst) {
maintrace = subtrace = _trace;
} else {
parenttrace = subtrace;
subtrace["sub"].push(_trace);
subtrace = _trace;
}
ret = origFunction.apply(this, arguments);
if (isFunction(ret)) inject(ret);
_trace["return"] = ret;
if (isFirst) {
tracedepth = 0;
logTrace(maintrace);
} else {
subtrace = parenttrace;
}
return ret;
};
};
/*
# Injects log calls inside some functions of "obj"
# depending on the "list" and "inverted" parameter.
#
# If "inverted" is true, only the props inside "list" are considered.
# If "inverted" is not true, all props inside "list" are ignored.
#
# @param obj An object which should get each function replaced.
# @param list An optional array of strings with props to consider.
# @param inverted An optional boolean indicating how "list" is to be interpreted.
# @returns undefined
*/
inject = function(obj, list, inverted) {
var prop, _results;
if (list == null) list = [];
if (inverted == null) inverted = false;
list = "," + (list || []).join(",") + ",";
if ((obj != null ? obj.__inlog__ : void 0) != null) return;
if (obj != null) obj.__inlog__ = true;
_results = [];
for (prop in obj) {
_results.push((function(prop) {
if (obj.hasOwnProperty(prop) && isFunction(obj[prop]) && (list.indexOf("," + prop + ",") !== -1) === inverted) {
return obj[prop] = createReplacementFunction(prop, obj[prop]);
} else {
}
})(prop));
}
return _results;
};
inject(d3.selection.prototype);
inject(d3.transition.prototype);
inject(d3.scale);
inject(d3.csv);
inject(d3.json);
inject(d3, ['csv', 'json']);
}).call(this);