nsIConsoleService 编辑

xpcom/base/nsIConsoleService.idlScriptable The console service is the back-end for the Error Console, bundled with every Mozilla application, and for Firefox's Web Console and Browser Console. It is used to log various messages, warnings, and errors and to obtain the logged messages. Inherits from: nsISupports Last changed in Gecko 19 (Firefox 19 / Thunderbird 19 / SeaMonkey 2.16)

Implemented by: @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);Obsolete since 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()

To obtain an array of all logged messages.

Obsolete since Gecko 19 (Firefox 19 / Thunderbird 19 / SeaMonkey 2.16)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

void getMessageArray(
  [array, size_is(count)] out nsIConsoleMessage messages,
  out PRUint32 count
);
Parameters
messages
An array of logged messages.
count
The number of messages in the array. If no messages are logged, this function will return a count of 0, but allocating a placeholder word for messages, showing as a 0-length array when called from the script.
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 allocating a placeholder word for messages, showing as a 0-length array when called from the script.
Note: See the code samples below for how to call getMessageArray.

logMessage()

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

logStringMessage()

A convenient method for logging simple messages.

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

registerListener()

Registers a listener, to notify when an error is logged.

Note: To guard against stack overflows from listeners which could log messages (this could be done inadvertently through 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.

reset()

To 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 they too can clear their message buffers. (This also works before Gecko 19.)

unregisterListener()

Deregisters 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 logging a message string to the 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 for a file associated with an 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 this line. You may pass null if you are lazy; this will prevent the source line showing in the Error Console.
  • aLineNumber and aColumnNumber — specify the exact location of an 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 error message. There are quite a few category strings and they do not currently seem to be listed in a single place. Hopefully, they will all eventually be listed in nsIScriptError.idl.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:76 次

字数:11999

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文