2014-06-25 09:12:07 +04:00
|
|
|
/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
|
2012-07-04 01:53:37 +04:00
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["Logger"];
|
|
|
|
const PREF_DEBUG = "toolkit.identity.debug";
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
2013-03-27 21:01:46 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cr = Components.results;
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
function IdentityLogger() {
|
|
|
|
Services.prefs.addObserver(PREF_DEBUG, this, false);
|
|
|
|
this._debug = Services.prefs.getBoolPref(PREF_DEBUG);
|
|
|
|
return this;
|
2012-07-04 01:53:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
IdentityLogger.prototype = {
|
2012-07-04 01:53:37 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
|
|
|
|
|
|
|
|
observe: function observe(aSubject, aTopic, aData) {
|
2013-03-27 21:01:46 +04:00
|
|
|
switch(aTopic) {
|
2012-07-04 01:53:37 +04:00
|
|
|
case "nsPref:changed":
|
2013-03-27 21:01:46 +04:00
|
|
|
this._debug = Services.prefs.getBoolPref(PREF_DEBUG);
|
2012-07-04 01:53:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "quit-application-granted":
|
2013-03-27 21:01:46 +04:00
|
|
|
Services.prefs.removeObserver(PREF_DEBUG, this);
|
2012-07-04 01:53:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.log("Logger observer", "Unknown topic:", aTopic);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
_generateLogMessage: function _generateLogMessage(aPrefix, args) {
|
|
|
|
// create a string representation of a list of arbitrary things
|
|
|
|
let strings = [];
|
2012-07-04 01:53:37 +04:00
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
// XXX bug 770418 - args look like flattened array, not list of strings
|
2013-03-27 19:19:09 +04:00
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
args.forEach(function(arg) {
|
|
|
|
if (typeof arg === 'string') {
|
|
|
|
strings.push(arg);
|
|
|
|
} else if (typeof arg === 'undefined') {
|
|
|
|
strings.push('undefined');
|
|
|
|
} else if (arg === null) {
|
2012-07-04 01:53:37 +04:00
|
|
|
strings.push('null');
|
|
|
|
} else {
|
2013-03-27 21:01:46 +04:00
|
|
|
try {
|
|
|
|
strings.push(JSON.stringify(arg, null, 2));
|
|
|
|
} catch(err) {
|
|
|
|
strings.push("<<something>>");
|
2012-07-04 01:53:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-03-27 21:01:46 +04:00
|
|
|
return 'Identity ' + aPrefix + ': ' + strings.join(' ');
|
2012-07-04 01:53:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* log() - utility function to print a list of arbitrary things
|
|
|
|
*
|
|
|
|
* Enable with about:config pref toolkit.identity.debug
|
|
|
|
*/
|
2013-03-27 21:01:46 +04:00
|
|
|
log: function log(aPrefix, ...args) {
|
|
|
|
if (!this._debug) {
|
2012-07-04 01:53:37 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-03-27 21:01:46 +04:00
|
|
|
let output = this._generateLogMessage(aPrefix, args);
|
2013-03-27 19:19:09 +04:00
|
|
|
dump(output + "\n");
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
// Additionally, make the output visible in the Error Console
|
|
|
|
Services.console.logStringMessage(output);
|
2013-03-27 19:19:09 +04:00
|
|
|
},
|
|
|
|
|
2012-07-04 01:53:37 +04:00
|
|
|
/**
|
2013-03-27 21:01:46 +04:00
|
|
|
* reportError() - report an error through component utils as well as
|
2012-07-04 01:53:37 +04:00
|
|
|
* our log function
|
|
|
|
*/
|
2013-03-27 21:01:46 +04:00
|
|
|
reportError: function reportError(aPrefix, ...aArgs) {
|
|
|
|
let prefix = aPrefix + ' ERROR';
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
// Report the error in the browser
|
2013-03-27 21:01:46 +04:00
|
|
|
let output = this._generateLogMessage(aPrefix, aArgs);
|
2012-07-11 10:54:27 +04:00
|
|
|
Cu.reportError(output);
|
|
|
|
dump("ERROR: " + output + "\n");
|
|
|
|
for (let frame = Components.stack.caller; frame; frame = frame.caller) {
|
|
|
|
dump(frame + "\n");
|
|
|
|
}
|
2012-07-04 01:53:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
};
|
2013-03-27 19:19:09 +04:00
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
this.Logger = new IdentityLogger();
|