addon-sdk/source/lib/sdk/console/plain-text.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const { Cc, Ci, Cu, Cr } = require("chrome");
michael@0 12 const self = require("../self");
michael@0 13 const prefs = require("../preferences/service");
michael@0 14 const { merge } = require("../util/object");
michael@0 15 const { ConsoleAPI } = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
michael@0 16
michael@0 17 const DEFAULT_LOG_LEVEL = "error";
michael@0 18 const ADDON_LOG_LEVEL_PREF = "extensions." + self.id + ".sdk.console.logLevel";
michael@0 19 const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
michael@0 20
michael@0 21 let logLevel = DEFAULT_LOG_LEVEL;
michael@0 22 function setLogLevel() {
michael@0 23 logLevel = prefs.get(ADDON_LOG_LEVEL_PREF,
michael@0 24 prefs.get(SDK_LOG_LEVEL_PREF,
michael@0 25 DEFAULT_LOG_LEVEL));
michael@0 26 }
michael@0 27 setLogLevel();
michael@0 28
michael@0 29 let logLevelObserver = {
michael@0 30 QueryInterface: function(iid) {
michael@0 31 if (!iid.equals(Ci.nsIObserver) &&
michael@0 32 !iid.equals(Ci.nsISupportsWeakReference) &&
michael@0 33 !iid.equals(Ci.nsISupports))
michael@0 34 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 35 return this;
michael@0 36 },
michael@0 37 observe: function(subject, topic, data) {
michael@0 38 setLogLevel();
michael@0 39 }
michael@0 40 };
michael@0 41 let branch = Cc["@mozilla.org/preferences-service;1"].
michael@0 42 getService(Ci.nsIPrefService).
michael@0 43 getBranch(null);
michael@0 44 branch.addObserver(ADDON_LOG_LEVEL_PREF, logLevelObserver, true);
michael@0 45 branch.addObserver(SDK_LOG_LEVEL_PREF, logLevelObserver, true);
michael@0 46
michael@0 47 function PlainTextConsole(print, innerID) {
michael@0 48
michael@0 49 let consoleOptions = {
michael@0 50 prefix: self.name + ": ",
michael@0 51 maxLogLevel: logLevel,
michael@0 52 dump: print,
michael@0 53 innerID: innerID
michael@0 54 };
michael@0 55 let console = new ConsoleAPI(consoleOptions);
michael@0 56
michael@0 57 // As we freeze the console object, we can't modify this property afterward
michael@0 58 Object.defineProperty(console, "maxLogLevel", {
michael@0 59 get: function() {
michael@0 60 return logLevel;
michael@0 61 }
michael@0 62 });
michael@0 63
michael@0 64 // We defined the `__exposedProps__` in our console chrome object.
michael@0 65 //
michael@0 66 // Meanwhile we're investigating with the platform team if `__exposedProps__`
michael@0 67 // are needed, or are just a left-over.
michael@0 68
michael@0 69 console.__exposedProps__ = Object.keys(ConsoleAPI.prototype).reduce(function(exposed, prop) {
michael@0 70 exposed[prop] = "r";
michael@0 71 return exposed;
michael@0 72 }, {});
michael@0 73
michael@0 74 Object.freeze(console);
michael@0 75 return console;
michael@0 76 };
michael@0 77 exports.PlainTextConsole = PlainTextConsole;

mercurial