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