Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #2604, implement log levels
Browse files Browse the repository at this point in the history
Set log level to debug in run-addon, otherwise default to warn
  • Loading branch information
ianb committed Apr 12, 2017
1 parent 3716753 commit 087a853
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 36 deletions.
8 changes: 4 additions & 4 deletions addon/webextension/background/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals main, auth, catcher, deviceInfo, communication */
/* globals main, auth, catcher, deviceInfo, communication, log */

"use strict";

Expand All @@ -11,11 +11,11 @@ this.analytics = (function () {
exports.sendEvent = function (action, label, options) {
let eventCategory = "addon";
if (! telemetryPrefKnown) {
console.warn("sendEvent called before we were able to refresh");
log.warn("sendEvent called before we were able to refresh");
return Promise.resolve();
}
if (! telemetryPref) {
console.info(`Cancelled sendEvent ${eventCategory}/${action}/${label || 'none'} ${JSON.stringify(options)}`);
log.info(`Cancelled sendEvent ${eventCategory}/${action}/${label || 'none'} ${JSON.stringify(options)}`);
return Promise.resolve();
}
if (typeof label == "object" && (! options)) {
Expand Down Expand Up @@ -45,7 +45,7 @@ this.analytics = (function () {
for (let [gaField, value] of Object.entries(abTests)) {
options[gaField] = value;
}
console.info(`sendEvent ${eventCategory}/${action}/${label || 'none'} ${JSON.stringify(options)}`);
log.info(`sendEvent ${eventCategory}/${action}/${label || 'none'} ${JSON.stringify(options)}`);
req.send(JSON.stringify({
deviceId: auth.getDeviceId(),
event: eventCategory,
Expand Down
14 changes: 7 additions & 7 deletions addon/webextension/background/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals browser */
/* globals browser, log */
/* globals main, makeUuid, deviceInfo, analytics, catcher, buildSettings, communication */

"use strict";
Expand All @@ -20,7 +20,7 @@ this.auth = (function () {
registrationInfo = result.registrationInfo;
} else {
registrationInfo = generateRegistrationInfo();
console.info("Generating new device authentication ID", registrationInfo);
log.info("Generating new device authentication ID", registrationInfo);
return browser.storage.local.set({registrationInfo});
}
}));
Expand All @@ -47,14 +47,14 @@ this.auth = (function () {
req.setRequestHeader("content-type", "application/json");
req.onload = catcher.watchFunction(() => {
if (req.status == 200) {
console.info("Registered login");
log.info("Registered login");
initialized = true;
saveAuthInfo(JSON.parse(req.responseText));
resolve(true);
analytics.sendEvent("registered");
} else {
analytics.sendEvent("register-failed", `bad-response-${req.status}`);
console.warn("Error in response:", req.responseText);
log.warn("Error in response:", req.responseText);
let exc = new Error("Bad response: " + req.status);
exc.popupMessage = "LOGIN_ERROR";
reject(exc);
Expand Down Expand Up @@ -89,7 +89,7 @@ this.auth = (function () {
resolve(register());
}
} else if (req.status >= 300) {
console.warn("Error in response:", req.responseText);
log.warn("Error in response:", req.responseText);
let exc = new Error("Could not log in: " + req.status);
exc.popupMessage = "LOGIN_ERROR";
analytics.sendEvent("login-failed", `bad-response-${req.status}`);
Expand All @@ -102,7 +102,7 @@ this.auth = (function () {
} else {
initialized = true;
let jsonResponse = JSON.parse(req.responseText);
console.info("Screenshots logged in");
log.info("Screenshots logged in");
analytics.sendEvent("login");
saveAuthInfo(jsonResponse);
if (ownershipCheck) {
Expand Down Expand Up @@ -159,7 +159,7 @@ this.auth = (function () {
if (authHeader) {
return {"x-screenshots-auth": authHeader};
} else {
console.warn("No auth header available");
log.warn("No auth header available");
return {};
}
});
Expand Down
10 changes: 5 additions & 5 deletions addon/webextension/background/communication.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals browser, catcher */
/* globals browser, catcher, log */

"use strict";

Expand All @@ -9,12 +9,12 @@ this.communication = (function () {

browser.runtime.onMessage.addListener(catcher.watchFunction((req, sender, sendResponse) => {
if (! (req.funcName in registeredFunctions)) {
console.error(`Received unknown internal message type ${req.funcName}`);
log.error(`Received unknown internal message type ${req.funcName}`);
sendResponse({type: "error", name: "Unknown message type"});
return;
}
if (! Array.isArray(req.args)) {
console.error("Received message with no .args list");
log.error("Received message with no .args list");
sendResponse({type: "error", name: "No .args"});
return;
}
Expand All @@ -24,7 +24,7 @@ this.communication = (function () {
req.args.unshift(sender);
result = func.apply(null, req.args);
} catch (e) {
console.error(`Error in ${req.funcName}:`, e, e.stack);
log.error(`Error in ${req.funcName}:`, e, e.stack);
// FIXME: should consider using makeError from catcher here:
sendResponse({type: "error", message: e+""});
return;
Expand All @@ -33,7 +33,7 @@ this.communication = (function () {
result.then((concreteResult) => {
sendResponse({type: "success", value: concreteResult});
}).catch((errorResult) => {
console.error(`Promise error in ${req.funcName}:`, errorResult, errorResult && errorResult.stack);
log.error(`Promise error in ${req.funcName}:`, errorResult, errorResult && errorResult.stack);
sendResponse({type: "error", message: errorResult+""});
});
return true;
Expand Down
2 changes: 1 addition & 1 deletion addon/webextension/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ this.main = (function () {
});
}
}).catch((error) => {
console.error("Error getting hasSeenOnboarding:", error);
log.error("Error getting hasSeenOnboarding:", error);
});

exports.setBackend = function (newBackend) {
Expand Down
11 changes: 6 additions & 5 deletions addon/webextension/background/selectorLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals browser, catcher */
/* globals browser, catcher, log */

"use strict";

Expand All @@ -8,12 +8,13 @@ this.selectorLoader = (function () {
// These modules are loaded in order, first standardScripts, then optionally onboardingScripts, and then selectorScripts
// The order is important due to dependencies
const standardScripts = [
"build/buildSettings.js",
"log.js",
"catcher.js",
"assertIsTrusted.js",
"background/selectorLoader.js",
"selector/callBackground.js",
"selector/util.js",
"build/buildSettings.js"
"selector/util.js"
];

const selectorScripts = [
Expand Down Expand Up @@ -62,14 +63,14 @@ this.selectorLoader = (function () {
runAt: "document_end"
})
.catch((error) => {
console.error("error in script:", file, error);
log.error("error in script:", file, error);
error.scriptName = file;
throw error;
})
})
});
return lastPromise.then(() => {
console.log("finished loading scripts:", scripts.join(" "));
log.debug("finished loading scripts:", scripts.join(" "));
},
(error) => {
exports.unloadIfLoaded(tabId);
Expand Down
6 changes: 3 additions & 3 deletions addon/webextension/background/senderror.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals analytics, browser, communication, makeUuid, Raven, catcher, auth */
/* globals analytics, browser, communication, makeUuid, Raven, catcher, auth, log */

"use strict";

Expand Down Expand Up @@ -78,12 +78,12 @@ this.senderror = (function () {

exports.reportError = function (e) {
if (!analytics.getTelemetryPrefSync()) {
console.error("Telemetry disabled. Not sending critical error:", e);
log.error("Telemetry disabled. Not sending critical error:", e);
return;
}
let dsn = auth.getSentryPublicDSN();
if (! dsn) {
console.warn("Error:", e);
log.warn("Error:", e);
return;
}
if (! Raven.isSetup()) {
Expand Down
10 changes: 6 additions & 4 deletions addon/webextension/catcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* globals log */

"use strict";

var global = this;
Expand All @@ -10,7 +12,7 @@ this.catcher = (function () {
let queue = [];

exports.unhandled = function (error, info) {
console.error("Unhandled error:", error, info);
log.error("Unhandled error:", error, info);
let e = makeError(error, info);
if (! handler) {
queue.push(e);
Expand Down Expand Up @@ -57,16 +59,16 @@ this.catcher = (function () {

exports.watchPromise = function watchPromise(promise) {
return promise.catch((e) => {
console.error("------Error in promise:", e);
console.error(e.stack);
log.error("------Error in promise:", e);
log.error(e.stack);
exports.unhandled(makeError(e));
throw e;
});
};

exports.registerHandler = function (h) {
if (handler) {
console.error("registerHandler called after handler was already registered");
log.error("registerHandler called after handler was already registered");
return;
}
handler = h;
Expand Down
47 changes: 47 additions & 0 deletions addon/webextension/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* globals buildSettings */

"use strict";

this.log = (function () {
let exports = {};

const levels = ["debug", "info", "warn", "error"];
if (! levels.includes(buildSettings.logLevel)) {
console.warn("Invalid buildSettings.logLevel:", buildSettings.logLevel);
}
let shouldLog = {};

{
let startLogging = false;
for (let level of levels) {
if (buildSettings.logLevel === level) {
startLogging = true;
}
if (startLogging) {
shouldLog[level] = true;
}
}
}

function stub() {}
exports.debug = exports.info = exports.warn = exports.error = stub;

if (shouldLog.debug) {
exports.debug = console.debug.bind(console);
}

if (shouldLog.info) {
exports.info = console.info.bind(console);
}

if (shouldLog.warn) {
exports.warn = console.warn.bind(console);
}

if (shouldLog.error) {
exports.error = console.error.bind(console);
}

return exports;
})();
null;
6 changes: 4 additions & 2 deletions addon/webextension/manifest.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
},
"background": {
"scripts": [
"build/buildSettings.js",
"log.js",
"makeUuid.js",
"catcher.js",
"background/selectorLoader.js",
Expand All @@ -37,7 +39,6 @@
"background/senderror.js",
"build/raven.js",
"build/shot.js",
"build/buildSettings.js",
"background/analytics.js",
"background/deviceInfo.js",
"background/takeshot.js",
Expand All @@ -48,8 +49,9 @@
{
"matches": ["http://localhost/*"],
"js": [
"catcher.js",
"build/buildSettings.js",
"log.js",
"catcher.js",
"selector/callBackground.js",
"sitehelper.js"
],
Expand Down
2 changes: 1 addition & 1 deletion addon/webextension/onboarding/slides.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ this.slides = (function () {

const onResize = catcher.watchFunction(function () {
if (! iframe) {
console.warn("slides onResize called when iframe is not setup");
log.warn("slides onResize called when iframe is not setup");
return;
}
updateIframeSize();
Expand Down
4 changes: 2 additions & 2 deletions addon/webextension/selector/callBackground.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals browser */
/* globals browser, log */

"use strict";

Expand All @@ -11,7 +11,7 @@ this.callBackground = function callBackground (funcName, ...args) {
exc.name = "BackgroundError";
throw exc;
} else {
console.error("Unexpected background result:", result);
log.error("Unexpected background result:", result);
let exc = new Error(`Bad response type from background page: ${result.type || undefined}`);
exc.resultType = result.type || "undefined";
throw exc;
Expand Down
2 changes: 1 addition & 1 deletion addon/webextension/selector/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ this.ui = (function () { // eslint-disable-line no-unused-vars
}
}
catcher.unhandled(new Error("Surprising mover element"), {element: target.outerHTML});
console.warn("Got mover-target that wasn't a specific direction");
log.warn("Got mover-target that wasn't a specific direction");
}
}
target = target.parentNode;
Expand Down
2 changes: 1 addition & 1 deletion addon/webextension/selector/uicontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ this.uicontrol = (function () {
callBackground('closeSelector');
selectorLoader.unloadModules();
} catch (e) {
console.error('deactivate', e)
log.error('Error in deactivate', e)
// Sometimes this fires so late that the document isn't available
// We don't care about the exception, so we swallow it here
}
Expand Down

0 comments on commit 087a853

Please sign in to comment.