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
|
|
|
|
|
|
|
/******************************************************************************/
|
2021-07-25 16:55:35 +02:00
|
|
|
|
|
|
|
let buffer = null;
|
|
|
|
let lastReadTime = 0;
|
|
|
|
let writePtr = 0;
|
|
|
|
|
2021-08-27 02:03:39 +02:00
|
|
|
// After 30 seconds without being read, a buffer will be considered
|
2021-07-25 16:55:35 +02:00
|
|
|
// unused, and thus removed from memory.
|
|
|
|
const logBufferObsoleteAfter = 30 * 1000;
|
|
|
|
|
|
|
|
const janitor = ( ) => {
|
|
|
|
if (
|
|
|
|
buffer !== null &&
|
|
|
|
lastReadTime < (Date.now() - logBufferObsoleteAfter)
|
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
logger.enabled = false;
|
2021-07-25 16:55:35 +02:00
|
|
|
buffer = null;
|
|
|
|
writePtr = 0;
|
2021-07-29 01:48:38 +02:00
|
|
|
logger.ownerId = undefined;
|
2021-07-25 16:55:35 +02:00
|
|
|
vAPI.messaging.broadcast({ what: 'loggerDisabled' });
|
|
|
|
}
|
|
|
|
if ( buffer !== null ) {
|
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const boxEntry = function(details) {
|
|
|
|
if ( details.tstamp === undefined ) {
|
|
|
|
details.tstamp = Date.now();
|
|
|
|
}
|
|
|
|
return JSON.stringify(details);
|
|
|
|
};
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const logger = {
|
2021-07-25 16:55:35 +02:00
|
|
|
enabled: false,
|
|
|
|
ownerId: undefined,
|
|
|
|
writeOne: function(details) {
|
|
|
|
if ( buffer === null ) { return; }
|
|
|
|
const box = boxEntry(details);
|
|
|
|
if ( writePtr === buffer.length ) {
|
|
|
|
buffer.push(box);
|
|
|
|
} else {
|
|
|
|
buffer[writePtr] = box;
|
2015-04-24 00:23:03 +02:00
|
|
|
}
|
2021-07-25 16:55:35 +02:00
|
|
|
writePtr += 1;
|
|
|
|
},
|
|
|
|
readAll: function(ownerId) {
|
|
|
|
this.ownerId = ownerId;
|
|
|
|
if ( buffer === null ) {
|
|
|
|
this.enabled = true;
|
|
|
|
buffer = [];
|
2017-12-01 22:42:33 +01:00
|
|
|
vAPI.setTimeout(janitor, logBufferObsoleteAfter);
|
|
|
|
}
|
2021-07-25 16:55:35 +02:00
|
|
|
const out = buffer.slice(0, writePtr);
|
|
|
|
writePtr = 0;
|
|
|
|
lastReadTime = Date.now();
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
};
|
2017-12-01 22:42:33 +01:00
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
export default logger;
|
2015-04-24 00:23:03 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|