home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / DebugUtils.jsm < prev    next >
Text File  |  2012-06-08  |  2KB  |  85 lines

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2010 POTI, Inc.
  7.  * http://www.songbirdnest.com
  8.  *
  9.  * This file may be licensed under the terms of of the
  10.  * GNU General Public License Version 2 (the ``GPL'').
  11.  *
  12.  * Software distributed under the License is distributed
  13.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  14.  * express or implied. See the GPL for the specific language
  15.  * governing rights and limitations.
  16.  *
  17.  * You should have received a copy of the GPL along with this
  18.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  19.  * or write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  *
  22.  *=END SONGBIRD GPL
  23.  */
  24.  
  25. /**
  26.  * \file  DebugUtils.jsm
  27.  * \brief Javascript module providing some debug utility functions.
  28.  */
  29.  
  30. EXPORTED_SYMBOLS = ["DebugUtils"];
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Cr = Components.results
  35.  
  36. var DebugUtils = {
  37.   /**
  38.    * \brief Returns the value of NSPR_LOG_MODULES environment variable.
  39.    */
  40.   get logModules() {
  41.     let environment = Cc["@mozilla.org/process/environment;1"]
  42.                         .createInstance(Ci.nsIEnvironment);
  43.     let result = environment.get("NSPR_LOG_MODULES");
  44.  
  45.     // Memoize the result
  46.     this.__defineGetter__("logModules", function() result);
  47.     return this.logModules;
  48.   },
  49.  
  50.   /**
  51.    * \brief Generates a log function for a module.
  52.    *
  53.    * This will only return a logging function if your module is listed in the
  54.    * NSPR_LOG_MODULES environment variable with a log level of at least @level.
  55.    * Otherwise it will return a no-op function.  Note that log output will
  56.    * still go to stderr, not to NSPR_LOG_FILE as one might expect.
  57.    *
  58.    * \param module The NSPR log module to use
  59.    * \param level The minimum logging level, defaults to 3
  60.    */
  61.   generateLogFunction: function DebugUtils_generateLogFunction(module, level) {
  62.     let doLog = false;
  63.     let minLevel = 3;
  64.     if (arguments.length > 1) {
  65.       minLevel = parseInt(arguments[1]);
  66.     }
  67.     for each (let entry in this.logModules.split(/,/)) {
  68.       if (/(.*):(\d+)$/.test(entry) &&
  69.           (RegExp.$1 == module || RegExp.$1 == "all") &&
  70.           parseInt(RegExp.$2) >= minLevel) {
  71.         doLog = true;
  72.       }
  73.     }
  74.  
  75.     if (doLog) {
  76.       return function LOG(msg) {
  77.         dump(module + ": " + msg + "\n");
  78.       };
  79.     }
  80.     else {
  81.       return function LOG(msg) {};
  82.     }
  83.   }
  84. };
  85.