root/trunk/components/debugService.js

Revision 1562, 7.1 kB (checked in by MaierMan, 11 months ago)

#1299: Abandon XPCOM.js(m)

Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is DownThemAll! Debug Service.
15  *
16  * The Initial Developer of the Original Code is Nils Maier
17  * Portions created by the Initial Developer are Copyright (C) 2008
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Nils Maier <MaierMan@web.de>
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36
37 const Cc = Components.classes;
38 const Ci = Components.interfaces;
39 const Cr = Components.results;
40 const ctor = Components.Constructor;
41 const module = Components.utils.import;
42 const error = Components.utils.reportError;
43
44 module("resource://gre/modules/XPCOMUtils.jsm");
45
46 const FileStream = new ctor('@mozilla.org/network/file-output-stream;1', 'nsIFileOutputStream', 'init');
47 const ScriptError = new ctor('@mozilla.org/scripterror;1', 'nsIScriptError', 'init');
48
49 function DebugService() {
50   this._pb.addObserver('extensions.dta.logging', this, true);
51   this._setEnabled(this._pb.getBoolPref('extensions.dta.logging'));
52  
53   try {
54     if (this._file.fileSize > (200 * 1024)) {
55       this.remove();
56     }
57   }
58   catch(ex) {
59     // No-Op
60   }
61 }
62 DebugService.prototype = {
63   classDescription: "DownThemAll! Debug and Logging Service",
64   contractID: "@downthemall.net/debug-service;1",
65   classID: Components.ID("0B82FEBB-59A1-41d7-B31D-D5A686E11A69"),
66  
67   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference, Ci.nsIWeakReference, Ci.dtaIDebugService]),
68  
69   QueryReferent: function(iid) this.QueryInterface(iid),
70   GetWeakReference: function() this,
71  
72   // nsIObserver
73   observe: function DS_observe(subject, topic, prefName) {
74     this._setEnabled(this._pb.getBoolPref('extensions.dta.logging'));
75   },
76  
77   get _cs() {
78     delete DebugService.prototype._cs;
79     return (DebugService.prototype._cs = Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService));
80   },
81   get _pb() {
82     delete DebugService.prototype._pb;
83     return (DebugService.prototype._pb = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch2));
84   },
85  
86   get _file() {
87     let file = Cc["@mozilla.org/file/directory_service;1"]
88       .getService(Ci.nsIProperties)
89       .get("ProfD", Ci.nsILocalFile);
90      file.append('dta_log.txt');
91      delete DebugService.prototype._file;
92      return (DebugService.prototype._file = file);
93   },
94  
95   get file() {
96     return this._file;
97   },
98   get enabled() {
99     return this._enabled;
100   },
101   _setEnabled: function DS_setEnabled(nv) {
102     this._enabled = nv;
103     if (nv) {
104       this.logString = this.log = this._log;
105     }
106     else {
107       this.logString = this.log = this._logDisabled;
108     }
109   },
110   _formatTimeDate: function DS_formatTimeDate(value) {
111     return String(value).replace(/\b(\d)\b/g, "0$1");
112   },
113   _log: function DS__log(msg, exception) {
114     try {
115       if (!msg || (msg == "" && typeof(exception) != "object")) {
116         return;
117       }
118       if (!(msg instanceof String) && typeof(msg) != 'string') {
119         for (var i = 0; i < 10 && msg.wrappedJSObject; ++i) {
120           msg = msg.wrappedJSObject;
121         }
122         msg = msg.toSource();
123       }
124       let time = new Date();
125       let text = [];
126       text.push(this._formatTimeDate(time.getHours()));
127       text.push(':');
128       text.push(this._formatTimeDate(time.getMinutes()));
129       text.push(':');
130       text.push(this._formatTimeDate(time.getSeconds()));
131       text.push('::');
132       text.push(time.getMilliseconds());
133       text.push('\n');
134
135       if (msg != "") {
136         text.push(msg.replace(/\n/g, "\n\t") + " ");
137       }
138       if (exception) {
139         text.push("\tError: " + exception);
140       }
141       text.push('\n');
142       let stack = Components.stack;
143       if (stack) {
144         stack = stack.caller.caller;
145       }
146       let lineNumber = 0;
147       let columnNumber = 0;
148       let fileName = null;
149       let sourceLine = '';
150      
151      
152       if (exception && exception.location) {
153         lineNumber = exception.lineNumber;
154         fileName = exception.filename;
155         columnNumber = exception.columnNumber;
156         stack = exception.location;
157
158         let initialLine = "Source Frame :: " + fileName;
159         initialLine += " :: " + exception.location;
160         initialLine += " :: line: " + lineNumber;
161         text.push('\t>');
162         text.push(initialLine);
163         text.push('\n');
164       }
165       else if (exception && exception.stack) {
166         lineNumber = exception.lineNumber;
167         fileName = exception.fileName;
168         columnNumber = 0;
169         let initialLine = "Source Frame (error) :: " + fileName;
170         initialLine += " :: " + exception.name;
171         initialLine += " :: line: " + lineNumber;
172         text.push("\t>" + initialLine + "\n");
173        
174       }
175       else if (exception && stack) {
176         lineNumber = stack.lineNumber;
177         fileName = stack.filename;
178         let initialLine = "Source Frame (stack) :: " + fileName;
179         initialLine += " :: " + stack.name;
180         initialLine += " :: line: " + lineNumber;
181         text.push('\t>');
182         text.push(initialLine);
183         text.push('\n');
184       }
185       else if (stack) {
186         text.push('\t>');
187         text.push(stack.toString());
188         text.push('\n');
189         lineNumber = stack.lineNumber;
190         fileName = stack.filename;
191       }
192      
193       if (stack instanceof Ci.nsIStackFrame) {
194         let sourceLine = stack.sourceLine;
195         let s = stack.caller;
196         for (let i = 0; i < 4 && s; ++i) {
197           text.push('\t>');
198           text.push(s.toString());
199           text.push('\n');
200           s = s.caller;
201         }
202         text = text.join('');
203         if (stack && exception) {
204           this._cs.logMessage(new ScriptError(text, fileName, sourceLine, lineNumber, columnNumber, 0x2, 'component javascript'));
205            
206         }
207         else {
208           this._cs.logStringMessage(text);
209         }
210       }
211       else {
212         text = text.join('');
213         this._cs.logStringMessage(text);
214       }
215       var f = new FileStream(this.file, 0x04 | 0x08 | 0x10, 0664, 0);
216       f.write(text, text.length);
217       f.close();
218     }
219     catch(ex) {
220       error(ex);
221     }
222  
223   },
224   _logDisabled: function DS__dumpDisabled() {
225     // no-op;
226   },
227   log: this._log,
228   logString: this._log,
229    
230   remove: function DS_remove() {
231     try {
232       this._file.remove(false);
233     }
234     catch (ex) {
235       throw Cr.NS_ERROR_FAILURE;
236     }
237   }
238 };
239
240 // entrypoint
241 function NSGetModule(compMgr, fileSpec) XPCOMUtils.generateModule([DebugService]);
Note: See TracBrowser for help on using the browser.