1 // Bug 1506 P1: This is just a handy logger. If you have a better one, toss
4 /*************************************************************************
5 * TBLogger (JavaScript XPCOM component)
7 * Allows loglevel-based logging to different logging mechanisms.
9 *************************************************************************/
11 // Module specific constants
12 const kMODULE_NAME
= "Torbutton Logger";
13 const kMODULE_CONTRACTID
= "@torproject.org/torbutton-logger;1";
14 const kMODULE_CID
= Components
.ID("f36d72c9-9718-4134-b550-e109638331d7");
16 const { Services
} = ChromeUtils
.import("resource://gre/modules/Services.jsm");
17 const { XPCOMUtils
} = ChromeUtils
.import(
18 "resource://gre/modules/XPCOMUtils.jsm"
21 XPCOMUtils
.defineLazyModuleGetters(this, {
22 ComponentUtils
: "resource://gre/modules/ComponentUtils.jsm",
25 function TorbuttonLogger() {
27 Services
.prefs
.addObserver("extensions.torbutton", this);
29 this.loglevel
= Services
.prefs
.getIntPref("extensions.torbutton.loglevel");
30 this.logmethod
= Services
.prefs
.getIntPref("extensions.torbutton.logmethod");
33 var logMngr
= Cc
["@mozmonkey.com/debuglogger/manager;1"].getService(
34 Ci
.nsIDebugLoggerManager
36 this._debuglog
= logMngr
.registerLogger("torbutton");
38 this._debuglog
= false;
40 this._console
= Services
.console
;
42 // This JSObject is exported directly to chrome
43 this.wrappedJSObject
= this;
44 this.log(3, "Torbutton debug output ready");
48 * JS XPCOM component registration goop:
50 * Everything below is boring boilerplate and can probably be ignored.
53 const nsIClassInfo
= Ci
.nsIClassInfo
;
55 const logString
= { 1: "VERB", 2: "DBUG", 3: "INFO", 4: "NOTE", 5: "WARN" };
58 return i
< 10 ? "0" + i
: i
;
61 TorbuttonLogger
.prototype = {
62 QueryInterface
: ChromeUtils
.generateQI([Ci
.nsIClassInfo
]),
64 wrappedJSObject
: null, // Initialized by constructor
66 // make this an nsIClassInfo object
67 flags
: nsIClassInfo
.DOM_OBJECT
,
69 // method of nsIClassInfo
70 classDescription
: "TorbuttonLogger",
72 contractID
: kMODULE_CONTRACTID
,
74 // method of nsIClassInfo
75 getInterfaces(count
) {
76 var interfaceList
= [nsIClassInfo
];
77 count
.value
= interfaceList
.length
;
81 // method of nsIClassInfo
82 getHelperForLanguage(count
) {
86 formatLog(str
, level
) {
89 padInt(d
.getUTCMonth() + 1) +
91 padInt(d
.getUTCDate()) +
93 padInt(d
.getUTCHours()) +
95 padInt(d
.getUTCMinutes()) +
97 padInt(d
.getUTCSeconds());
98 return "[" + now
+ "] Torbutton " + logString
[level
] + ": " + str
;
103 switch (this.logmethod
) {
105 if (this.loglevel
<= level
) {
106 dump(this.formatLog(str
, level
) + "\n");
111 if (this.loglevel
<= level
) {
112 this._console
.logStringMessage(this.formatLog(str
, level
));
118 safe_log(level
, str
, scrub
) {
119 if (this.loglevel
< 4) {
120 this.eclog(level
, str
+ scrub
);
122 this.eclog(level
, str
+ " [scrubbed]");
127 switch (this.logmethod
) {
128 case 2: // debuglogger
129 if (this._debuglog
) {
130 this._debuglog
.log(6 - level
, this.formatLog(str
, level
));
135 if (this.loglevel
<= level
) {
136 dump(this.formatLog(str
, level
) + "\n");
139 case 1: // errorconsole
140 if (this.loglevel
<= level
) {
141 this._console
.logStringMessage(this.formatLog(str
, level
));
145 dump("Bad log method: " + this.logmethod
);
149 // Pref observer interface implementation
151 // topic: what event occurred
152 // subject: what nsIPrefBranch we're observing
153 // data: which pref has been changed (relative to subject)
154 observe(subject
, topic
, data
) {
155 if (topic
!= "nsPref:changed") {
159 case "extensions.torbutton.logmethod":
160 this.logmethod
= Services
.prefs
.getIntPref(
161 "extensions.torbutton.logmethod"
163 if (this.logmethod
=== 0) {
164 Services
.prefs
.setBoolPref("browser.dom.window.dump.enabled", true);
166 Services
.prefs
.getIntPref("extensions.torlauncher.logmethod", 3) !== 0
168 // If Tor Launcher is not available or its log method is not 0
169 // then let's reset the dump pref.
170 Services
.prefs
.setBoolPref("browser.dom.window.dump.enabled", false);
173 case "extensions.torbutton.loglevel":
174 this.loglevel
= Services
.prefs
.getIntPref(
175 "extensions.torbutton.loglevel"
182 // Assign factory to global object.
183 const NSGetFactory
= XPCOMUtils
.generateNSGetFactory
184 ? XPCOMUtils
.generateNSGetFactory([TorbuttonLogger
])
185 : ComponentUtils
.generateNSGetFactory([TorbuttonLogger
]);