-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtrace.js
91 lines (80 loc) · 2.88 KB
/
trace.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
var functionLogger = {};
functionLogger.log = true;//Set this to false to disable logging
/**
* Gets a function that when called will log information about itself if logging is turned on.
*
* @param func The function to add logging to.
* @param name The name of the function.
*
* @return A function that will perform logging and then call the function.
*/
functionLogger.getLoggableFunction = function(func, name) {
return function() {
if (functionLogger.log) {
var logText = name + '(';
for (var i = 0; i < arguments.length; i++) {
if (i > 0) {
logText += ', ';
}
logText += JSON.stringify(arguments[i]);
}
logText += ');';
console.trace();
console.log(logText);
}
return func.apply(this, arguments);
}
};
/**
* After this is called, all direct children of the provided namespace object that are
* functions will log their name as well as the values of the parameters passed in.
*
* @param namespaceObject The object whose child functions you'd like to add logging to.
*/
functionLogger.addLoggingToNamespace = function(namespaceObject){
for(var name in namespaceObject){
var potentialFunction = namespaceObject[name];
if(Object.prototype.toString.call(potentialFunction) === '[object Function]'){
Object.defineProperty(namespaceObject, name, {
writable: true,
value: functionLogger.getLoggableFunction(potentialFunction, name) });
}
}
};
functionLogger.accProxy = function(obj, name) {
return new Proxy(obj, { get: function(obj, prop) {
console.trace();
if (typeof prop !== 'symbol') {
console.log(`get ${name}.${prop}`)
} else {
console.log('get symbol');
}
return Reflect.get(...arguments);
},
set: function(obj, prop, value) {
console.trace();
if (typeof prop !== 'symbol'&&typeof value !== 'symbol') {
console.log(`set ${name}.${prop}=${value}`)
} else {
console.log(`set ${name}.symbol=???`);
}
return Reflect.set(...arguments);
},
})
}
functionLogger.addAccessLoggingToNamespace = function(namespaceObject, namespace) {
for(var name in namespaceObject){
try {
// console.log(Object.prototype.toString.call(namespaceObject[name]))
if(Object.prototype.toString.call(namespaceObject[name]) !== '[object Function]'){
Object.defineProperty(namespaceObject, name,
{
writable: true,
value: functionLogger.accProxy(namespaceObject[name], `${namespace}.${name}`)
}
)
}
} catch{}
}
};
export default functionLogger;