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