|
|
(3 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
| {{extdev}}
| | See [https://developer.mozilla.org/en/Error_Console Error Console] at developer.mozilla.org. |
|
| |
|
| ==What is JavaScript Console==
| | [[Category:Redirects]] |
| JavaScript Console is a tool available in most Mozilla-based applications that is used for reporting errors in the application chrome and in web pages user opens. Despite its name it reports not only [[JavaScript]]-related errors and warnings, but also [[CSS]] errors and arbitrary messages from chrome code.
| |
| | |
| [http://forums.mozillazine.org/viewtopic.php?t=318102 Console<sup>2</sup>] provides a replacement for JavaScript Console, fixing many of its bugs and implementing long-wanted enhancement requests.
| |
| | |
| ==Manipulating data displayed in JavaScript Console==
| |
| Information displayed in JavaScript Console can be accessed and manipulated through <code>nsIConsoleService</code> interface. To get it use the code similar to the following (you can read more about creating XPCOM components and interfaces [http://xulplanet.com/references/xpcomref/creatingcomps.html here]):
| |
| var consoleService = Components.classes['@mozilla.org/consoleservice;1']
| |
| .getService(Components.interfaces.nsIConsoleService);
| |
| | |
| ===Logging custom messages in JavaScript Console===
| |
| There are two functions in <code>nsIConsoleService</code> that may be used to log messages from your code: <code>logMessage()</code> and <code>logStringMessage()</code>. The former is more flexible, while the latter is easier to use. How to use each of them is explained below.
| |
| | |
| ====The easy way====
| |
| To print simple string messages to JavaScript Console, use the following code:
| |
| consoleService.logStringMessage("Hello world!");
| |
| | |
| You may want to create a helper function like this:
| |
| function myDump(aMessage) {
| |
| var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
| |
| .getService(Components.interfaces.nsIConsoleService);
| |
| consoleService.logStringMessage("My extension: " + aMessage);
| |
| }
| |
| You can also use the <code>dump()</code> function for that. For more details see [[Viewing dump() output]].
| |
| | |
| ====The advanced way====
| |
| The more tricky way is to pass an <code>nsIScriptError</code> instance to <code>nsIConsoleService.logMessage()</code> method:
| |
| | |
| 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);
| |
| }
| |
| | |
| The above function logs a message like this to JavaScript Console:
| |
| Warning: function goQuitApplication does not always return a value
| |
| Source File: chrome://global/content/globalOverlay.js Line: 68
| |
| return true;
| |
| --------------^
| |
| | |
| Now to the meaning of function's parameters:
| |
| *<code>aMessage</code> — the string to be logged. You must provide this.
| |
| *<code>aSourceName</code> — the URL of file with error. This will be a hyperlink in the JavaScript Console, so you'd better use real URL. You may pass <code>null</code> if it's not applicable.
| |
| *<code>aSourceLine</code> — the line #<code>aLineNumber</code> from <code>aSourceName</code> file. You are responsible for providing that line. You may pass <code>null</code> if you are lazy; that will prevent showing the source line in JavaScript Console.
| |
| *<code>aLineNumber</code> and <code>aColumnNumber</code> — specify the exact location of error. <code>aColumnNumber</code> is used to draw the arrow pointing to the problem character.
| |
| *<code>aFlags</code> — one of flags declared in <code>nsIScriptError</code>. At the time of writing, possible values are: <code>nsIScriptError.errorFlag = 0</code>, <code>nsIScriptError.warningFlag = 1</code>, <code>nsIScriptError.exceptionFlag = 2</code> and <code>nsIScriptError.strictFlag = 4</code>.
| |
| *<code>aCategory</code> — 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 <tt>nsIScriptError.idl</tt> eventually.
| |
| | |
| An example call, that generates the above warning:
| |
| myLogToConsole("function goQuitApplication does not always return a value",
| |
| "chrome://global/content/globalOverlay.js", " return true;", 68, 13,
| |
| Components.interfaces.nsIScriptError.warningFlag, "XUL javascript");
| |
| | |
| ==== Using <code>Components.utils.reportError</code> ====
| |
| In Firefox 1.5 and later (and other products) there's a new method, <code>Components.utils.reportError()</code>. It is intended to be used in a <code>catch</code> block to report caught errors in the JS Console, but you can also use it to log arbitrary messages. Note, that unlike <code>nsIConsoleService.log[String]Message()</code>, <code>Components.utils.reportError()</code> reports the message with severity "error".
| |
| | |
| Example 1. This is how <code>Components.utils.reportError()</code> is intended to be used.
| |
| try {
| |
| ...
| |
| } catch(e) {
| |
| // handle the exception
| |
| Components.utils.reportError(e); // still report it to the console
| |
| }
| |
| | |
| Example 2. This is a very easy way to print a string to the JS Console, which you can use for debugging, if you can't be bothered to obtain a reference to console service (as described above).
| |
| Components.utils.reportError("hi!")
| |
| | |
| Again, '''this only works on Firefox/Thunderbird 1.5 or later'''.
| |
| | |
| :''Note: during the 1.5 development cycle, this function was initially introduced as <code>Components.reportError()</code>. The access by older name is supported, but deprecated. It may be removed in the future versions.
| |
| | |
| ===Using console listeners===
| |
| :''This section is a stub. You can help MozillaZine knowledge base by writing it.''
| |
| * What are console listeners. When they are used (in JS Console implementation, for example).
| |
| * An example of console listener, registered with <code>nsIConsoleService.registerListener()</code>.
| |
| | |
| ===Accessing available messages===
| |
| :''This section is a stub. You can help MozillaZine knowledge base by writing it.''
| |
| * An example using <code>nsIConsoleService.getMessageArray()</code>.
| |
| * Possible performance problems.
| |
| | |
| ==Miscellaneous==
| |
| | |
| ===toJavaScriptConsole()===
| |
| If your code is executed in a browser or mail overlay, the <code>toJavaScriptConsole()</code> helper function is available. It opens the JavaScript Console, useful when debugging an extension.
| |
| | |
| ==Resources==
| |
| * [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console service document] at mozilla.org
| |
| * XulPlanet.com documents
| |
| ** [http://xulplanet.com/references/xpcomref/ifaces/nsIConsoleService.html nsIConsoleService]
| |
| ** [http://xulplanet.com/references/xpcomref/ifaces/nsIScriptError.html nsIScriptError]
| |
| | |
| [[Category:Development]] [[Category:Development tools]] [[Category:Example code]] [[Category:JavaScript example code]]
| |