|
1 /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ |
|
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 "use strict"; |
|
8 |
|
9 this.EXPORTED_SYMBOLS = ["Logger"]; |
|
10 const PREF_DEBUG = "toolkit.identity.debug"; |
|
11 |
|
12 const Cu = Components.utils; |
|
13 const Ci = Components.interfaces; |
|
14 const Cc = Components.classes; |
|
15 const Cr = Components.results; |
|
16 |
|
17 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
18 Cu.import("resource://gre/modules/Services.jsm"); |
|
19 |
|
20 function IdentityLogger() { |
|
21 Services.prefs.addObserver(PREF_DEBUG, this, false); |
|
22 this._debug = Services.prefs.getBoolPref(PREF_DEBUG); |
|
23 return this; |
|
24 } |
|
25 |
|
26 IdentityLogger.prototype = { |
|
27 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), |
|
28 |
|
29 observe: function observe(aSubject, aTopic, aData) { |
|
30 switch(aTopic) { |
|
31 case "nsPref:changed": |
|
32 this._debug = Services.prefs.getBoolPref(PREF_DEBUG); |
|
33 break; |
|
34 |
|
35 case "quit-application-granted": |
|
36 Services.prefs.removeObserver(PREF_DEBUG, this); |
|
37 break; |
|
38 |
|
39 default: |
|
40 this.log("Logger observer", "Unknown topic:", aTopic); |
|
41 break; |
|
42 } |
|
43 }, |
|
44 |
|
45 _generateLogMessage: function _generateLogMessage(aPrefix, args) { |
|
46 // create a string representation of a list of arbitrary things |
|
47 let strings = []; |
|
48 |
|
49 // XXX bug 770418 - args look like flattened array, not list of strings |
|
50 |
|
51 args.forEach(function(arg) { |
|
52 if (typeof arg === 'string') { |
|
53 strings.push(arg); |
|
54 } else if (typeof arg === 'undefined') { |
|
55 strings.push('undefined'); |
|
56 } else if (arg === null) { |
|
57 strings.push('null'); |
|
58 } else { |
|
59 try { |
|
60 strings.push(JSON.stringify(arg, null, 2)); |
|
61 } catch(err) { |
|
62 strings.push("<<something>>"); |
|
63 } |
|
64 } |
|
65 }); |
|
66 return 'Identity ' + aPrefix + ': ' + strings.join(' '); |
|
67 }, |
|
68 |
|
69 /** |
|
70 * log() - utility function to print a list of arbitrary things |
|
71 * |
|
72 * Enable with about:config pref toolkit.identity.debug |
|
73 */ |
|
74 log: function log(aPrefix, ...args) { |
|
75 if (!this._debug) { |
|
76 return; |
|
77 } |
|
78 let output = this._generateLogMessage(aPrefix, args); |
|
79 dump(output + "\n"); |
|
80 |
|
81 // Additionally, make the output visible in the Error Console |
|
82 Services.console.logStringMessage(output); |
|
83 }, |
|
84 |
|
85 /** |
|
86 * reportError() - report an error through component utils as well as |
|
87 * our log function |
|
88 */ |
|
89 reportError: function reportError(aPrefix, ...aArgs) { |
|
90 let prefix = aPrefix + ' ERROR'; |
|
91 |
|
92 // Report the error in the browser |
|
93 let output = this._generateLogMessage(aPrefix, aArgs); |
|
94 Cu.reportError(output); |
|
95 dump("ERROR: " + output + "\n"); |
|
96 for (let frame = Components.stack.caller; frame; frame = frame.caller) { |
|
97 dump(frame + "\n"); |
|
98 } |
|
99 } |
|
100 |
|
101 }; |
|
102 |
|
103 this.Logger = new IdentityLogger(); |