MozillaZine

Error Console

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 13:01, 4 October 2004
Asqueella (Talk | contribs)
(move from Dev:Tips)
<-- Previous diff
Revision as of 13:18, 4 October 2004
Asqueella (Talk | contribs)
(change formatting to <pre>)
Next diff -->
Line 3: Line 3:
<ul> <ul>
<li>Declare global variable gConsoleService: <li>Declare global variable gConsoleService:
- var gConsoleService;+<pre>var gConsoleService;</pre>
</li> </li>
<li>Initialize it [[Dev : Extensions : onLoad|on load]]: <li>Initialize it [[Dev : Extensions : onLoad|on load]]:
- gConsoleService = Components.classes['@mozilla.org/consoleservice;1']+<pre>gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
- .getService(Components.interfaces.nsIConsoleService);+ .getService(Components.interfaces.nsIConsoleService);</pre>
(note, you can also use toJavaScriptConsole() function to open JavaScript console on startup) (note, you can also use toJavaScriptConsole() function to open JavaScript console on startup)
</li> </li>
<li>Define a function: <li>Define a function:
- function MyApp_logMessage(aMessage) {+<pre>function MyApp_logMessage(aMessage) {
- gConsoleService.logStringMessage('MyApp: ' + aMessage);+ gConsoleService.logStringMessage('MyApp: ' + aMessage);
- }+}</pre>
</li> </li>
<li>Use it like here: <li>Use it like here:
- MyApp_logMessage('Initialized.');+<pre>MyApp_logMessage('Initialized.');</pre>
</li> </li>
</ul> </ul>
-See also: [[Dev : Tips : Enabling dump|Using dump()]]+See also: [[Dev : Tips : Enabling dump|Using dump()]], [http://www.mozilla.org/projects/xpcom/using-consoleservice.html Using Console Service].
- +
-Reference: [http://www.mozilla.org/projects/xpcom/using-consoleservice.html]+

Revision as of 13:18, 4 October 2004

You can output information to console using the following method:

  • Declare global variable gConsoleService:
    var gConsoleService;
  • Initialize it on load:
    gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                        .getService(Components.interfaces.nsIConsoleService);

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

  • Define a function:
    function MyApp_logMessage(aMessage) {
      gConsoleService.logStringMessage('MyApp: ' + aMessage);
    }
  • Use it like here:
    MyApp_logMessage('Initialized.');

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