MozillaZine

Dev : Tips : Printing to JSConsole

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 21:46, 11 July 2004
Torisugari (Talk | contribs)

<-- Previous diff
Revision as of 23:59, 13 August 2004
Asqueella (Talk | contribs)
(markup + link)
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:
-*Declare global variable gConsoleService:+<ul>
 +<li>Declare global variable gConsoleService:
var gConsoleService; var gConsoleService;
-*Initialize it [[Dev : Extensions : onLoad|on load]]:+</li>
 +<li>Initialize it [[Dev : Extensions : onLoad|on load]]:
gConsoleService = Components.classes['@mozilla.org/consoleservice;1'] gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
.getService(Components.interfaces.nsIConsoleService); .getService(Components.interfaces.nsIConsoleService);
(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)
-*Define a function:+</li>
 +<li>Define a function:
function MyApp_logMessage(aMessage) { function MyApp_logMessage(aMessage) {
gConsoleService.logStringMessage('MyApp: ' + aMessage); gConsoleService.logStringMessage('MyApp: ' + aMessage);
} }
-*Use it like here:+</li>
 +<li>Use it like here:
MyApp_logMessage('Initialized.'); MyApp_logMessage('Initialized.');
- +</li>
-(thanks to clav, velcrospud and sage.mozdev.org for this)+</ul>
See also: [[Dev : Tips : Enabling dump|Using dump()]] See also: [[Dev : Tips : Enabling dump|Using dump()]]
 +
 +Reference: [http://www.mozilla.org/projects/xpcom/using-consoleservice.html]

Revision as of 23:59, 13 August 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()

Reference: [1]