1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/LogUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,103 @@ 1.4 +/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +"use strict"; 1.11 + 1.12 +this.EXPORTED_SYMBOLS = ["Logger"]; 1.13 +const PREF_DEBUG = "toolkit.identity.debug"; 1.14 + 1.15 +const Cu = Components.utils; 1.16 +const Ci = Components.interfaces; 1.17 +const Cc = Components.classes; 1.18 +const Cr = Components.results; 1.19 + 1.20 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.21 +Cu.import("resource://gre/modules/Services.jsm"); 1.22 + 1.23 +function IdentityLogger() { 1.24 + Services.prefs.addObserver(PREF_DEBUG, this, false); 1.25 + this._debug = Services.prefs.getBoolPref(PREF_DEBUG); 1.26 + return this; 1.27 +} 1.28 + 1.29 +IdentityLogger.prototype = { 1.30 + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), 1.31 + 1.32 + observe: function observe(aSubject, aTopic, aData) { 1.33 + switch(aTopic) { 1.34 + case "nsPref:changed": 1.35 + this._debug = Services.prefs.getBoolPref(PREF_DEBUG); 1.36 + break; 1.37 + 1.38 + case "quit-application-granted": 1.39 + Services.prefs.removeObserver(PREF_DEBUG, this); 1.40 + break; 1.41 + 1.42 + default: 1.43 + this.log("Logger observer", "Unknown topic:", aTopic); 1.44 + break; 1.45 + } 1.46 + }, 1.47 + 1.48 + _generateLogMessage: function _generateLogMessage(aPrefix, args) { 1.49 + // create a string representation of a list of arbitrary things 1.50 + let strings = []; 1.51 + 1.52 + // XXX bug 770418 - args look like flattened array, not list of strings 1.53 + 1.54 + args.forEach(function(arg) { 1.55 + if (typeof arg === 'string') { 1.56 + strings.push(arg); 1.57 + } else if (typeof arg === 'undefined') { 1.58 + strings.push('undefined'); 1.59 + } else if (arg === null) { 1.60 + strings.push('null'); 1.61 + } else { 1.62 + try { 1.63 + strings.push(JSON.stringify(arg, null, 2)); 1.64 + } catch(err) { 1.65 + strings.push("<<something>>"); 1.66 + } 1.67 + } 1.68 + }); 1.69 + return 'Identity ' + aPrefix + ': ' + strings.join(' '); 1.70 + }, 1.71 + 1.72 + /** 1.73 + * log() - utility function to print a list of arbitrary things 1.74 + * 1.75 + * Enable with about:config pref toolkit.identity.debug 1.76 + */ 1.77 + log: function log(aPrefix, ...args) { 1.78 + if (!this._debug) { 1.79 + return; 1.80 + } 1.81 + let output = this._generateLogMessage(aPrefix, args); 1.82 + dump(output + "\n"); 1.83 + 1.84 + // Additionally, make the output visible in the Error Console 1.85 + Services.console.logStringMessage(output); 1.86 + }, 1.87 + 1.88 + /** 1.89 + * reportError() - report an error through component utils as well as 1.90 + * our log function 1.91 + */ 1.92 + reportError: function reportError(aPrefix, ...aArgs) { 1.93 + let prefix = aPrefix + ' ERROR'; 1.94 + 1.95 + // Report the error in the browser 1.96 + let output = this._generateLogMessage(aPrefix, aArgs); 1.97 + Cu.reportError(output); 1.98 + dump("ERROR: " + output + "\n"); 1.99 + for (let frame = Components.stack.caller; frame; frame = frame.caller) { 1.100 + dump(frame + "\n"); 1.101 + } 1.102 + } 1.103 + 1.104 +}; 1.105 + 1.106 +this.Logger = new IdentityLogger();