summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/console
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/jetpack/sdk/console')
-rw-r--r--toolkit/jetpack/sdk/console/plain-text.js78
-rw-r--r--toolkit/jetpack/sdk/console/traceback.js86
2 files changed, 164 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/console/plain-text.js b/toolkit/jetpack/sdk/console/plain-text.js
new file mode 100644
index 000000000..0e44cf106
--- /dev/null
+++ b/toolkit/jetpack/sdk/console/plain-text.js
@@ -0,0 +1,78 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+module.metadata = {
+ "stability": "unstable"
+};
+
+const { Cc, Ci, Cu, Cr } = require("chrome");
+const self = require("../self");
+const prefs = require("../preferences/service");
+const { merge } = require("../util/object");
+const { ConsoleAPI } = Cu.import("resource://gre/modules/Console.jsm", {});
+
+const DEFAULT_LOG_LEVEL = "error";
+const ADDON_LOG_LEVEL_PREF = "extensions." + self.id + ".sdk.console.logLevel";
+const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
+
+var logLevel = DEFAULT_LOG_LEVEL;
+function setLogLevel() {
+ logLevel = prefs.get(ADDON_LOG_LEVEL_PREF,
+ prefs.get(SDK_LOG_LEVEL_PREF,
+ DEFAULT_LOG_LEVEL));
+}
+setLogLevel();
+
+var logLevelObserver = {
+ QueryInterface: function(iid) {
+ if (!iid.equals(Ci.nsIObserver) &&
+ !iid.equals(Ci.nsISupportsWeakReference) &&
+ !iid.equals(Ci.nsISupports))
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ return this;
+ },
+ observe: function(subject, topic, data) {
+ setLogLevel();
+ }
+};
+var branch = Cc["@mozilla.org/preferences-service;1"].
+ getService(Ci.nsIPrefService).
+ getBranch(null);
+branch.addObserver(ADDON_LOG_LEVEL_PREF, logLevelObserver, true);
+branch.addObserver(SDK_LOG_LEVEL_PREF, logLevelObserver, true);
+
+function PlainTextConsole(print, innerID) {
+
+ let consoleOptions = {
+ prefix: self.name,
+ maxLogLevel: logLevel,
+ dump: print,
+ innerID: innerID,
+ consoleID: "addon/" + self.id
+ };
+ let console = new ConsoleAPI(consoleOptions);
+
+ // As we freeze the console object, we can't modify this property afterward
+ Object.defineProperty(console, "maxLogLevel", {
+ get: function() {
+ return logLevel;
+ }
+ });
+
+ // We defined the `__exposedProps__` in our console chrome object.
+ //
+ // Meanwhile we're investigating with the platform team if `__exposedProps__`
+ // are needed, or are just a left-over.
+
+ console.__exposedProps__ = Object.keys(ConsoleAPI.prototype).reduce(function(exposed, prop) {
+ exposed[prop] = "r";
+ return exposed;
+ }, {});
+
+ Object.freeze(console);
+ return console;
+};
+exports.PlainTextConsole = PlainTextConsole;
diff --git a/toolkit/jetpack/sdk/console/traceback.js b/toolkit/jetpack/sdk/console/traceback.js
new file mode 100644
index 000000000..be0fb7b94
--- /dev/null
+++ b/toolkit/jetpack/sdk/console/traceback.js
@@ -0,0 +1,86 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { Ci, components } = require("chrome");
+const { parseStack, sourceURI } = require("toolkit/loader");
+const { readURISync } = require("../net/url");
+
+function safeGetFileLine(path, line) {
+ try {
+ var scheme = require("../url").URL(path).scheme;
+ // TODO: There should be an easier, more accurate way to figure out
+ // what's the case here.
+ if (!(scheme == "http" || scheme == "https"))
+ return readURISync(path).split("\n")[line - 1];
+ } catch (e) {}
+ return null;
+}
+
+function nsIStackFramesToJSON(frame) {
+ var stack = [];
+
+ while (frame) {
+ if (frame.filename) {
+ stack.unshift({
+ fileName: sourceURI(frame.filename),
+ lineNumber: frame.lineNumber,
+ name: frame.name
+ });
+ }
+ frame = frame.caller;
+ }
+
+ return stack;
+};
+
+var fromException = exports.fromException = function fromException(e) {
+ if (e instanceof Ci.nsIException)
+ return nsIStackFramesToJSON(e.location);
+ if (e.stack && e.stack.length)
+ return parseStack(e.stack);
+ if (e.fileName && typeof(e.lineNumber == "number"))
+ return [{fileName: sourceURI(e.fileName),
+ lineNumber: e.lineNumber,
+ name: null}];
+ return [];
+};
+
+var get = exports.get = function get() {
+ return nsIStackFramesToJSON(components.stack.caller);
+};
+
+var format = exports.format = function format(tbOrException) {
+ if (tbOrException === undefined) {
+ tbOrException = get();
+ tbOrException.pop();
+ }
+
+ var tb;
+ if (typeof(tbOrException) == "object" &&
+ tbOrException.constructor.name == "Array")
+ tb = tbOrException;
+ else
+ tb = fromException(tbOrException);
+
+ var lines = ["Traceback (most recent call last):"];
+
+ tb.forEach(
+ function(frame) {
+ if (!(frame.fileName || frame.lineNumber || frame.name))
+ return;
+
+ lines.push(' File "' + frame.fileName + '", line ' +
+ frame.lineNumber + ', in ' + frame.name);
+ var sourceLine = safeGetFileLine(frame.fileName, frame.lineNumber);
+ if (sourceLine)
+ lines.push(' ' + sourceLine.trim());
+ });
+
+ return lines.join("\n");
+};