-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug-console.js
544 lines (436 loc) · 17.4 KB
/
debug-console.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// DebugConsole - outputs debug messages to special page debug console
/*global IR*/
function RingBuffer(length) {
this.buffer = [];
this.length = 0;
this.pointer = 0;
this.push = function (item) {
this.buffer[this.pointer] = item;
this.pointer = (this.pointer + 1) % length;
this.length = this.buffer.length;
return this.length;
};
this.get = function (key) {
if (typeof key === 'undefined') {
return this.buffer[this.pointer - 1];
}
var index = (this.pointer + key) % this.length;
return this.buffer[index];
};
this.clear = function () {
this.buffer.length = 0;
this.length = 0;
this.pointer = 0;
};
}
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var maxCount = str.length * count;
count = Math.floor(Math.log(count) / Math.log(2));
while (count) {
str += str;
count--;
}
str += str.substring(0, maxCount - str.length);
return str;
};
}
function DebugConsole(options) {
this.lineCount = (options && options.lineCount) ? options.lineCount : 25;
this.defaultPageName = (options) ? options.defaultPage : null;
this.debugPageName = options ? options.debugPage : null;
this.noConsoleLog = (options) ? options.noConsoleLog : false;
this.debugPage = IR.GetPopup(this.debugPageName);
this.isPopup = !!this.debugPage;
this._eventCallbacks = {};
if (!this.isPopup) {
this.debugPage = IR.GetPage(this.debugPageName);
}
this.fieldSizes = {
timestamp : 25,
event : 9,
source : 10
};
this.consoleDisabled = false;
this.active = false;
this.messages = new RingBuffer((options && options.maxBufferSize) ? options.maxBufferSize : 1024);
this.lastPage = null;
this.eventFilter = {};
this.sourceFilter = {};
this.fieldFilter = {};
this.columnsCount = undefined;
var that = this;
this.registerListener = function (cb, data) {
var button = this.debugPage.GetItem(data.itemName);
if (button) {
IR.AddListener(IR.EVENT_ITEM_RELEASE, button, cb, data);
}
return this;
};
this.unregisterListener = function (cb, data) {
var button = this.debugPage.GetItem(data.itemName);
if (button) {
IR.RemoveListener(IR.EVENT_ITEM_RELEASE, button, cb);
}
return this;
};
this.on = function(event, callback) {
if (this._eventCallbacks[event]) {
throw new Error('Callback for this event is already set: ' + event);
}
this._eventCallbacks[event] = callback;
return this;
};
this.callEvent = function(/* event, arg1, arg2 ...*/) {
var args = Array.prototype.slice.call(arguments, 0);
var event = args.shift();
if (this._eventCallbacks[event]) {
this._eventCallbacks[event].apply(this, args);
}
};
if (this.debugPage) {
IR.AddListener(IR.EVENT_ITEM_SHOW, this.debugPage, onConsoleShow);
IR.AddListener(IR.EVENT_ITEM_HIDE, this.debugPage, onConsoleHide);
this.registerListener(onClearItemPressed, {itemName: 'Clear'});
this.registerListener(onGoBackItemPressed, {itemName: 'GoBack'});
this.registerListener(onPlayItemPressed, {itemName: 'PlayPause'});
this.registerListener(onItemPressed, {itemName: 'ShowDebug', path: ['eventFilter', 'debug']});
this.registerListener(onItemPressed, {itemName: 'ShowInfo', path: ['eventFilter', 'info']});
this.registerListener(onItemPressed, {itemName: 'ShowWarning', path: ['eventFilter', 'warning']});
this.registerListener(onItemPressed, {itemName: 'ShowError', path: ['eventFilter', 'error']});
this.registerListener(onItemPressed, {itemName: 'ShowTimestamp', path: ['fieldFilter', 'timestamp']});
this.registerListener(onItemPressed, {itemName: 'ShowEvent', path: ['fieldFilter', 'event']});
this.registerListener(onItemPressed, {itemName: 'ShowSource', path: ['fieldFilter', 'source']});
this.registerListener(onItemPressed, {itemName: 'ShowMessage', path: ['fieldFilter', 'message']});
}
function onConsoleShow(notChangeActive) {
if (!notChangeActive) {
that.active = true;
}
if (that.active) {
that.updateConsole();
initButtonValue(that.debugPage, 'PlayPause', that.active);
initButtonValue(that.debugPage, 'ShowDebug', !that.eventFilter.debug);
initButtonValue(that.debugPage, 'ShowInfo', !that.eventFilter.info);
initButtonValue(that.debugPage, 'ShowWarning', !that.eventFilter.warning);
initButtonValue(that.debugPage, 'ShowError', !that.eventFilter.error);
initButtonValue(that.debugPage, 'ShowTimestamp', !that.fieldFilter.timestamp);
initButtonValue(that.debugPage, 'ShowEvent', !that.fieldFilter.event);
initButtonValue(that.debugPage, 'ShowSource', !that.fieldFilter.source);
initButtonValue(that.debugPage, 'ShowMessage', !that.fieldFilter.message);
}
}
function onConsoleHide() {
that.active = false;
}
function onGoBackItemPressed() {
that.hideConsole();
}
function onClearItemPressed() {
that.clear();
that.updateConsole();
}
/** @this onPlayItemPressed */
function onPlayItemPressed() {
var button = that.debugPage.GetItem(this.itemName);
that.active = button.Value;
}
/** @this onItemPressed */
function onItemPressed() {
// К this привязана структура {item : button, obj : objName, prop: propName}
var button = that.debugPage.GetItem(this.itemName);
var len = this.path.length;
if (button) {
var object = that;
for (var i = 0; i < len - 1; i++) {
object = object[this.path[i]];
}
object[this.path[len - 1]] = !button.Value;
if (len > 1) { // Это означает, что нажата кнопка фильтров
that.callEvent('settings', this.path[len - 2], this.path[len - 1], !button.Value);
}
}
that.updateConsole();
}
this.kill = function () {
this.clear();
if (this.debugPage) {
IR.RemoveListener(IR.EVENT_ITEM_SHOW, this.debugPage, onConsoleShow);
IR.RemoveListener(IR.EVENT_ITEM_HIDE, this.debugPage, onConsoleHide);
this.unregisterListener(onGoBackItemPressed, {itemName: 'Clear'});
this.unregisterListener(onGoBackItemPressed, {itemName: 'GoBack'});
this.unregisterListener(onItemPressed, {itemName: 'PlayPause'});
this.unregisterListener(onItemPressed, {itemName: 'ShowDebug'});
this.unregisterListener(onItemPressed, {itemName: 'ShowInfo'});
this.unregisterListener(onItemPressed, {itemName: 'ShowWarning'});
this.unregisterListener(onItemPressed, {itemName: 'ShowError'});
this.unregisterListener(onItemPressed, {itemName: 'ShowTimestamp'});
this.unregisterListener(onItemPressed, {itemName: 'ShowEvent'});
this.unregisterListener(onItemPressed, {itemName: 'ShowSource'});
this.unregisterListener(onItemPressed, {itemName: 'ShowMessage'});
}
};
this.setLineCount = function (count) {
this.lineCount = count;
return this;
};
this.setFieldSizes = function (fieldSizes) {
for (var key in fieldSizes) {
if (fieldSizes.hasOwnProperty(key)) {
this.fieldSizes[key] = fieldSizes[key];
}
}
return this;
};
this.setColumnsCount = function(count) {
this.columnsCount = count;
return this;
};
this.log = function(msg) {
if (this.consoleDisabled) {
return;
}
if (typeof msg === 'string') {
var msgObj = {};
msgObj.message = msg;
msgObj.event = 'info';
msgObj.source = 'SCRIPT';
var d = new Date();
msgObj.timestamp =
('00' + d.getDate()).slice(-2) + '-' +
('00' + (d.getMonth() + 1)).slice(-2) + '-' +
d.getFullYear() + ' ' +
('00' + d.getHours()).slice(-2) + ':' +
('00' + d.getMinutes()).slice(-2) + ':' +
('00' + d.getSeconds()).slice(-2) + '.' +
('000' + d.getMilliseconds()).slice(-3);
if (msgObj.message.indexOf('DEBUG: ') == 0) {
msgObj.event = 'debug';
msgObj.message = msgObj.message.substr(7);
} else if (msgObj.message.indexOf('ERROR: ') == 0) {
msgObj.event = 'error';
msgObj.message = msgObj.message.substr(7);
} else if (msgObj.message.indexOf('WARNING: ') == 0) {
msgObj.event = 'warning';
msgObj.message = msgObj.message.substr(9);
}
msg = msgObj;
}
this.messages.push(msg);
if (this.active) {
this.updateConsole(msg);
}
if (!this.noConsoleLog) {
var event = '';
if (msg.event && msg.event !== 'info') {
event = msg.event.toUpperCase() + ': ';
}
IR.Log(event + msg.message);
}
};
this.showField = function (field, show) {
if (this.fieldFilter[field] != !show) {
this.fieldFilter[field] = !show;
this.callEvent('settings', 'showField', field, show);
onConsoleShow(true); // update console
}
return this;
};
this.setEventFilter = function (event, hide) {
if (this.eventFilter[event] != hide) {
this.eventFilter[event] = hide;
this.callEvent('settings', 'eventFilter', event, hide);
onConsoleShow(true); // update console
}
return this;
};
this.setSourceFilter = function (source, hide) {
if (this.sourceFilter[source] != hide) {
this.sourceFilter[source] = hide;
this.callEvent('settings', 'sourceFilter', source, hide);
onConsoleShow(true); // update console
}
return this;
};
this.redefine = function (functionName, newFunction) {
this[functionName] = newFunction;
return this;
};
this.disableConsole = function () {
this.consoleDisabled = true;
return this;
};
this.enableConsole = function () {
this.consoleDisabled = false;
return this;
};
this.msgToString = function(msg, ignoreFilter) {
var text = (msg.message && ((ignoreFilter || !this.fieldFilter.message))) ? msg.message : '';
text = this.msgToInfoString(msg, ignoreFilter) + text;
return text;
};
this.msgToInfoString = function(msg, ignoreFilter) {
var timestamp = '';
if (!this.fieldFilter.timestamp || ignoreFilter) {
timestamp = msg.timestamp;
if (timestamp) {
if (typeof timestamp !== 'string') {
timestamp = timestamp.toString();
}
} else {
timestamp = '';
}
timestamp = (timestamp + ' '.repeat(this.fieldSizes.timestamp)).slice(0, this.fieldSizes.timestamp);
}
var source = (ignoreFilter || !this.fieldFilter.source) ? ((msg.source ? msg.source : '') +
' '.repeat(this.fieldSizes.source)).slice(0, this.fieldSizes.source) : '';
var event = (ignoreFilter || !this.fieldFilter.event) ? ((msg.event ? msg.event : '') +
' '.repeat(this.fieldSizes.event)).slice(0, this.fieldSizes.event) : '';
return timestamp + event + source;
};
this.calculateMessageOffset = function(ignoreFilter) {
return (this.msgToInfoString({})).length;
};
this.showConsole = function () {
if (this.isPopup) {
IR.ShowPopup(this.debugPageName);
} else if (this.debugPageName){
var lastPage = IR.CurrentPage ? IR.CurrentPage.Name : null;
if (lastPage != this.debugPageName) {
IR.ShowPage(this.debugPageName);
this.lastPage = lastPage;
}
}
};
this.hideConsole = function () {
if (this.isPopup) {
IR.HidePopup(this.debugPageName);
} else {
var name = this.lastPage ? this.lastPage : this.defaultPageName;
if (name) {
IR.ShowPage(name);
this.lastPage = null;
}
}
};
// eslint-disable-next-line no-unused-vars
this.updateConsole = function(msg) {
function getLineCount(s) {
return s.split(/\r\n|\r|\n/).length;
}
if (!that.debugPage) {
return;
}
var console = that.debugPage.GetItem('Log');
if (!console) {
return;
}
var offset = this.calculateMessageOffset();
var columnsCount = this.columnsCount ? this.columnsCount - offset : null;
if (columnsCount <= 0) { columnsCount = null; }
var empty = ' '.repeat(offset);
var count = this.getMessageCount();
var lineCount = 0;
var index = 0;
var text = '';
var lines = [];
var re = columnsCount ? new RegExp('(.{1,' + columnsCount + '})', 'g') : ''; // Regular expression example: /(.{1,80})/g
while (lineCount < this.lineCount) {
if (lines.length == 0) {
if (index >= count) { break; }
var m = this.getMessage(count - index - 1);
index++;
var isLegit = !(m.event && this.eventFilter[m.event]) && !(m.source && this.sourceFilter[m.source]);
if (!isLegit) { continue; }
lines = (m.message || '').split(/\r\n|\r|\n/); // check if message has line breaks
if (columnsCount) {
for (var j = lines.length - 1; j >=0; j--) {
// split each lines into a number of lines depending on the line length and the insert into lines
lines = lines.slice(0, j).concat((lines[j] || '').match(re), lines.slice(j+1));
}
}
}
var msgText = lines.pop();
if (msgText == '') { continue; }
text = lines.length ? (empty + msgText + '\n' + text) : this.msgToInfoString(m) + msgText + '\n' + text;
lineCount++;
}
console.Text = text;
// console.Text = this.getLastMessagesText(this.lineCount);
};
this.getMessageCount = function() {
return this.messages.length;
};
this.getMessage = function(index) {
return that.messages.get(index);
};
this.getLastMessages = function(count, ignoreFilter) {
if (count == undefined || count > that.messages.length) {
count = this.messages.length;
}
var result = [];
var found = 0;
for (var i = this.messages.length - 1; i >=0; i--) {
if (found >= count) break;
var msg = this.messages.get(i);
var event = msg.event;
var source = msg.source;
if (ignoreFilter || (!(event && this.eventFilter[event]) && !(source && this.sourceFilter[source]))) {
found++;
result.push(msg);
}
}
return result;
};
this.getLastMessagesText = function(count, ignoreFilter) {
var messages = this.getLastMessages(count, ignoreFilter);
var text = '';
for (var i = messages.length - 1; i >= 0; i--) {
text = text + this.msgToString(messages[i], ignoreFilter) + '\n';
}
return text;
};
this.clear = function() {
this.messages.clear();
};
function initButtonValue(page, name, value, defValue) {
if (page) {
var button = page.GetItem(name);
if (button) {
button.Value = (value != undefined) ? value : defValue;
}
}
}
}
// Necessary to use in IridiumMobile
if ((typeof IR === 'object') && (typeof module === 'object')) {
module['debug-console'] = {
DebugConsole: DebugConsole
};
exports = undefined;
}