forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
339 lines (294 loc) · 8.13 KB
/
log.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* log.js
* Debug log functions
* @author ryanli, convincedd
*/
'use strict';
/* eslint-disable */
if (!this.Foxtrick)
// @ts-ignore
var Foxtrick = {};
/* eslint-enable */
/**
* outputs a list of strings/objects/errors to Foxtrick log
*
* @param {*[]} args
*/
// eslint-disable-next-line complexity
Foxtrick.log = function(...args) {
if (args.length < 2 && typeof args[0] === 'undefined') {
// useless logging
return;
}
// compile everything into a single string for trivial logging contexts
var hasError = false, concated = '';
for (let content of args) {
let item = '';
if (content instanceof Error) {
// exception
hasError = true;
if (Foxtrick.arch == 'Sandboxed') {
item = content.message;
if (typeof content.stack !== 'undefined')
item += '\n' + content.stack;
}
}
else if (typeof content == 'string') {
item = content;
}
else {
try {
item = JSON.stringify(content);
}
catch (e) {
item = String(content);
for (let [k, v] of Object.entries(content))
item += `${k}:${v}\n`;
}
}
concated += ` ${item}`;
}
concated += '\n';
// add the compiled string to HTML log container
Foxtrick.log.cache += concated;
Foxtrick.log.flush();
// store in debug storage (retrieved with forum debug log icon)
if (Foxtrick.context == 'content')
Foxtrick.SB.ext.sendRequest({ req: 'addDebugLog', log: concated });
else
Foxtrick.addToDebugLogStorage(concated);
for (let content of args) {
if (content instanceof Error && content.stack)
Foxtrick.reportError(content);
}
// Foxtrick.Prefs may not have loaded yet
if (Foxtrick.Prefs && Foxtrick.Prefs.getBool('logDisabled'))
return;
/* eslint-disable no-console */
if (typeof console !== 'undefined' && typeof console.log === 'function') {
// if console.log is available, make use of it
// for firefox it's in the webconsole (ctrl+shift+K) in preferences.html
// and logging in the browser console (ctrl+shift+J)
console.log(...args);
if (!hasError)
return;
// print a nice stack trace for exceptions in the console
let stackDumped = false;
for (let content of args) {
if (content instanceof Error && typeof content.stack !== 'undefined') {
if (typeof console.error == 'undefined')
console.log(content.stack);
else
console.error(content.stack);
stackDumped = true;
}
}
if (!stackDumped && typeof console.trace === 'function')
console.trace();
}
/* eslint-enable no-console */
};
/**
* environment info shown in log as header
*
* @param {document} doc
* @return {string}
*/
Foxtrick.log.header = function(doc) {
const INFO = [
Foxtrick.version + ' ' + Foxtrick.branch,
Foxtrick.arch + ' ' + Foxtrick.platform,
Foxtrick.Prefs.getString('htLanguage'),
Foxtrick.util.layout.isStandard(doc) ? 'standard' : 'simple',
Foxtrick.util.layout.isRtl(doc) ? 'RTL' : 'LTR',
Foxtrick.isStage(doc) ? ', Stage' : '',
];
var h = 'Version {}, {} platform, {} locale, {} layout, {} direction{}\n';
return Foxtrick.format(h, INFO);
};
/**
* cache log contents, will be flushed to page after calling Foxtrick.log.flush()
*
* @type {string}
*/
Foxtrick.log.cache = '';
Foxtrick.log.client = null;
Foxtrick.lazyProp(Foxtrick.log, 'client', () => {
var client = new Foxtrick.exceptionless.ExceptionlessClient('Z6ACuQcWkqEirnhl1n7FD38J0rWxYNepS2DN9s7F', 'https://bugs.foxtrick.org');
client.config.updateSettingsWhenIdleInterval = 0;
client.config.useReferenceIds(); // Foxtrick.log.client.getLastReferenceId();
client.config.includeMachineName = false;
client.config.includeIpAddress = false;
client.config.includeCookies = false;
client.config.includePostData = false;
client.config.moduleCollector = false;
let versionRe = /^\d+\.\d+(\.\d+)?/;
let version = Foxtrick.version;
let vMajor = version.match(versionRe)[0];
if (vMajor != version)
version = version.slice(0, vMajor.length) + '-' + version.slice(vMajor.length + 1);
client.config.setVersion(version);
return client;
});
/**
* a reference to the last document element for flushing
*
* this is a potential memory leak,
* therefore it needs to be cleared onbeforeunload
*
* @type {document}
*/
Foxtrick.log.doc = null;
/**
* print to HTML log, when doc is available
*
* @param {document} [document]
*/
Foxtrick.log.flush = function(document) {
if (Foxtrick.platform !== 'Firefox' && Foxtrick.context === 'background')
return;
var doc = document;
if (!doc) {
if (this.doc)
doc = this.doc;
else
return;
}
else if (doc !== this.doc) {
this.doc = doc;
doc.defaultView.addEventListener('beforeunload', function(ev) {
if (Foxtrick.log.doc === ev.target)
Foxtrick.log.doc = null;
});
}
if (!Foxtrick.Prefs.getBool('DisplayHTMLDebugOutput'))
return;
if (!doc.getElementById('page') || Foxtrick.log.cache === '')
return;
var div = doc.getElementById('ft-log');
var consoleDiv;
if (div) {
consoleDiv = doc.getElementById('ft-log-pre');
}
else {
// create log container
div = doc.createElement('div');
div.id = 'ft-log';
let header = doc.createElement('h2');
header.textContent = Foxtrick.L10n.getString('log.header');
div.appendChild(header);
consoleDiv = doc.createElement('pre');
consoleDiv.id = 'ft-log-pre';
consoleDiv.textContent = Foxtrick.log.header(doc);
div.appendChild(consoleDiv);
// add to page
let bottom = doc.getElementById('bottom');
if (bottom)
bottom.parentNode.insertBefore(div, bottom);
}
// add to log
consoleDiv.textContent += Foxtrick.log.cache;
// clear the cache
Foxtrick.log.cache = '';
};
/**
* debug log storage
*
* (retrieved with forum debug log icon)
*
* @type {string}
*/
Foxtrick.debugLogStorage = '';
/**
* @param {string} text
*/
Foxtrick.addToDebugLogStorage = function(text) {
Foxtrick.debugLogStorage += text;
};
/**
* a wrapper around Foxtrick.log for compatibility
*
* @deprecated
* @param {*} content
*/
Foxtrick.dump = function(content) {
Foxtrick.log(String(content).trim());
};
/**
* @param {string} header
* @param {string} bug
* @param {string} prefs
* @param {function(string):void} [refIdCb]
*/
Foxtrick.reportBug = (header, bug, prefs, refIdCb) => {
let client = Foxtrick.log.client;
let { teamId, teamName } = Foxtrick.modules.Core.TEAM;
client.config.setUserIdentity(String(teamId), String(teamName));
let b = client.createLog('bugReport', header, 'Error')
// eslint-disable-next-line no-magic-numbers
.setReferenceId(Math.floor((1 + Math.random()) * 0x10000000000).toString(16).slice(1))
.addTags(String(Foxtrick.branch.match(/^\w+/)))
.setProperty('HTLang', Foxtrick.Prefs.getString('htLanguage'))
.setProperty('bug', String(bug))
.setProperty('prefs', String(prefs));
let doc = Foxtrick.log.doc;
if (Foxtrick.isStage(doc))
b.addTags('onstage');
if (Foxtrick.util.layout.isRtl(doc))
b.addTags('RTL');
if (!Foxtrick.util.layout.isStandard(doc))
b.addTags('simpleSkin');
b.submit((ctx) => {
refIdCb && refIdCb(ctx.event.reference_id);
});
};
/**
* @param {Error} err
*/
Foxtrick.reportError = (err) => {
var report = () => {
let client = Foxtrick.log.client;
let { teamId, teamName } = Foxtrick.modules.Core.TEAM;
client.config.setUserIdentity(String(teamId), String(teamName));
// workaround for TracerKit parser
err.stack = err.stack.replace(/moz-extension/g, 'chrome-extension');
let e = client.createException(err)
.addTags(String(Foxtrick.branch.match(/^\w+/)))
.setProperty('HTLang', Foxtrick.Prefs.getString('htLanguage'));
if (Foxtrick.log.doc) {
let doc = Foxtrick.log.doc;
if (Foxtrick.isStage(doc))
e.addTags('onstage');
if (Foxtrick.util.layout.isRtl(doc))
e.addTags('RTL');
if (!Foxtrick.util.layout.isStandard(doc))
e.addTags('simpleSkin');
}
else {
e.addTags('bgcontext');
}
e.submit();
let doc = Foxtrick.log.doc;
if (!doc)
return; // TODO
Foxtrick.modules.Core.displayErrorNotice(doc, false);
};
var ask = () => {
let doc = Foxtrick.log.doc;
if (!doc)
return; // TODO
Foxtrick.modules.Core.displayErrorNotice(doc, true);
};
if (!Foxtrick.Prefs)
return;
switch (Foxtrick.Prefs.getString('errorReporting')) {
case 'reportAll':
report();
break;
case 'ignoreAll':
return;
default:
ask();
break;
}
};