Fix logging

This commit is contained in:
Tamius Han 2020-01-29 01:36:23 +01:00
parent 1899f486e1
commit 78ff7d7e40
3 changed files with 67 additions and 43 deletions

View File

@ -6,6 +6,8 @@ class Logger {
} }
static saveConfig(conf) { static saveConfig(conf) {
console.info('Saving logger conf:', conf)
if (currentBrowser.firefox || currentBrowser.edge) { if (currentBrowser.firefox || currentBrowser.edge) {
return browser.storage.local.set( {'uwLogger': JSON.stringify(conf)}); return browser.storage.local.set( {'uwLogger': JSON.stringify(conf)});
} else if (currentBrowser.chrome) { } else if (currentBrowser.chrome) {
@ -39,11 +41,11 @@ class Logger {
}); });
} }
if (Debug.debug && Debug.debugStorage) { if (process.env.CHANNEL === 'dev') {
try { try {
console.log("[Logger::getSaved] Got settings:", JSON.parse(ret.uwLogger)); console.info("[Logger::getSaved] Got settings:", JSON.parse(ret.uwLogger));
} catch (e) { } catch (e) {
console.log("[Logger::getSaved] No settings.") console.info("[Logger::getSaved] No settings.")
} }
} }
@ -55,7 +57,7 @@ class Logger {
} }
async init(conf) { async init(conf) {
if (conf && process.env.CHANNEL === 'dev') { if (conf && process.env.CHANNEL === 'dev' && !conf.useConfFromStorage) {
this.conf = conf; this.conf = conf;
} else { } else {
this.conf = await Logger.getConfig(); this.conf = await Logger.getConfig();
@ -74,7 +76,7 @@ class Logger {
this.globalHistory = {}; this.globalHistory = {};
this.startTime = performance.now(); this.startTime = performance.now();
this.temp_disable = false; this.temp_disable = false;
this.stopTime = conf.timeout ? performance.now() + (conf.timeout * 1000) : undefined; this.stopTime = this.conf.timeout ? performance.now() + (this.conf.timeout * 1000) : undefined;
const ths = this; const ths = this;
const br = currentBrowser.firefox ? browser : chrome; const br = currentBrowser.firefox ? browser : chrome;
@ -207,6 +209,17 @@ class Logger {
return false; return false;
} }
isTimeUp() {
if (this.stopTime && performance.now() > this.stopTime) {
if (this.conf.allowLogging) {
this.log('force', 'debug', '-----[ alloted time has run out. logging will stop ]-----');
this.conf.allowLogging = false;
}
return true;
}
return false;
}
// NOTE: THIS FUNCTION IS NEVER USED INTERNALLY! // NOTE: THIS FUNCTION IS NEVER USED INTERNALLY!
canLog(component) { canLog(component) {
if (!this.conf.allowLogging) { if (!this.conf.allowLogging) {
@ -226,11 +239,6 @@ class Logger {
if (!this.conf.fileOptions.enabled || this.temp_disable) { if (!this.conf.fileOptions.enabled || this.temp_disable) {
return false; return false;
} }
if (!this.stopTime || performance.now() > this.stopTime) {
this.logger.log('force', 'debug', '-----[ alloted time has run out. logging will stop ]-----');
this.conf.allowLogging = false;
return false;
}
if (Array.isArray(component) && component.length ) { if (Array.isArray(component) && component.length ) {
for (const c of component) { for (const c of component) {
if (this.conf.fileOptions[c]) { if (this.conf.fileOptions[c]) {
@ -242,12 +250,7 @@ class Logger {
} }
} }
canLogConsole(component) { canLogConsole(component) {
if (!this.conf.consoleOptions.enabled || this.temp_disable) { if (!this.conf.consoleOptions?.enabled || this.temp_disable) {
return false;
}
if (performance.now() > this.stopTime) {
this.logger.log('force', 'debug', '-----[ alloted time has run out. logging will stop ]-----');
this.conf.allowLogging = false;
return false; return false;
} }
if (Array.isArray(component) && component.length) { if (Array.isArray(component) && component.length) {
@ -262,16 +265,48 @@ class Logger {
return this.conf.logAll; return this.conf.logAll;
} }
logToFile(message, stackInfo) {
let ts = performance.now();
if (ts <= this.history[this.history.length - 1]) {
ts = this.history[this.history.length - 1] + 0.00001;
}
this.history.push({
ts: ts,
message: JSON.stringify(message),
stack: stackInfo,
})
}
logToConsole(message, stackInfo) {
console.log(...message, {stack: stackInfo});
}
// level is unused as of now, but this may change in the future // level is unused as of now, but this may change in the future
// levels: 'info', 'warn', 'error'. // levels: 'info', 'warn', 'error'.
// if level is `true` (bool), logging happens regardless of any other // if level is `true` (bool), logging happens regardless of any other
// settings // settings
log(level, component, ...message) { log(level, component, ...message) {
if (!this.conf) { if (!this.conf || !this.conf.allowLogging) {
return; return;
} }
const stackInfo = this.parseStack(); const stackInfo = this.parseStack();
// skip all checks if we force log
if (level === 'force') {
if (this.conf.fileOptions.enabled) {
this.logToFile(message, stackInfo);
}
if (this.conf.consoleOptions.enabled) {
this.logToConsole(message, stackInfo);
}
return; // don't check further — recursion-land ahead!
}
if (this.isTimeUp()) {
return;
}
// don't log stuff from blacklisted origin (unless logger conf says otherwise) // don't log stuff from blacklisted origin (unless logger conf says otherwise)
if (this.isBlacklistedOrigin(stackInfo)) { if (this.isBlacklistedOrigin(stackInfo)) {
@ -279,26 +314,18 @@ class Logger {
} }
if (this.conf.fileOptions.enabled) { if (this.conf.fileOptions.enabled) {
if (this.canLogFile(component) || level === 'force') { if (this.canLogFile(component)) {
let ts = performance.now(); this.logToFile(message, stackInfo);
if (ts <= this.history[this.history.length - 1]) {
ts = this.history[this.history.length - 1] + 0.00001;
}
this.history.push({
ts: ts,
message: JSON.stringify(message),
stack: stackInfo,
})
} }
} }
if (this.conf.consoleOptions.enabled) { if (this.conf.consoleOptions.enabled) {
if (this.canLogConsole(component) || level === 'force') { if (this.canLogConsole(component)) {
console.log(...message, {stack: stackInfo}); this.logToConsole(message, stackInfo);
} }
} }
} }
// leaves a noticeable mark in the file log at the time it got triggered, as well as // leaves a noticeable mark in the file log at the time it got triggered, as well as
// at the intervals of 1s and .5s before the trigger moment // at the intervals of 1s and .5s before the trigger moment
cahen() { cahen() {

View File

@ -44,12 +44,12 @@ class UW {
if (!this.logger) { if (!this.logger) {
const loggingOptions = { const loggingOptions = {
allowLogging: true, allowLogging: true,
useConfFromStorage: true,
fileOptions: { fileOptions: {
enabled: false, enabled: false
// really the same stuff as consoleOptions
}, },
consoleOptions: { consoleOptions: {
enabled: true, // if logging is enabled at all enabled: true,
'debug': true, 'debug': true,
'init': true, 'init': true,
'settings': true, 'settings': true,
@ -64,16 +64,12 @@ class UW {
// 'videoRescan': true, // 'videoRescan': true,
// 'playerRescan': true, // 'playerRescan': true,
'arDetect': true, 'arDetect': true,
'arDetect_verbose': true, 'arDetect_verbose': true
}, },
allowBlacklistedOrigins: { allowBlacklistedOrigins: {
// logs that were called from functions named here will be excluded from both console logging 'periodicPlayerCheck': false,
// as well as logging to file. Exclusions happen because these functions — being periodic in 'periodicVideoStyleChangeCheck': false,
// nature as well as doing a lot of work — usually clog console too fast without providing 'handleMouseMove': false
// any valuable info.
// 'periodicPlayerCheck': true,
// 'periodicVideoStyleChangeCheck': true,
'handleMouseMove': false,
} }
}; };
this.logger = new Logger(); this.logger = new Logger();

View File

@ -63,7 +63,7 @@ export default {
}, },
data() { data() {
return { return {
loggingEnabled: undefined, loggingEnabled: false,
loggerSettings: '', loggerSettings: '',
loggerSettingsError: false, loggerSettingsError: false,
lastLoadedLoggerSettings: undefined, lastLoadedLoggerSettings: undefined,
@ -105,7 +105,7 @@ Browser-related stuff (please ensure this section is correct):
fileOptions: conf.fileOptions, fileOptions: conf.fileOptions,
consoleOptions: conf.consoleOptions consoleOptions: conf.consoleOptions
}; };
this.loggerSettings = JSON.stringify(lastLoadedSettings, null, 2); this.loggerSettings = JSON.stringify(lastLoadedLoggerSettings, null, 2);
}, },
async updateLoggerSettings(allowLogging) { async updateLoggerSettings(allowLogging) {
this.loggingEnabled = allowLogging; this.loggingEnabled = allowLogging;
@ -113,6 +113,7 @@ Browser-related stuff (please ensure this section is correct):
const parsedSettings = JSON.parse(this.loggerSettings); const parsedSettings = JSON.parse(this.loggerSettings);
Logger.saveConfig({ Logger.saveConfig({
allowLogging: allowLogging, allowLogging: allowLogging,
timeout: parsedSettings.timeout || undefined,
fileOptions: parsedSettings.fileOptions || {}, fileOptions: parsedSettings.fileOptions || {},
consoleOptions: parsedSettings.consoleOptions || {}, consoleOptions: parsedSettings.consoleOptions || {},
}); });