-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstracer.js
42 lines (36 loc) · 1.26 KB
/
jstracer.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
(function () {
// external
trace = function(object, functionName) {
var oldFunction = object[functionName];
if (!oldFunction || oldFunction.__tracer__) return null;
object[functionName] = makeTracer(object, functionName);
object[functionName].__original_function__ = oldFunction;
}
// external
untrace = function(object, functionName) {
if (!object[functionName] ||
!object[functionName].__tracer__) return null;
object[functionName] = object[functionName].__original_function__;
}
function makeTracer(object, functionName) {
var oldFunction = object[functionName];
var result = function() {
indent.indent = indent.indent + 1;
console.log(indent() + "[" + functionName + "]");
try {
oldFunction.apply(object, arguments);
console.log(indent() + "[/" + functionName + "]");
} finally {
indent.indent = indent.indent - 1;
}
}
result.__tracer__ = true;
return result;
}
function indent() {
var s = "";
for (var i = 1; i < indent.indent; i += 1) s += " ";
return s;
}
indent.indent = 0;
})();