2015-04-24 00:23:03 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2015-04-24 00:23:03 +02:00
|
|
|
|
|
|
|
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() {
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
let buffer = null;
|
|
|
|
let lastReadTime = 0;
|
|
|
|
let writePtr = 0;
|
2017-12-01 22:42:33 +01:00
|
|
|
|
|
|
|
// After 60 seconds without being read, a buffer will be considered
|
|
|
|
// unused, and thus removed from memory.
|
2018-12-13 18:30:54 +01:00
|
|
|
const logBufferObsoleteAfter = 30 * 1000;
|
2017-12-01 22:42:33 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const janitor = ( ) => {
|
2017-12-01 22:42:33 +01:00
|
|
|
if (
|
|
|
|
buffer !== null &&
|
|
|
|
lastReadTime < (Date.now() - logBufferObsoleteAfter)
|
|
|
|
) {
|
2018-12-13 18:30:54 +01:00
|
|
|
api.enabled = false;
|
2017-12-01 22:42:33 +01:00
|
|
|
buffer = null;
|
|
|
|
writePtr = 0;
|
2018-01-08 20:29:39 +01:00
|
|
|
api.ownerId = undefined;
|
2017-12-01 22:42:33 +01:00
|
|
|
vAPI.messaging.broadcast({ what: 'loggerDisabled' });
|
2015-04-24 00:23:03 +02:00
|
|
|
}
|
2017-12-01 22:42:33 +01:00
|
|
|
if ( buffer !== null ) {
|
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-12 22:36:20 +01:00
|
|
|
const boxEntry = function(details) {
|
|
|
|
if ( details.tstamp === undefined ) {
|
|
|
|
details.tstamp = Date.now();
|
|
|
|
}
|
|
|
|
return JSON.stringify(details);
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const api = {
|
|
|
|
enabled: false,
|
2018-01-08 20:29:39 +01:00
|
|
|
ownerId: undefined,
|
2018-12-13 18:30:54 +01:00
|
|
|
writeOne: function(details) {
|
2017-12-01 22:42:33 +01:00
|
|
|
if ( buffer === null ) { return; }
|
|
|
|
if ( writePtr === buffer.length ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
buffer.push(boxEntry(details));
|
2017-12-01 22:42:33 +01:00
|
|
|
} else {
|
2019-01-12 22:36:20 +01:00
|
|
|
buffer[writePtr] = boxEntry(details);
|
2017-12-01 22:42:33 +01:00
|
|
|
}
|
|
|
|
writePtr += 1;
|
|
|
|
},
|
2018-01-08 20:29:39 +01:00
|
|
|
readAll: function(ownerId) {
|
|
|
|
this.ownerId = ownerId;
|
2017-12-01 22:42:33 +01:00
|
|
|
if ( buffer === null ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
this.enabled = true;
|
2017-12-01 22:42:33 +01:00
|
|
|
buffer = [];
|
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
const out = buffer.slice(0, writePtr);
|
2017-12-01 22:42:33 +01:00
|
|
|
writePtr = 0;
|
|
|
|
lastReadTime = Date.now();
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
};
|
2015-04-24 00:23:03 +02:00
|
|
|
|
2018-01-08 20:29:39 +01:00
|
|
|
return api;
|
|
|
|
|
2015-04-24 00:23:03 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|