uBlock/src/js/logger.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015-present 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
*/
'use strict';
/******************************************************************************/
/******************************************************************************/
µBlock.logger = (function() {
const LogEntry = function(details) {
this.init(details);
};
LogEntry.prototype.init = function(details) {
this.details = JSON.stringify(details);
};
let buffer = null;
let lastReadTime = 0;
let writePtr = 0;
// After 60 seconds without being read, a buffer will be considered
// unused, and thus removed from memory.
const logBufferObsoleteAfter = 30 * 1000;
const janitor = ( ) => {
if (
buffer !== null &&
lastReadTime < (Date.now() - logBufferObsoleteAfter)
) {
api.enabled = false;
buffer = null;
writePtr = 0;
2018-01-08 20:29:39 +01:00
api.ownerId = undefined;
vAPI.messaging.broadcast({ what: 'loggerDisabled' });
}
if ( buffer !== null ) {
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
}
};
const api = {
enabled: false,
2018-01-08 20:29:39 +01:00
ownerId: undefined,
writeOne: function(details) {
if ( buffer === null ) { return; }
if ( writePtr === buffer.length ) {
buffer.push(new LogEntry(details));
} else {
buffer[writePtr].init(details);
}
writePtr += 1;
},
2018-01-08 20:29:39 +01:00
readAll: function(ownerId) {
this.ownerId = ownerId;
if ( buffer === null ) {
this.enabled = true;
buffer = [];
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
}
const out = buffer.slice(0, writePtr);
writePtr = 0;
lastReadTime = Date.now();
return out;
},
};
2018-01-08 20:29:39 +01:00
return api;
})();
/******************************************************************************/