-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
94 lines (85 loc) · 2.61 KB
/
utils.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
var Utils = {
errorLogger: function (f) {
return function (args) {
try {
f.apply(this, arguments);
}
catch (e) {
console.error(Utils.formatException(e));
throw e;
}
};
},
handleUndefinedCallback: function (callback) {
if (callback) {
return function (args) {
callback.apply(this, arguments);
}
}
else {
return function () {};
}
},
formatException: function (e) {
if (e.stack) {
return e.stack;
}
return e;
},
argumentsToArray: function (input) {
return Array.prototype.slice.call(input);
},
printObject: function (obj, indent) {
var result = JSON.stringify(obj, null, 2);
if (result === undefined) {
return result;
}
return result.split('\n').join('\n' + indent);
},
printTime: function (date) {
return date.getHours().pad(2) + ":" + date.getMinutes().pad(2) + ":" + date.getSeconds().pad(2) + "." +
date.getMilliseconds().pad(3);
},
instrument: function (obj, objName, options) {
options = options || {};
if (options.defaultMode === undefined) {
options.defaultMode = 'include';
}
if (options.methods === undefined) {
options.methods = {};
}
for (var name in Utils.getMethods(obj)) {
var methodOptions = options.methods[name];
if (!methodOptions) {
methodOptions = {};
}
if (methodOptions) {
if (methodOptions.mode === undefined) {
methodOptions.mode = options.defaultMode;
}
if (methodOptions.logSubTree === undefined) {
methodOptions.logSubTree = true;
}
if (methodOptions.logOwnCall === undefined) {
methodOptions.logOwnCall = true;
}
}
if (methodOptions.mode === 'include') {
obj[name] = LoggingDecorator.create(
objName + '.' + name, obj[name], methodOptions.logSubTree, methodOptions.logOwnCall);
}
}
},
getMethods: function (obj) {
var result = { };
for (var name in obj) {
try {
if (typeof(obj[name]) == "function") {
result[name] = obj[name];
}
} catch (err) {
}
}
return result;
}
};