mirror of https://github.com/gorhill/uBlock.git
code review: simplify needlessly complicated logger buffering code
This commit is contained in:
parent
88853070a1
commit
faad68f37b
207
src/js/logger.js
207
src/js/logger.js
|
@ -1,7 +1,7 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
uBlock - a browser extension to block requests.
|
uBlock - a browser extension to block requests.
|
||||||
Copyright (C) 2015 Raymond Hill
|
Copyright (C) 2015-2017 Raymond Hill
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
|
@ -26,162 +26,67 @@
|
||||||
|
|
||||||
µBlock.logger = (function() {
|
µBlock.logger = (function() {
|
||||||
|
|
||||||
/******************************************************************************/
|
var LogEntry = function(args) {
|
||||||
/******************************************************************************/
|
this.init(args);
|
||||||
|
};
|
||||||
|
|
||||||
var LogEntry = function(args) {
|
LogEntry.prototype.init = function(args) {
|
||||||
this.init(args);
|
this.tstamp = Date.now();
|
||||||
};
|
this.tab = args[0] || '';
|
||||||
|
this.cat = args[1] || '';
|
||||||
|
this.d0 = args[2];
|
||||||
|
this.d1 = args[3];
|
||||||
|
this.d2 = args[4];
|
||||||
|
this.d3 = args[5];
|
||||||
|
this.d4 = args[6];
|
||||||
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
var buffer = null;
|
||||||
|
var lastReadTime = 0;
|
||||||
|
var writePtr = 0;
|
||||||
|
|
||||||
LogEntry.prototype.init = function(args) {
|
// After 60 seconds without being read, a buffer will be considered
|
||||||
this.tstamp = Date.now();
|
// unused, and thus removed from memory.
|
||||||
this.tab = args[0] || '';
|
var logBufferObsoleteAfter = 60 * 1000;
|
||||||
this.cat = args[1] || '';
|
|
||||||
this.d0 = args[2];
|
|
||||||
this.d1 = args[3];
|
|
||||||
this.d2 = args[4];
|
|
||||||
this.d3 = args[5];
|
|
||||||
this.d4 = args[6];
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
var janitor = function() {
|
||||||
/******************************************************************************/
|
if (
|
||||||
|
buffer !== null &&
|
||||||
var LogBuffer = function() {
|
lastReadTime < (Date.now() - logBufferObsoleteAfter)
|
||||||
this.lastReadTime = 0;
|
) {
|
||||||
this.size = 50;
|
buffer = null;
|
||||||
this.buffer = new Array(this.size);
|
writePtr = 0;
|
||||||
this.readPtr = 0;
|
vAPI.messaging.broadcast({ what: 'loggerDisabled' });
|
||||||
this.writePtr = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
LogBuffer.prototype.clearBuffer = function(beg, end) {
|
|
||||||
for ( var i = beg; i < end; i++ ) {
|
|
||||||
this.buffer[i] = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
LogBuffer.prototype.writeOne = function(args) {
|
|
||||||
// Reusing log entry = less memory churning
|
|
||||||
var entry = this.buffer[this.writePtr];
|
|
||||||
if ( entry instanceof LogEntry === false ) {
|
|
||||||
this.buffer[this.writePtr] = new LogEntry(args);
|
|
||||||
} else {
|
|
||||||
entry.init(args);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
// 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);
|
|
||||||
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;
|
if ( buffer !== null ) {
|
||||||
}
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
return {
|
||||||
|
writeOne: function() {
|
||||||
LogBuffer.prototype.readAll = function() {
|
if ( buffer === null ) { return; }
|
||||||
var out;
|
if ( writePtr === buffer.length ) {
|
||||||
if ( this.readPtr < this.writePtr ) {
|
buffer.push(new LogEntry(arguments));
|
||||||
out = this.buffer.slice(this.readPtr, this.writePtr);
|
} else {
|
||||||
} else if ( this.writePtr < this.readPtr ) {
|
buffer[writePtr].init(arguments);
|
||||||
out = this.buffer.slice(this.readPtr).concat(this.buffer.slice(0, this.writePtr));
|
}
|
||||||
} else {
|
writePtr += 1;
|
||||||
out = [];
|
},
|
||||||
}
|
readAll: function() {
|
||||||
this.readPtr = this.writePtr;
|
if ( buffer === null ) {
|
||||||
this.lastReadTime = Date.now();
|
buffer = [];
|
||||||
return out;
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
||||||
};
|
}
|
||||||
|
var out = buffer.slice(0, writePtr);
|
||||||
/******************************************************************************/
|
writePtr = 0;
|
||||||
/******************************************************************************/
|
lastReadTime = Date.now();
|
||||||
|
return out;
|
||||||
// Tab id to log buffer instances
|
},
|
||||||
var logBuffer = null;
|
isEnabled: function() {
|
||||||
|
return buffer !== null;
|
||||||
// After 60 seconds without being read, a buffer will be considered unused, and
|
}
|
||||||
// thus removed from memory.
|
};
|
||||||
var logBufferObsoleteAfter = 60 * 1000;
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var janitor = function() {
|
|
||||||
if (
|
|
||||||
logBuffer !== null &&
|
|
||||||
logBuffer.lastReadTime < (Date.now() - logBufferObsoleteAfter)
|
|
||||||
) {
|
|
||||||
api.writeOne = writeOneNoop;
|
|
||||||
logBuffer = null;
|
|
||||||
vAPI.messaging.broadcast({ what: 'loggerDisabled' });
|
|
||||||
}
|
|
||||||
if ( logBuffer !== null ) {
|
|
||||||
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var writeOneNoop = function() {
|
|
||||||
};
|
|
||||||
|
|
||||||
var writeOne = function() {
|
|
||||||
logBuffer.writeOne(arguments);
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var readAll = function() {
|
|
||||||
if ( logBuffer === null ) {
|
|
||||||
api.writeOne = writeOne;
|
|
||||||
logBuffer = new LogBuffer();
|
|
||||||
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
||||||
}
|
|
||||||
return logBuffer.readAll();
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var isEnabled = function() {
|
|
||||||
return logBuffer !== null;
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var api = {
|
|
||||||
writeOne: writeOneNoop,
|
|
||||||
readAll: readAll,
|
|
||||||
isEnabled: isEnabled
|
|
||||||
};
|
|
||||||
|
|
||||||
return api;
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue