-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.js
167 lines (144 loc) · 4.82 KB
/
Logger.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
"use strict";
var Logger = class Logger {
/**
* Get the singleton object
* @return {Logger}
*/
static get _ () {
if (Logger._singleton === undefined)
Logger._ = new Logger();
return Logger._singleton;
}
/**
* Set the singleton object
* @param {Logger}
*/
static set _ (singleton) {
Logger._singleton = singleton;
}
/**
* Instantiate the Logger
* @constructor
* @this {Logger}
*/
constructor () {
Logger.ACTIVE = true;
Logger.MODULES = {
ROOM: false,
ROOMPOSITION: false,
};
Logger.indentation = ["", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "];
Logger.level = 0;
}
/**
* Apply the wrapper to selected functions
*/
init () {
if (Logger.ACTIVE && this._init !== true) {
this._init = true;
var methods = [];
if (Logger.MODULES.ROOM) {
this.wrap('Room', Room, 'find');
this.wrap('Room', Room, 'findPath');
}
if (Logger.MODULES.ROOMPOSITION) {
this.wrap('RoomPosition', RoomPosition, 'findPathTo');
this.wrap('RoomPosition', RoomPosition, 'findClosestByPath');
}
}
}
/**
* wrap function of class c
* @param {String} class name
* @param {Object} class object
* @param {String} method name
*/
wrap (LoggerName, c, method) {
if (Logger.ACTIVE) {
var f = c.prototype[method];
c.prototype[method] = function() {
Logger.functionEnter(LoggerName + '.' + method);
var tStart = Game.cpu.getUsed();
var returnValue = f.apply(this, arguments);
var tReq = Game.cpu.getUsed() - tStart;
Logger.functionExit(LoggerName + '.' + method, + tReq);
return returnValue;
};
}
}
/**
* log a message
* @param {String} msg
*/
static functionEnter (name) {
Logger.log( '--> ' + name );
Logger.level ++;
}
/**
* log a message
* @param {String} msg
*/
static functionExit (name, tReq) {
Logger.level --;
Logger.log( '<-- ' + name + ' [' + tReq.toFixed(2) + '] ');
}
/**
* log a message
* @param {String} msg
*/
static log (msg) {
console.log( Logger.indentation[Logger.level] + msg );
}
/**
* log a message
* @param {String} msg
*/
static logError (msg) {
console.log( Logger.indentation[Logger.level] + 'ERROR: ' + msg );
}
/**
* log a object
* @param {String} obj
*/
static logDebug (obj) {
console.log( Logger.indentation[Logger.level] + 'DEBUG: ' + JSON.stringifyOnce(obj) );
}
};
JSON.stringifyOnce = function(obj, replacer, indent){
var printedObjects = [];
var printedObjectKeys = [];
function printOnceReplacer(key, value){
if ( printedObjects.length > 2000){ // browsers will not print more than 20K, I don't see the point to allow 2K.. algorithm will not be fast anyway if we have too many objects
return 'object too long';
}
var printedObjIndex = false;
printedObjects.forEach(function(obj, index){
if(obj===value){
printedObjIndex = index;
}
});
if ( key == ''){ //root element
printedObjects.push(obj);
printedObjectKeys.push("root");
return value;
}
else if(printedObjIndex+"" != "false" && typeof(value)=="object"){
if ( printedObjectKeys[printedObjIndex] == "root"){
return "(pointer to root)";
}else{
return "(see " + ((!!value && !!value.constructor) ? value.constructor.name.toLowerCase() : typeof(value)) + " with key " + printedObjectKeys[printedObjIndex] + ")";
}
}else{
var qualifiedKey = key || "(empty key)";
printedObjects.push(value);
printedObjectKeys.push(qualifiedKey);
if(replacer){
return replacer(key, value);
}else{
return value;
}
}
}
return JSON.stringify(obj, printOnceReplacer, indent);
};
module.exports = Logger;