Dev : Tips : Printing to JSConsole: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(toJavaScriptConsole)
m (naming convention)
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 aConsoleService:
*Declare global variable gConsoleService:
  var aConsoleService;
  var gConsoleService;
*Initialize it [[Dev : Extensions : onLoad|on load]]:
*Initialize it [[Dev : Extensions : onLoad|on load]]:
  aConsoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
  gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                    .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:
*Define a function:
  function logMessage(message) {
  function _MyApp_logMessage(message) {
   aConsoleService.logStringMessage('MyApp: ' + message);
   gConsoleService.logStringMessage('MyApp: ' + message);
  }
  }
*Use it like here:
*Use it like here:
  logMessage('Initialized.');
  _MyApp_logMessage('Initialized.');


(thanks to clav, velcrospud and sage.mozdev.org for this)
(thanks to clav, velcrospud and sage.mozdev.org for this)


See also: [[Dev : Tips : Enabling dump|Using dump()]]
See also: [[Dev : Tips : Enabling dump|Using dump()]]

Revision as of 21:29, 11 July 2004

You can output information to console using the following method:

  • Declare global variable gConsoleService:
var gConsoleService;
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(message) {
  gConsoleService.logStringMessage('MyApp: ' + message);
}
  • Use it like here:
_MyApp_logMessage('Initialized.');

(thanks to clav, velcrospud and sage.mozdev.org for this)

See also: Using dump()