MozillaZine

Error Console

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 13:18, 4 October 2004
Asqueella (Talk | contribs)
(change formatting to <pre>)
<-- Previous diff
Revision as of 10:54, 31 October 2004
Asqueella (Talk | contribs)
(tweak the code; add a link to JS coding guidelines)
Next diff -->
Line 1: Line 1:
You can output information to console using the following method: You can output information to console using the following method:
-<ul>+<ol>
-<li>Declare global variable gConsoleService:+<li>Declare a global variable (but be sure to use an [[Dev : Javascript coding guidelines|unique identifier]]!):
-<pre>var gConsoleService;</pre>+<pre>var gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
-</li>+
-<li>Initialize it [[Dev : Extensions : onLoad|on load]]:+
-<pre>gConsoleService = Components.classes['@mozilla.org/consoleservice;1']+
.getService(Components.interfaces.nsIConsoleService);</pre> .getService(Components.interfaces.nsIConsoleService);</pre>
-(note, you can also use toJavaScriptConsole() function to open JavaScript console on startup)+(note, you can also use <tt>toJavaScriptConsole()</tt> function to open JavaScript console on startup)
</li> </li>
 +
<li>Define a function: <li>Define a function:
-<pre>function MyApp_logMessage(aMessage) {+<pre>function myExt_logMessage(aMessage) {
- gConsoleService.logStringMessage('MyApp: ' + aMessage);+ gConsoleService.logStringMessage('My extension: ' + aMessage);
}</pre> }</pre>
 +
</li> </li>
<li>Use it like here: <li>Use it like here:
-<pre>MyApp_logMessage('Initialized.');</pre>+<pre>myExt_logMessage("Initialized.");</pre>
</li> </li>
-</ul>+</ol>
See also: [[Dev : Tips : Enabling dump|Using dump()]], [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console Service]. See also: [[Dev : Tips : Enabling dump|Using dump()]], [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console Service].

Revision as of 10:54, 31 October 2004

You can output information to console using the following method:

  1. Declare a global variable (but be sure to use an unique identifier!):
    var gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                        .getService(Components.interfaces.nsIConsoleService);

    (note, you can also use toJavaScriptConsole() function to open JavaScript console on startup)

  2. Define a function:
    function myExt_logMessage(aMessage) {
      gConsoleService.logStringMessage('My extension: ' + aMessage);
    }
  3. Use it like here:
    myExt_logMessage("Initialized.");

See also: Using dump(), Using Console Service.