logger.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2015-2019 Raymond Hill
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uBlock
  17. */
  18. 'use strict';
  19. ηMatrix.logger = (function () {
  20. let LogEntry = function (args) {
  21. this.init(args);
  22. };
  23. LogEntry.prototype.init = function (args) {
  24. this.tstamp = Date.now();
  25. this.tab = args[0] || '';
  26. this.cat = args[1] || '';
  27. this.d0 = args[2];
  28. this.d1 = args[3];
  29. this.d2 = args[4];
  30. this.d3 = args[5];
  31. };
  32. var buffer = null;
  33. var lastReadTime = 0;
  34. var writePtr = 0;
  35. // After 60 seconds without being read, a buffer will be considered
  36. // unused, and thus removed from memory.
  37. var logBufferObsoleteAfter = 30 * 1000;
  38. var janitor = function () {
  39. if (buffer !== null
  40. && lastReadTime < (Date.now() - logBufferObsoleteAfter)) {
  41. buffer = null;
  42. writePtr = 0;
  43. api.ownerId = undefined;
  44. }
  45. if (buffer !== null) {
  46. vAPI.setTimeout(janitor, logBufferObsoleteAfter);
  47. }
  48. };
  49. var api = {
  50. ownerId: undefined,
  51. writeOne: function () {
  52. if (buffer === null) {
  53. return;
  54. }
  55. if (writePtr === buffer.length) {
  56. buffer.push(new LogEntry(arguments));
  57. } else {
  58. buffer[writePtr].init(arguments);
  59. }
  60. writePtr += 1;
  61. },
  62. readAll: function (ownerId) {
  63. this.ownerId = ownerId;
  64. if (buffer === null) {
  65. buffer = [];
  66. vAPI.setTimeout(janitor, logBufferObsoleteAfter);
  67. }
  68. var out = buffer.slice(0, writePtr);
  69. writePtr = 0;
  70. lastReadTime = Date.now();
  71. return out;
  72. }
  73. };
  74. return api;
  75. })();