-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
184 lines (165 loc) · 5.39 KB
/
logger.js
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
/*
Copyright 2017 Craig Miskell
This file is part of CookieMaster, a Firefox Web Extension
CookieMaster 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.
CookieMaster 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/>.
*/
/*
Instead of logging to console.log, use the log function in this class.
Keeps a configurable number of log lines with any context we might choose
and let's a useful UI display them
*/
var LogLevel = {
ERROR: 0,
WARN: 1,
INFO: 2,
DEBUG: 3,
TRACE: 4,
}
//Minimum time (in ms) between sending notifications about new logs
var notificationInterval = 250;
//Increments by 1 every log message, added to the log message.
// At 1G messages per second, will last 24 days before hitting
// max safe int (2^53-1), at 1M/s (still utterly absurd), 17 years.
// Not gonna worry about this ever rolling over/maxing out
var counter = 0;
// TODO: Can we make this a singleton? There needs to be only one
class Logger {
constructor(limit = 1000, level = LogLevel.INFO) {
this._limit = limit;
this._level = level;
this.notificationTimestamp = 0;
this.notificationTimer = undefined;
this.clearLogs();
browser.runtime.onMessage.addListener(this.handleMessage.bind(this));
}
set limit(value) {
this._limit = value;
}
get limit() {
return this._limit;
}
set level(value) {
this._level = value;
}
get level() {
return this._level;
}
error(entry, requestId) {
this.log(LogLevel.ERROR, entry, requestId);
}
warn(entry, requestId) {
this.log(LogLevel.WARN, entry, requestId);
}
info(entry, requestId) {
this.log(LogLevel.INFO, entry, requestId);
}
debug(entry, requestId) {
this.log(LogLevel.DEBUG, entry, requestId);
}
trace(entry, requestId) {
this.log(LogLevel.TRACE, entry, requestId);
}
log(level, entry, requestId) {
if(level > this._level) {
return; //Log is higher level of detail than we're asking to record. Ignore it
}
let logline = entry;
if (entry instanceof Error) {
logline = entry.name + " Exception: "+entry.message + "\n"+entry.stack;
}
if (requestId) {
logline = requestId + ": "+logline;
}
if((this.logs.length > 0) && (logline == this.logs[0].log)) {
this.logs[0].count++;
} else {
this.logs.unshift({timestamp: new Date(), log: logline, level: level, i: counter++, count: 1});
}
if (this.logs.length > this._limit) {
this.logs.pop();
}
if(this.notificationTimer == undefined) {
if (Date.now() < (this.notificationTimestamp + notificationInterval)) {
//No timer, and it's too soon since the last time; create a timer to send a message a bit later
this.notificationTimer = setTimeout(() => {
this.notificationTimestamp = Date.now();
browser.runtime.sendMessage({type: MessageTypes.MessageLogged}).then(m => {}, e => {});
this.notificationTimer = undefined;
}, notificationInterval);
} else {
//Been a while since we last sent a message, and there's no timer; send one
this.notificationTimestamp = Date.now();
browser.runtime.sendMessage({type: MessageTypes.MessageLogged}).then(m => {}, e => {});
}
}
}
//Return the 'count' most recent entries, or all of them if there are less than 'count'
getEntries(count = 1000) {
return this.logs.slice(0, count);
}
clearLogs() {
this.logs = new Array();
}
//For logging from things not in the 'background' scope/page
handleMessage(message, sender, sendResponse) {
switch(message.type) {
case MessageTypes.LogMessage:
this.log(message.level, message.entry, message.requestId);
return false; //No response coming
break;
case MessageTypes.GetLogs:
let count = message.hasOwnProperty("count") ? message.count : undefined;
sendResponse(this.getEntries(count));
break;
}
}
}
// Implements the log-generating interface of Logger, by sending a message
// to the real Logger instance
class SendMessageLogger {
constructor() {
}
error(entry, requestId) {
this.log(LogLevel.ERROR, entry, requestId);
}
warn(entry, requestId) {
this.log(LogLevel.WARN, entry, requestId);
}
info(entry, requestId) {
this.log(LogLevel.INFO, entry, requestId);
}
debug(entry, requestId) {
this.log(LogLevel.DEBUG, entry, requestId);
}
trace(entry, requestId) {
this.log(LogLevel.TRACE, entry, requestId);
}
log(level, entry, requestId) {
browser.runtime.sendMessage({
type: MessageTypes.LogMessage,
level: level,
entry: entry,
requestId: requestId
});
}
}
//Use from places that may need to SendMessage to do logging (e.g. config.js)
// Uses the presence/absence of the 'logger' global var;
// present => direct
// absent => SendMessage
function contextSafeLogger() {
if(typeof logger === 'undefined') {
return new SendMessageLogger();
} else {
return new Logger();
}
}