aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/logger.js
blob: 2ef03ede062043e399494f62884f702aaa0553d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************

    uBlock - a browser extension to block requests.
    Copyright (C) 2015 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
*/

/* global µBlock */

/******************************************************************************/
/******************************************************************************/

µBlock.logger = (function() {

'use strict';

/******************************************************************************/
/******************************************************************************/

var LogEntry = function(args) {
    this.init(args);
};

/******************************************************************************/

var logEntryFactory = function(args) {
    var entry = logEntryJunkyard.pop();
    if ( entry ) {
        return entry.init(args);
    }
    return new LogEntry(args);
};

var logEntryJunkyard = [];
var logEntryJunkyardMax = 100;

/******************************************************************************/

LogEntry.prototype.init = function(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];
    return this;
};

/******************************************************************************/

LogEntry.prototype.dispose = function() {
    this.tstamp = 0;
    this.tab = this.cat = '';
    this.d0 = this.d1 = this.d2 = this.d3 = this.d4 = undefined;
    if ( logEntryJunkyard.length < logEntryJunkyardMax ) {
        logEntryJunkyard.push(this);
    }
};

/******************************************************************************/
/******************************************************************************/

var LogBuffer = function() {
    this.lastReadTime = 0;
    this.size = 50;
    this.buffer = new Array(this.size);
    this.readPtr = 0;
    this.writePtr = 0;
};

/******************************************************************************/

LogBuffer.prototype.dispose = function() {
    var entry;
    var i = this.buffer.length;
    while ( i-- ) {
        entry = this.buffer[i];
        if ( entry instanceof LogEntry ) {
            entry.dispose();
        }
    }
    this.buffer = null;
    return null;
};

/******************************************************************************/

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] = logEntryFactory(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;
    }
};

/******************************************************************************/

LogBuffer.prototype.readAll = function() {
    var out;
    if ( this.readPtr < this.writePtr ) {
        out = this.buffer.slice(this.readPtr, this.writePtr);
    } else if ( this.writePtr < this.readPtr ) {
        out = this.buffer.slice(this.readPtr).concat(this.buffer.slice(0, this.writePtr));
    } else {
        out = [];
    }
    this.readPtr = this.writePtr;
    this.lastReadTime = Date.now();
    return out;
};

/******************************************************************************/
/******************************************************************************/

// Tab id to log buffer instances
var logBuffer = 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 = logBuffer.dispose();
    }
    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;

/******************************************************************************/
/******************************************************************************/

})();

/******************************************************************************/