-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebugout.js
319 lines (312 loc) · 9.66 KB
/
debugout.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// save all the console.logs
function debugout() {
var self = this;
// OPTIONS
self.realTimeLoggingOn = true; // log in real time (forwards to console.log)
self.useTimestamps = false; // insert a timestamp in front of each log
self.useLocalStorage = false; // store the output using window.localStorage() and continuously add to the same log each session
self.recordLogs = true; // set to false after you're done debugging to avoid the log eating up memory
self.autoTrim = true; // to avoid the log eating up potentially endless memory
self.maxLines = 2500; // if autoTrim is true, this many most recent lines are saved
self.tailNumLines = 100; // how many lines tail() will retrieve
self.logFilename = 'log.txt'; // filename of log downloaded with downloadLog()
self.maxDepth = 25; // max recursion depth for logged objects
// vars
self.depth = 0;
self.parentSizes = [0];
self.currentResult = '';
self.startTime = new Date();
self.output = '';
this.version = function() { return '0.5.0' }
/*
USER METHODS
*/
this.getLog = function() {
var retrievalTime = new Date();
// if recording is off, so dev knows why they don't have any logs
if (!self.recordLogs) {
self.log('[debugout.js] log recording is off.');
}
// if using local storage, get values
if (self.useLocalStorage) {
var saved = window.localStorage.getItem('debugout.js');
if (saved) {
saved = JSON.parse(saved);
self.startTime = new Date(saved.startTime);
self.output = saved.log;
retrievalTime = new Date(saved.lastLog);
}
}
return self.output
+ '\n---- Log retrieved: '+retrievalTime+' ----\n'
+ self.formatSessionDuration(self.startTime, retrievalTime);
}
// accepts optional number or uses the default for number of lines
this.tail = function(numLines) {
var numLines = numLines || self.tailLines;
return self.trimLog(self.getLog(), numLines);
}
// accepts a string to search for
this.search = function(string) {
var lines = self.output.split('\n');
var rgx = new RegExp(string);
var matched = [];
// can't use a simple Array.prototype.filter() here
// because we need to add the line number
for (var i = 0; i < lines.length; i++) {
var addr = '['+i+'] ';
if (lines[i].match(rgx)) {
matched.push(addr + lines[i]);
}
}
var result = matched.join('\n');
if (result.length == 0) result = 'Nothing found for "'+string+'".';
return result
}
// accepts the starting line and how many lines after the starting line you want
this.getSlice = function(lineNumber, numLines) {
var lines = self.output.split('\n');
var segment = lines.slice(lineNumber, lineNumber + numLines);
return segment.join('\n');
}
// immediately downloads the log - for desktop browser use
this.downloadLog = function() {
var file = "data:text/plain;charset=utf-8,";
var logFile = self.getLog();
var encoded = encodeURIComponent(logFile);
file += encoded;
var a = document.createElement('a');
a.href = file;
a.target = '_blank';
a.download = self.logFilename;
document.body.appendChild(a);
a.click();
a.remove();
}
// clears the log
this.clear = function() {
var clearTime = new Date();
self.output = '---- Log cleared: '+clearTime+' ----\n';
if (self.useLocalStorage) {
// local storage
var saveObject = {
startTime: self.startTime,
log: self.output,
lastLog: clearTime
}
saveObject = JSON.stringify(saveObject);
window.localStorage.setItem('debugout.js', saveObject);
}
if (self.realTimeLoggingOn) console.log('[debugout.js] clear()');
}
// records a log
this.log = function(obj) {
// log in real time
if (self.realTimeLoggingOn) console.log(obj);
// record log
var type = self.determineType(obj);
if (type != null && self.recordLogs) {
var addition = self.formatType(type, obj);
// timestamp, formatted for brevity
if (self.useTimestamps) {
var logTime = new Date();
self.output += self.formatTimestamp(logTime);
}
self.output += addition;
if (self.autoTrim) self.output = self.trimLog(self.output, self.maxLines);
// local storage
if (self.useLocalStorage) {
var last = new Date();
var saveObject = {
startTime: self.startTime,
log: self.output,
lastLog: last
}
saveObject = JSON.stringify(saveObject);
window.localStorage.setItem('debugout.js', saveObject);
}
}
self.depth = 0;
self.parentSizes = [0];
self.currentResult = '';
}
/*
METHODS FOR CONSTRUCTING THE LOG
*/
// like typeof but classifies objects of type 'object'
// kept separate from formatType() so you can use at your convenience!
this.determineType = function(object) {
if (object != null) {
var typeResult;
var type = typeof object;
if (type == 'object') {
var len = object.length;
if (len == null) {
if (typeof object.getTime == 'function') {
typeResult = 'Date';
}
else if (typeof object.test == 'function') {
typeResult = 'RegExp';
}
else {
typeResult = 'Object';
}
} else {
typeResult = 'Array';
}
} else {
typeResult = type;
}
return typeResult;
} else {
return null;
}
}
// format type accordingly, recursively if necessary
this.formatType = function(type, obj) {
if (self.maxDepth && self.depth >= self.maxDepth) {
return '... (max-depth reached)';
}
switch(type) {
case 'Object' :
self.currentResult += '{\n';
self.depth++;
self.parentSizes.push(self.objectSize(obj));
var i = 0;
for (var prop in obj) {
self.currentResult += self.indentsForDepth(self.depth);
self.currentResult += prop + ': ';
var subtype = self.determineType(obj[prop]);
var subresult = self.formatType(subtype, obj[prop]);
if (subresult) {
self.currentResult += subresult;
if (i != self.parentSizes[self.depth]-1) self.currentResult += ',';
self.currentResult += '\n';
} else {
if (i != self.parentSizes[self.depth]-1) self.currentResult += ',';
self.currentResult += '\n';
}
i++;
}
self.depth--;
self.parentSizes.pop();
self.currentResult += self.indentsForDepth(self.depth);
self.currentResult += '}';
if (self.depth == 0) return self.currentResult;
break;
case 'Array' :
self.currentResult += '[';
self.depth++;
self.parentSizes.push(obj.length);
for (var i = 0; i < obj.length; i++) {
var subtype = self.determineType(obj[i]);
if (subtype == 'Object' || subtype == 'Array') self.currentResult += '\n' + self.indentsForDepth(self.depth);
var subresult = self.formatType(subtype, obj[i]);
if (subresult) {
self.currentResult += subresult;
if (i != self.parentSizes[self.depth]-1) self.currentResult += ', ';
if (subtype == 'Array') self.currentResult += '\n';
} else {
if (i != self.parentSizes[self.depth]-1) self.currentResult += ', ';
if (subtype != 'Object') self.currentResult += '\n';
else if (i == self.parentSizes[self.depth]-1) self.currentResult += '\n';
}
}
self.depth--;
self.parentSizes.pop();
self.currentResult += ']';
if (self.depth == 0) return self.currentResult;
break;
case 'function' :
obj += '';
var lines = obj.split('\n');
for (var i = 0; i < lines.length; i++) {
if (lines[i].match(/\}/)) self.depth--;
self.currentResult += self.indentsForDepth(self.depth);
if (lines[i].match(/\{/)) self.depth++;
self.currentResult += lines[i] + '\n';
}
return self.currentResult;
break;
case 'RegExp' :
return '/'+obj.source+'/';
break;
case 'Date' :
case 'string' :
if (self.depth > 0 || obj.length == 0) {
return '"'+obj+'"';
} else {
return obj;
}
case 'boolean' :
if (obj) return 'true';
else return 'false';
case 'number' :
return obj+'';
break;
}
}
this.indentsForDepth = function(depth) {
var str = '';
for (var i = 0; i < depth; i++) {
str += '\t';
}
return str;
}
this.trimLog = function(log, maxLines) {
var lines = log.split('\n');
if (lines.length > maxLines) {
lines = lines.slice(lines.length - maxLines);
}
return lines.join('\n');
}
this.lines = function() {
return self.output.split('\n').length;
}
// calculate testing time
this.formatSessionDuration = function(startTime, endTime) {
var msec = endTime - startTime;
var hh = Math.floor(msec / 1000 / 60 / 60);
var hrs = ('0' + hh).slice(-2);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
var mins = ('0' + mm).slice(-2);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
var secs = ('0' + ss).slice(-2);
msec -= ss * 1000;
return '---- Session duration: '+hrs+':'+mins+':'+secs+' ----'
}
this.formatTimestamp = function(timestamp) {
var year = timestamp.getFullYear();
var date = timestamp.getDate();
var month = ('0' + (timestamp.getMonth() +1)).slice(-2);
var hrs = Number(timestamp.getHours());
var mins = ('0' + timestamp.getMinutes()).slice(-2);
var secs = ('0' + timestamp.getSeconds()).slice(-2);
return '['+ year + '-' + month + '-' + date + ' ' + hrs + ':' + mins + ':'+secs + ']: ';
}
this.objectSize = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
}
/*
START/RESUME LOG
*/
if (self.useLocalStorage) {
var saved = window.localStorage.getItem('debugout.js');
if (saved) {
saved = JSON.parse(saved);
self.output = saved.log;
var start = new Date(saved.startTime);
var end = new Date(saved.lastLog);
self.output += '\n---- Session end: '+saved.lastLog+' ----\n';
self.output += self.formatSessionDuration(start, end);
self.output += '\n\n';
}
}
self.output += '---- Session started: '+self.startTime+' ----\n\n';
}