Translation updates
[torbutton.git] / components / torbutton-logger.js
blob2fdcd7e6a753e8668930ef0e755038ac7bcfa7b5
1 // Bug 1506 P1: This is just a handy logger. If you have a better one, toss
2 // this in the trash.
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",
23 });
25 function TorbuttonLogger() {
26 // Register observer
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");
32 try {
33 var logMngr = Cc["@mozmonkey.com/debuglogger/manager;1"].getService(
34 Ci.nsIDebugLoggerManager
36 this._debuglog = logMngr.registerLogger("torbutton");
37 } catch (exErr) {
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");
47 /**
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" };
57 function padInt(i) {
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",
71 classID: kMODULE_CID,
72 contractID: kMODULE_CONTRACTID,
74 // method of nsIClassInfo
75 getInterfaces(count) {
76 var interfaceList = [nsIClassInfo];
77 count.value = interfaceList.length;
78 return interfaceList;
81 // method of nsIClassInfo
82 getHelperForLanguage(count) {
83 return null;
86 formatLog(str, level) {
87 var d = new Date();
88 var now =
89 padInt(d.getUTCMonth() + 1) +
90 "-" +
91 padInt(d.getUTCDate()) +
92 " " +
93 padInt(d.getUTCHours()) +
94 ":" +
95 padInt(d.getUTCMinutes()) +
96 ":" +
97 padInt(d.getUTCSeconds());
98 return "[" + now + "] Torbutton " + logString[level] + ": " + str;
101 // error console log
102 eclog(level, str) {
103 switch (this.logmethod) {
104 case 0: // stderr
105 if (this.loglevel <= level) {
106 dump(this.formatLog(str, level) + "\n");
108 break;
109 default:
110 // errorconsole
111 if (this.loglevel <= level) {
112 this._console.logStringMessage(this.formatLog(str, level));
114 break;
118 safe_log(level, str, scrub) {
119 if (this.loglevel < 4) {
120 this.eclog(level, str + scrub);
121 } else {
122 this.eclog(level, str + " [scrubbed]");
126 log(level, str) {
127 switch (this.logmethod) {
128 case 2: // debuglogger
129 if (this._debuglog) {
130 this._debuglog.log(6 - level, this.formatLog(str, level));
131 break;
133 // fallthrough
134 case 0: // stderr
135 if (this.loglevel <= level) {
136 dump(this.formatLog(str, level) + "\n");
138 break;
139 case 1: // errorconsole
140 if (this.loglevel <= level) {
141 this._console.logStringMessage(this.formatLog(str, level));
143 break;
144 default:
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") {
156 return;
158 switch (data) {
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);
165 } else if (
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);
172 break;
173 case "extensions.torbutton.loglevel":
174 this.loglevel = Services.prefs.getIntPref(
175 "extensions.torbutton.loglevel"
177 break;
182 // Assign factory to global object.
183 const NSGetFactory = XPCOMUtils.generateNSGetFactory
184 ? XPCOMUtils.generateNSGetFactory([TorbuttonLogger])
185 : ComponentUtils.generateNSGetFactory([TorbuttonLogger]);