michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Cc, Ci, Cu, Cr } = require("chrome"); michael@0: const self = require("../self"); michael@0: const prefs = require("../preferences/service"); michael@0: const { merge } = require("../util/object"); michael@0: const { ConsoleAPI } = Cu.import("resource://gre/modules/devtools/Console.jsm", {}); michael@0: michael@0: const DEFAULT_LOG_LEVEL = "error"; michael@0: const ADDON_LOG_LEVEL_PREF = "extensions." + self.id + ".sdk.console.logLevel"; michael@0: const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel"; michael@0: michael@0: let logLevel = DEFAULT_LOG_LEVEL; michael@0: function setLogLevel() { michael@0: logLevel = prefs.get(ADDON_LOG_LEVEL_PREF, michael@0: prefs.get(SDK_LOG_LEVEL_PREF, michael@0: DEFAULT_LOG_LEVEL)); michael@0: } michael@0: setLogLevel(); michael@0: michael@0: let logLevelObserver = { michael@0: QueryInterface: function(iid) { michael@0: if (!iid.equals(Ci.nsIObserver) && michael@0: !iid.equals(Ci.nsISupportsWeakReference) && michael@0: !iid.equals(Ci.nsISupports)) michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: }, michael@0: observe: function(subject, topic, data) { michael@0: setLogLevel(); michael@0: } michael@0: }; michael@0: let branch = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefService). michael@0: getBranch(null); michael@0: branch.addObserver(ADDON_LOG_LEVEL_PREF, logLevelObserver, true); michael@0: branch.addObserver(SDK_LOG_LEVEL_PREF, logLevelObserver, true); michael@0: michael@0: function PlainTextConsole(print, innerID) { michael@0: michael@0: let consoleOptions = { michael@0: prefix: self.name + ": ", michael@0: maxLogLevel: logLevel, michael@0: dump: print, michael@0: innerID: innerID michael@0: }; michael@0: let console = new ConsoleAPI(consoleOptions); michael@0: michael@0: // As we freeze the console object, we can't modify this property afterward michael@0: Object.defineProperty(console, "maxLogLevel", { michael@0: get: function() { michael@0: return logLevel; michael@0: } michael@0: }); michael@0: michael@0: // We defined the `__exposedProps__` in our console chrome object. michael@0: // michael@0: // Meanwhile we're investigating with the platform team if `__exposedProps__` michael@0: // are needed, or are just a left-over. michael@0: michael@0: console.__exposedProps__ = Object.keys(ConsoleAPI.prototype).reduce(function(exposed, prop) { michael@0: exposed[prop] = "r"; michael@0: return exposed; michael@0: }, {}); michael@0: michael@0: Object.freeze(console); michael@0: return console; michael@0: }; michael@0: exports.PlainTextConsole = PlainTextConsole;