nsIConsoleService
From MDC
Contents |
[edit]
Summary
The console service is the back-end part of the JavaScript Console tool, bundled with every Mozilla application. It is used to log various messages, warnings, and errors and to obtain the logged messages.
Interface definition: xpcom/base/nsIConsoleService.idl
Contract ID: @mozilla.org/consoleservice;1
This interface is not frozen and is likely to change in future (bug 228205).
[edit]
Examples
[edit]
Logging a simple message
A common usage is to log a string message to console:
function LOG(msg) {
var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage(msg);
}
Alternative logging methods include Components.utils.reportError and dump().
[edit]
Logging a message with additional information
To include other information such as source file and line number, more complex code must be used.
function myLogToConsole(aMessage, aSourceName, aSourceLine, aLineNumber,
aColumnNumber, aFlags, aCategory)
{
var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService);
var scriptError = Components.classes["@mozilla.org/scripterror;1"]
.createInstance(Components.interfaces.nsIScriptError);
scriptError.init(aMessage, aSourceName, aSourceLine, aLineNumber,
aColumnNumber, aFlags, aCategory);
consoleService.logMessage(scriptError);
}
aMessage— the string to be logged. You must provide this.aSourceName— the URL of file with error. This will be a hyperlink in the JavaScript Console, so you'd better use real URL. You may passnullif it's not applicable.aSourceLine— the line #aLineNumberfromaSourceNamefile. You are responsible for providing that line. You may passnullif you are lazy; that will prevent showing the source line in JavaScript Console.aLineNumberandaColumnNumber— specify the exact location of error.aColumnNumberis used to draw the arrow pointing to the problem character.aFlags— one of flags declared innsIScriptError. At the time of writing, possible values are:nsIScriptError.errorFlag = 0,nsIScriptError.warningFlag = 1,nsIScriptError.exceptionFlag = 2andnsIScriptError.strictFlag = 4.aCategory— a string indicating what kind of code caused the message. There are quite a few category strings and they don't seem to be listed in a single place. Hopefully, they will all be listed in nsIScriptError.idl eventually.
Categories: Interfaces | NeedsContent