nsIConsoleService

Error Console在Firefox中已经过期,只有将devtools.errorconsole.enabled 设为 true才能使用. 对于web应用使用 Web Console 代替, 对于浏览器中的chrome应用,使用 Browser Console 代替 .

console service是Error Console 后端的一部分, 与每一个Mozilla应用绑定在一起. 它用来记录各种消息、警告以及错误,同时可获取所有已经记录的消息
继承于: nsISupports 最后修改于Gecko 1.9 (Firefox 3)

实现方式: @mozilla.org/consoleservice;1 as a service:

var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                     .getService(Components.interfaces.nsIConsoleService);

Method overview

void getMessageArray([array, size_is(count)] out nsIConsoleMessage messages, out uint32_t count);已废弃 Gecko 19
void getMessageArray([optional] out uint32_t count, [retval, array, size_is(count)] out nsIConsoleMessage messages);
void logMessage(in nsIConsoleMessage message);
void logStringMessage(in wstring message);
void registerListener(in nsIConsoleListener listener);
void reset();
void unregisterListener(in nsIConsoleListener listener);

Methods

getMessageArray()

获取有关目前所有控制台记录的信息的数组

已废弃 Gecko 19 (Firefox 19 / Thunderbird 19 / SeaMonkey 2.16)

void getMessageArray(
  [array, size_is(count)] out nsIConsoleMessage messages,
  out PRUint32 count
);
Parameters
messages
已经记录的消息数组
count
数组中消息的数。如果没有消息被记录,函数会返回0,但同时仍会为message分配一个标记,在从脚本调用时,表示返回的一个长度为0的消息。

void getMessageArray(
  [optional] out PRUint32 count,
  [retval, array, size_is(count)] out nsIConsoleMessage messages
);
Parameters
count
The number of messages in the array. If no messages are logged, this function will return a count of 0 but still will allocate one word for messages, so as to show up as a 0-length array when called from script.

Note: See the code samples below for how to call getMessageArray.

logMessage()

void logMessage(
  in nsIConsoleMessage message
);
Parameters
message
An nsIConsoleMessage to log.

logStringMessage()

Convenience method for logging simple messages.

void logStringMessage(
  in wstring message
);
Parameters
message
The string to log.

registerListener()

Registers a listener for notification when an error is logged.

Note: To guard against stack overflows from listeners that could log messages (it is easy to do this inadvertently from listeners implemented in JavaScript), we do not call any listeners when another error is already being logged.

void registerListener(
  in nsIConsoleListener listener
);
Parameters
listener
The nsIConsoleListener to add.

Requires Gecko 1.9 (Firefox 3)

reset()

Clear the message buffer (For example, for privacy reasons).

void reset();
Parameters

None.

Note: Console listeners expect you to log an empty string message before calling reset so that they can clear their message buffers too. (This works before Gecko 1.9 too of course.)

unregisterListener()

Unregisters a listener.

void unregisterListener(
  in nsIConsoleListener listener
);
Parameters
listener
The nsIConsoleListener to remove.

Examples

Retrieving the message array

To retrieve the message array in Gecko prior to version 19:

function getConsoleMessageArray() {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  var array = {};
  consoleService.getMessageArray(array, {});
  return array.value;
}

To retrieve the message array in Gecko 19 or later:

function getConsoleMessageArray() {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  return consoleService.getMessageArray();
}

To retrieve the message array in a compatible way:

function getConsoleMessageArray() {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  var array = {};
  return consoleService.getMessageArray(array, {}) || array.value;
}

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().

Logging a message with additional information

To include other information an nsIConsoleMessage object must be used. In this example nsIScriptError, which implements nsIConsoleMessage, is used to include information about the source file and line number of the error.

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 Error Console, so you'd better use real URL. You may pass null if it's not applicable.
  • aSourceLine — the line #aLineNumber from aSourceName file. You are responsible for providing that line. You may pass null if you are lazy; that will prevent showing the source line in Error Console.
  • aLineNumber and aColumnNumber — specify the exact location of error. aColumnNumber is used to draw the arrow pointing to the problem character.
  • aFlags — one of flags declared in nsIScriptError. At the time of writing, possible values are:
    • nsIScriptError.errorFlag = 0
    • nsIScriptError.warningFlag = 1
    • nsIScriptError.exceptionFlag = 2 and
    • nsIScriptError.strictFlag = 4.
  • aCategory — a string indicating what kind of code caused the message. There are quite a few category strings and they do not seem to be listed in a single place. Hopefully, they will all be listed in nsIScriptError.idl eventually.

See also

 

文档标签和贡献者

 此页面的贡献者: ziyunfei, donghao526
 最后编辑者: ziyunfei,