toolkit/identity/LogUtils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial