2012-07-03 14:53:37 -07:00
|
|
|
/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
|
|
|
|
/* 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 08:19:09 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["Logger", "getLogger"];
|
2012-07-03 14:53:37 -07:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
function Logger(aIdentifier, aEnablingPref) {
|
|
|
|
this._identifier = aIdentifier;
|
|
|
|
this._enablingPref = aEnablingPref;
|
|
|
|
|
|
|
|
// Enabled by default if a pref for toggling the logger is not given
|
|
|
|
this._enabled = !this._enablingPref;
|
|
|
|
|
|
|
|
this.init();
|
2012-07-03 14:53:37 -07:00
|
|
|
}
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
Logger.prototype = {
|
2012-07-03 14:53:37 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
init: function Logger_init() {
|
|
|
|
if (this._enablingPref) {
|
|
|
|
Services.prefs.addObserver(this._enablingPref, this, false);
|
|
|
|
this._enabled = Services.prefs.getBoolPref(this._enablingPref);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-03 14:53:37 -07:00
|
|
|
observe: function observe(aSubject, aTopic, aData) {
|
2013-03-27 08:19:09 -07:00
|
|
|
switch (aTopic) {
|
2012-07-03 14:53:37 -07:00
|
|
|
case "nsPref:changed":
|
2013-03-27 08:19:09 -07:00
|
|
|
this._enabled = Services.prefs.getBoolPref(this._enablingPref);
|
|
|
|
dump("LogUtils " +
|
|
|
|
(this._enabled ? "enabled" : "disabled") +
|
|
|
|
" for " + this._identifier + "\n");
|
2012-07-03 14:53:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "quit-application-granted":
|
2013-03-27 08:19:09 -07:00
|
|
|
Services.prefs.removeObserver(this._enablingPref, this);
|
2012-07-03 14:53:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.log("Logger observer", "Unknown topic:", aTopic);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
_generatePrefix: function _generatePrefix() {
|
|
|
|
let caller = Components.stack.caller.caller;
|
|
|
|
let parts = ['[' + this._identifier + ']'];
|
|
|
|
|
|
|
|
// filename could be like path/to/foo.js or Scratchpad/1
|
|
|
|
if (caller.filename) {
|
|
|
|
let path = caller.filename.split('/');
|
|
|
|
if (path[path.length - 1].match(/\./)) {
|
|
|
|
parts.push(path[path.length - 1])
|
|
|
|
} else {
|
|
|
|
parts.push(caller.filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Might not be called from a function; might be top-level
|
|
|
|
if (caller.name) {
|
|
|
|
parts.push(caller.name + '()');
|
|
|
|
}
|
2012-07-03 14:53:37 -07:00
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
parts.push('line ' + caller.lineNumber + ': ');
|
2012-07-03 14:53:37 -07:00
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
return parts.join(' ');
|
|
|
|
},
|
|
|
|
|
|
|
|
_generateLogMessage: function _generateLogMessage(severity, argList) {
|
|
|
|
let strings = [];
|
|
|
|
argList.forEach(function(arg) {
|
|
|
|
if (arg === null) {
|
2012-07-03 14:53:37 -07:00
|
|
|
strings.push('null');
|
|
|
|
} else {
|
2013-03-27 08:19:09 -07:00
|
|
|
switch (typeof arg) {
|
|
|
|
case 'string':
|
|
|
|
strings.push(arg);
|
|
|
|
break;
|
|
|
|
case 'undefined':
|
|
|
|
strings.push('undefined');
|
|
|
|
break;
|
|
|
|
case 'function':
|
|
|
|
strings.push('<<function>>');
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
try {
|
|
|
|
strings.push(JSON.stringify(arg, null, 2));
|
|
|
|
} catch (err) {
|
|
|
|
strings.push('<<object>>');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
try {
|
|
|
|
strings.push(arg.toString());
|
|
|
|
} catch (err) {
|
|
|
|
strings.push('<<something>>');
|
|
|
|
}
|
|
|
|
break;
|
2012-07-03 14:53:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-03-27 08:19:09 -07:00
|
|
|
return strings.join(' ');
|
2012-07-03 14:53:37 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* log() - utility function to print a list of arbitrary things
|
|
|
|
*
|
|
|
|
* Enable with about:config pref toolkit.identity.debug
|
|
|
|
*/
|
2013-03-27 08:19:09 -07:00
|
|
|
log: function log(...argList) {
|
|
|
|
if (!this._enabled) {
|
2012-07-03 14:53:37 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-03-27 08:19:09 -07:00
|
|
|
let output = this._generatePrefix() + this._generateLogMessage('info', argList);
|
2012-07-03 14:53:37 -07:00
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
// print to the shell console and the browser error console
|
|
|
|
dump(output + "\n");
|
2012-07-03 14:53:37 -07:00
|
|
|
Services.console.logStringMessage(output);
|
|
|
|
},
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
warning: function Logger_warning(...argList) {
|
|
|
|
if (!this._enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = this._generatePrefix() + this._generateLogMessage('warning', argList);
|
|
|
|
},
|
|
|
|
|
2012-07-03 14:53:37 -07:00
|
|
|
/**
|
2013-03-27 08:19:09 -07:00
|
|
|
* error() - report an error through component utils as well as
|
2012-07-03 14:53:37 -07:00
|
|
|
* our log function
|
|
|
|
*/
|
2013-03-27 08:19:09 -07:00
|
|
|
error: function Logger_error(...argList) {
|
|
|
|
if (!this._enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-03 14:53:37 -07:00
|
|
|
|
|
|
|
// Report the error in the browser
|
2013-03-27 08:19:09 -07:00
|
|
|
let output = this._generatePrefix() + this._generateLogMessage('error', argList);
|
2012-07-10 23:54:27 -07:00
|
|
|
Cu.reportError(output);
|
2013-03-27 08:19:09 -07:00
|
|
|
|
|
|
|
// print to the console
|
2012-07-10 23:54:27 -07:00
|
|
|
dump("ERROR: " + output + "\n");
|
2013-03-27 08:19:09 -07:00
|
|
|
dump(" traceback follows:\n");
|
2012-07-10 23:54:27 -07:00
|
|
|
for (let frame = Components.stack.caller; frame; frame = frame.caller) {
|
|
|
|
dump(frame + "\n");
|
|
|
|
}
|
2012-07-03 14:53:37 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-27 08:19:09 -07:00
|
|
|
/**
|
|
|
|
* let logger = getLogger('my component', 'toolkit.foo.debug');
|
|
|
|
* logger.log("I would like", 42, "pies", {'and-some': 'object'});
|
|
|
|
*/
|
|
|
|
|
|
|
|
let _loggers = {};
|
|
|
|
|
|
|
|
this.getLogger = function(aIdentifier, aEnablingPref) {
|
|
|
|
let key = aIdentifier;
|
|
|
|
if (aEnablingPref) {
|
|
|
|
key = key + '-' + aEnablingPref;
|
|
|
|
}
|
|
|
|
if (!_loggers[key]) {
|
|
|
|
_loggers[key] = new Logger(aIdentifier, aEnablingPref);
|
|
|
|
}
|
|
|
|
return _loggers[key];
|
|
|
|
}
|