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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/console/plain-text.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "unstable"
    1.12 +};
    1.13 +
    1.14 +const { Cc, Ci, Cu, Cr } = require("chrome");
    1.15 +const self = require("../self");
    1.16 +const prefs = require("../preferences/service");
    1.17 +const { merge } = require("../util/object");
    1.18 +const { ConsoleAPI } = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
    1.19 +
    1.20 +const DEFAULT_LOG_LEVEL = "error";
    1.21 +const ADDON_LOG_LEVEL_PREF = "extensions." + self.id + ".sdk.console.logLevel";
    1.22 +const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
    1.23 +
    1.24 +let logLevel = DEFAULT_LOG_LEVEL;
    1.25 +function setLogLevel() {
    1.26 +  logLevel = prefs.get(ADDON_LOG_LEVEL_PREF,
    1.27 +                           prefs.get(SDK_LOG_LEVEL_PREF,
    1.28 +                                     DEFAULT_LOG_LEVEL));
    1.29 +}
    1.30 +setLogLevel();
    1.31 +
    1.32 +let logLevelObserver = {
    1.33 +  QueryInterface: function(iid) {
    1.34 +    if (!iid.equals(Ci.nsIObserver) &&
    1.35 +        !iid.equals(Ci.nsISupportsWeakReference) &&
    1.36 +        !iid.equals(Ci.nsISupports))
    1.37 +      throw Cr.NS_ERROR_NO_INTERFACE;
    1.38 +    return this;
    1.39 +  },
    1.40 +  observe: function(subject, topic, data) {
    1.41 +    setLogLevel();
    1.42 +  }
    1.43 +};
    1.44 +let branch = Cc["@mozilla.org/preferences-service;1"].
    1.45 +             getService(Ci.nsIPrefService).
    1.46 +             getBranch(null);
    1.47 +branch.addObserver(ADDON_LOG_LEVEL_PREF, logLevelObserver, true);
    1.48 +branch.addObserver(SDK_LOG_LEVEL_PREF, logLevelObserver, true);
    1.49 +
    1.50 +function PlainTextConsole(print, innerID) {
    1.51 +
    1.52 +  let consoleOptions = {
    1.53 +    prefix: self.name + ": ",
    1.54 +    maxLogLevel: logLevel,
    1.55 +    dump: print,
    1.56 +    innerID: innerID
    1.57 +  };
    1.58 +  let console = new ConsoleAPI(consoleOptions);
    1.59 +
    1.60 +  // As we freeze the console object, we can't modify this property afterward
    1.61 +  Object.defineProperty(console, "maxLogLevel", {
    1.62 +    get: function() {
    1.63 +      return logLevel;
    1.64 +    }
    1.65 +  });
    1.66 +
    1.67 +  // We defined the `__exposedProps__` in our console chrome object.
    1.68 +  //
    1.69 +  // Meanwhile we're investigating with the platform team if `__exposedProps__`
    1.70 +  // are needed, or are just a left-over.
    1.71 +
    1.72 +  console.__exposedProps__ = Object.keys(ConsoleAPI.prototype).reduce(function(exposed, prop) {
    1.73 +    exposed[prop] = "r";
    1.74 +    return exposed;
    1.75 +  }, {});
    1.76 +
    1.77 +  Object.freeze(console);
    1.78 +  return console;
    1.79 +};
    1.80 +exports.PlainTextConsole = PlainTextConsole;

mercurial