2015-04-24 00:23:03 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
uBlock - a browser extension to block requests.
|
2015-04-24 00:23:03 +02:00
|
|
|
Copyright (C) 2015 Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
'use strict';
|
2015-04-24 00:23:03 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.logger = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
var LogEntry = function(args) {
|
|
|
|
this.init(args);
|
2015-04-24 00:23:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
LogEntry.prototype.init = function(args) {
|
2015-04-24 00:23:03 +02:00
|
|
|
this.tstamp = Date.now();
|
2015-05-09 00:28:01 +02:00
|
|
|
this.tab = args[0] || '';
|
|
|
|
this.cat = args[1] || '';
|
|
|
|
this.d0 = args[2];
|
|
|
|
this.d1 = args[3];
|
|
|
|
this.d2 = args[4];
|
|
|
|
this.d3 = args[5];
|
2015-06-05 01:27:03 +02:00
|
|
|
this.d4 = args[6];
|
2015-04-24 00:23:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var LogBuffer = function() {
|
|
|
|
this.lastReadTime = 0;
|
|
|
|
this.size = 50;
|
|
|
|
this.buffer = new Array(this.size);
|
|
|
|
this.readPtr = 0;
|
|
|
|
this.writePtr = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-28 15:25:19 +02:00
|
|
|
LogBuffer.prototype.clearBuffer = function(beg, end) {
|
|
|
|
for ( var i = beg; i < end; i++ ) {
|
|
|
|
this.buffer[i] = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
LogBuffer.prototype.writeOne = function(args) {
|
2015-04-24 00:23:03 +02:00
|
|
|
// Reusing log entry = less memory churning
|
|
|
|
var entry = this.buffer[this.writePtr];
|
|
|
|
if ( entry instanceof LogEntry === false ) {
|
2016-03-30 14:37:41 +02:00
|
|
|
this.buffer[this.writePtr] = new LogEntry(args);
|
2015-04-24 00:23:03 +02:00
|
|
|
} else {
|
2015-05-09 00:28:01 +02:00
|
|
|
entry.init(args);
|
2015-04-24 00:23:03 +02:00
|
|
|
}
|
|
|
|
this.writePtr += 1;
|
|
|
|
if ( this.writePtr === this.size ) {
|
|
|
|
this.writePtr = 0;
|
|
|
|
}
|
|
|
|
// Grow the buffer between 1.5x-2x the current size
|
|
|
|
if ( this.writePtr === this.readPtr ) {
|
|
|
|
var toMove = this.buffer.slice(0, this.writePtr);
|
2015-06-28 15:25:19 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/391
|
|
|
|
// "The slice() method returns a shallow copy of a portion of an
|
|
|
|
// "array into a new array object."
|
|
|
|
// "shallow" => since we reuse entries, we need to remove the copied
|
|
|
|
// entries to prevent single instance of LogEntry being used in
|
|
|
|
// more than one slot.
|
|
|
|
this.clearBuffer(0, this.writePtr);
|
2015-04-24 00:23:03 +02:00
|
|
|
var minSize = Math.ceil(this.size * 1.5);
|
|
|
|
this.size += toMove.length;
|
|
|
|
if ( this.size < minSize ) {
|
|
|
|
this.buffer = this.buffer.concat(toMove, new Array(minSize - this.size));
|
|
|
|
this.writePtr = this.size;
|
|
|
|
} else {
|
|
|
|
this.buffer = this.buffer.concat(toMove);
|
|
|
|
this.writePtr = 0;
|
|
|
|
}
|
|
|
|
this.size = this.buffer.length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
LogBuffer.prototype.readAll = function() {
|
|
|
|
var out;
|
|
|
|
if ( this.readPtr < this.writePtr ) {
|
|
|
|
out = this.buffer.slice(this.readPtr, this.writePtr);
|
|
|
|
} else if ( this.writePtr < this.readPtr ) {
|
|
|
|
out = this.buffer.slice(this.readPtr).concat(this.buffer.slice(0, this.writePtr));
|
|
|
|
} else {
|
|
|
|
out = [];
|
|
|
|
}
|
|
|
|
this.readPtr = this.writePtr;
|
|
|
|
this.lastReadTime = Date.now();
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Tab id to log buffer instances
|
2015-05-09 00:28:01 +02:00
|
|
|
var logBuffer = null;
|
2015-04-24 00:23:03 +02:00
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
// After 60 seconds without being read, a buffer will be considered unused, and
|
2015-04-24 00:23:03 +02:00
|
|
|
// thus removed from memory.
|
2015-05-09 00:28:01 +02:00
|
|
|
var logBufferObsoleteAfter = 60 * 1000;
|
2015-04-24 00:23:03 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
var janitor = function() {
|
|
|
|
if (
|
|
|
|
logBuffer !== null &&
|
|
|
|
logBuffer.lastReadTime < (Date.now() - logBufferObsoleteAfter)
|
|
|
|
) {
|
2015-05-21 20:15:17 +02:00
|
|
|
api.writeOne = writeOneNoop;
|
2016-03-30 14:37:41 +02:00
|
|
|
logBuffer = null;
|
2015-05-09 00:28:01 +02:00
|
|
|
}
|
|
|
|
if ( logBuffer !== null ) {
|
2015-05-17 19:02:56 +02:00
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
2015-04-24 00:23:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
var writeOneNoop = function() {
|
|
|
|
};
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
var writeOne = function() {
|
2015-05-21 20:15:17 +02:00
|
|
|
logBuffer.writeOne(arguments);
|
2015-04-24 00:23:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-09 00:28:01 +02:00
|
|
|
var readAll = function() {
|
|
|
|
if ( logBuffer === null ) {
|
2015-05-21 20:15:17 +02:00
|
|
|
api.writeOne = writeOne;
|
2015-05-09 00:28:01 +02:00
|
|
|
logBuffer = new LogBuffer();
|
2015-05-17 19:02:56 +02:00
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
2015-05-09 00:28:01 +02:00
|
|
|
}
|
|
|
|
return logBuffer.readAll();
|
2015-04-25 13:28:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
var isEnabled = function() {
|
2015-05-09 00:28:01 +02:00
|
|
|
return logBuffer !== null;
|
2015-04-24 00:23:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
var api = {
|
|
|
|
writeOne: writeOneNoop,
|
2015-04-25 13:28:35 +02:00
|
|
|
readAll: readAll,
|
2015-05-21 20:15:17 +02:00
|
|
|
isEnabled: isEnabled
|
2015-04-24 00:23:03 +02:00
|
|
|
};
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
return api;
|
|
|
|
|
2015-04-24 00:23:03 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|