Backed out changeset e086e888bd8d (bug 879228), on the suspicion of causing a b2g mochitest-3 failure, since the failure mentions 'console is undefined' and this patch touched Console.jsm.

CLOSED TREE
This commit is contained in:
Daniel Holbert 2013-06-18 22:45:12 -07:00
parent 838a193e63
commit aa61defc0d

View File

@ -20,7 +20,7 @@
* implementation isn't always required (or even well defined)
*/
this.EXPORTED_SYMBOLS = [ "console", "ConsoleAPI" ];
this.EXPORTED_SYMBOLS = [ "console" ];
const Cu = Components.utils;
@ -272,41 +272,6 @@ function logProperty(aProp, aValue) {
return reply;
}
const LOG_LEVELS = {
"all": Number.MIN_VALUE,
"debug": 2,
"log": 3,
"info": 3,
"trace": 3,
"timeEnd": 3,
"time": 3,
"group": 3,
"groupEnd": 3,
"dir": 3,
"dirxml": 3,
"warn": 4,
"error": 5,
"off": Number.MAX_VALUE,
};
/**
* Helper to tell if a console message of `aLevel` type
* should be logged in stdout and sent to consoles given
* the current maximum log level being defined in `console.maxLogLevel`
*
* @param {string} aLevel
* Console message log level
* @param {string} aMaxLevel {string}
* String identifier (See LOG_LEVELS for possible
* values) that allows to filter which messages
* are logged based on their log level
* @return {boolean}
* Should this message be logged or not?
*/
function shouldLog(aLevel, aMaxLevel) {
return LOG_LEVELS[aMaxLevel] <= LOG_LEVELS[aLevel];
}
/**
* Parse a stack trace, returning an array of stack frame objects, where
* each has filename/lineNumber/functionName members
@ -423,25 +388,6 @@ function stopTimer(aName, aTimestamp) {
return { name: aName, duration: duration };
}
/**
* Dump a new message header to stdout by taking care of adding an eventual
* prefix
*
* @param {object} aConsole
* ConsoleAPI instance
* @param {string} aLevel
* The string identifier for the message log level
* @param {string} aMessage
* The string message to print to stdout
*/
function dumpMessage(aConsole, aLevel, aMessage) {
aConsole.dump(
"console." + aLevel + ": " +
aConsole.prefix +
aMessage + "\n"
);
}
/**
* Create a function which will output a concise level of output when used
* as a logging function
@ -455,16 +401,13 @@ function dumpMessage(aConsole, aLevel, aMessage) {
*/
function createDumper(aLevel) {
return function() {
if (!shouldLog(aLevel, this.maxLogLevel)) {
return;
}
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
sendConsoleAPIMessage(aLevel, frame, args);
let data = args.map(function(arg) {
return stringify(arg);
});
dumpMessage(this, aLevel, data.join(", "));
dump("console." + aLevel + ": " + data.join(", ") + "\n");
};
}
@ -481,16 +424,13 @@ function createDumper(aLevel) {
*/
function createMultiLineDumper(aLevel) {
return function() {
if (!shouldLog(aLevel, this.maxLogLevel)) {
return;
}
dumpMessage(this, aLevel, "");
dump("console." + aLevel + ": \n");
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
sendConsoleAPIMessage(aLevel, frame, args);
args.forEach(function(arg) {
this.dump(log(arg));
}, this);
dump(log(arg));
});
};
}
@ -556,32 +496,9 @@ function sendConsoleAPIMessage(aLevel, aFrame, aArgs, aOptions = {})
/**
* This creates a console object that somewhat replicates Firebug's console
* object
*
* @param {object} aConsoleOptions
* Optional dictionary with a set of runtime console options:
* - prefix {string} : An optional prefix string to be printed before
* the actual logged message
* - maxLogLevel {string} : String identifier (See LOG_LEVELS for
* possible values) that allows to filter which
* messages are logged based on their log level.
* If falsy value, all messages will be logged.
* If wrong value that doesn't match any key of
* LOG_LEVELS, no message will be logged
* - dump {function} : An optional function to intercept all strings
* written to stdout
* @return {object}
* A console API instance object
* object.
*/
function ConsoleAPI(aConsoleOptions = {}) {
// Normalize console options to set default values
// in order to avoid runtime checks on each console method call.
this.dump = aConsoleOptions.dump || dump;
this.prefix = aConsoleOptions.prefix || "";
this.maxLogLevel = aConsoleOptions.maxLogLevel || "all";
}
ConsoleAPI.prototype = {
this.console = {
debug: createMultiLineDumper("debug"),
log: createDumper("log"),
info: createDumper("info"),
@ -590,14 +507,11 @@ ConsoleAPI.prototype = {
exception: createMultiLineDumper("error"),
trace: function Console_trace() {
if (!shouldLog("trace", this.maxLogLevel)) {
return;
}
let args = Array.prototype.slice.call(arguments, 0);
let trace = getStack(Components.stack.caller);
sendConsoleAPIMessage("trace", trace[0], args,
{ stacktrace: trace });
dumpMessage(this, "trace", "\n" + formatTrace(trace));
dump("console.trace:\n" + formatTrace(trace) + "\n");
},
clear: function Console_clear() {},
@ -607,28 +521,18 @@ ConsoleAPI.prototype = {
groupEnd: createDumper("groupEnd"),
time: function Console_time() {
if (!shouldLog("time", this.maxLogLevel)) {
return;
}
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
let timer = startTimer(args[0]);
sendConsoleAPIMessage("time", frame, args, { timer: timer });
dumpMessage(this, "time",
"'" + timer.name + "' @ " + (new Date()));
dump("console.time: '" + timer.name + "' @ " + (new Date()) + "\n");
},
timeEnd: function Console_timeEnd() {
if (!shouldLog("timeEnd", this.maxLogLevel)) {
return;
}
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
let timer = stopTimer(args[0]);
sendConsoleAPIMessage("timeEnd", frame, args, { timer: timer });
dumpMessage(this, "timeEnd",
"'" + timer.name + "' " + timer.duration + "ms");
dump("console.timeEnd: '" + timer.name + "' " + timer.duration + "ms\n");
},
};
const console = new ConsoleAPI();