summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/console/plain-text.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
commit37d5300335d81cecbecc99812747a657588c63eb (patch)
tree765efa3b6a56bb715d9813a8697473e120436278 /toolkit/jetpack/sdk/console/plain-text.js
parentb2bdac20c02b12f2057b9ef70b0a946113a00e00 (diff)
parent4fb11cd5966461bccc3ed1599b808237be6b0de9 (diff)
downloadUXP-37d5300335d81cecbecc99812747a657588c63eb.tar
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.gz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.lz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.xz
UXP-37d5300335d81cecbecc99812747a657588c63eb.zip
Merge branch 'ext-work'
Diffstat (limited to 'toolkit/jetpack/sdk/console/plain-text.js')
-rw-r--r--toolkit/jetpack/sdk/console/plain-text.js78
1 files changed, 78 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;