PHP code
  1. <?php
  2. /**
  3.  * Log:: driver to write messages into FirePHP console
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * @category  Logging
  8.  * @package   Log
  9.  * @author    Laurent Laville <pear@laurent-laville.org>
  10.  * @copyright 2008 Laurent Laville
  11.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  12.  * @version   CVS: $Id:$
  13.  * @link      http://pear.php.net/package/Log
  14.  * @since     File available since Release 1.12.0
  15.  */
  16.  
  17. require_once 'FirePHPCore/FirePHP.class.php';
  18.  
  19. define('PEAR_LOG_FIREPHP_LOG', FirePHP::LOG);
  20. define('PEAR_LOG_FIREPHP_INFO', FirePHP::INFO);
  21. define('PEAR_LOG_FIREPHP_WARN', FirePHP::WARN);
  22. define('PEAR_LOG_FIREPHP_ERROR', FirePHP::ERROR);
  23. define('PEAR_LOG_FIREPHP_DUMP', FirePHP::DUMP);
  24. define('PEAR_LOG_FIREPHP_TRACE', FirePHP::TRACE);
  25. define('PEAR_LOG_FIREPHP_TABLE', FirePHP::TABLE);
  26. define('PEAR_LOG_FIREPHP_EXCEPTION', FirePHP::EXCEPTION);
  27. define('PEAR_LOG_FIREPHP_GROUP_START', FirePHP::GROUP_START);
  28. define('PEAR_LOG_FIREPHP_GROUP_END', FirePHP::GROUP_END);
  29.  
  30. /**
  31.  * The Log_firephp class is a concrete implementation of the Log::
  32.  * abstract class which writes message into FirePHP console.
  33.  *
  34.  * http://www.firephp.org/
  35.  *
  36.  * @category  Logging
  37.  * @package   Log
  38.  * @author    Laurent Laville <pear@laurent-laville.org>
  39.  * @copyright 2008 Laurent Laville
  40.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  41.  * @version   Release: 1.12.0
  42.  * @link      http://pear.php.net/package/Log
  43.  * @since     File available since Release 1.12.0
  44.  */
  45. class Log_firephp extends Log
  46. {
  47.     /**
  48.      * String containing the format of a log line.
  49.      * @var string
  50.      * @access private
  51.      */
  52.     var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  53.  
  54.     /**
  55.      * String containing the timestamp format.  It will be passed directly to
  56.      * strftime().  Note that the timestamp string will generated using the
  57.      * current locale.
  58.      *
  59.      * Note! Default lineFormat of this driver does not display time.
  60.      *
  61.      * @var string
  62.      * @access private
  63.      */
  64.     var $_timeFormat = '%b %d %H:%M:%S';
  65.  
  66.     /**
  67.      * Options for the FireCorePHP library
  68.      *
  69.      * @var array
  70.      */
  71.      var $_options = array();
  72.  
  73.     /**
  74.      * Constructs a new Log_firephp object.
  75.      *
  76.      * @param string $name  Ignored.
  77.      * @param string $ident The identity string.
  78.      * @param array  $conf  The configuration array.
  79.      * @param int    $level Log messages up to and including this level.
  80.      *
  81.      * @access public
  82.      */
  83.     function Log_firephp($name = '', $ident = 'PHP', $conf = array(),
  84.                          $level = PEAR_LOG_DEBUG)
  85.     {
  86.         $this->_id    = md5(microtime());
  87.         $this->_ident = $ident;
  88.         $this->_mask  = Log::UPTO($level);
  89.  
  90.         if (!empty($conf['lineFormat'])) {
  91.             $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  92.                                              array_values($this->_formatMap),
  93.                                              $conf['lineFormat']);
  94.         }
  95.  
  96.         if (!empty($conf['timeFormat'])) {
  97.             $this->_timeFormat = $conf['timeFormat'];
  98.         }
  99.  
  100.         $options = array();
  101.  
  102.         if (isset($conf['maxObjectDepth'])) {
  103.             $options['maxObjectDepth'] = $conf['maxObjectDepth'];
  104.         }
  105.         if (isset($conf['maxArrayDepth'])) {
  106.             $options['maxArrayDepth'] = $conf['maxArrayDepth'];
  107.         }
  108.         if (isset($conf['useNativeJsonEncode'])) {
  109.             $options['useNativeJsonEncode'] = $conf['useNativeJsonEncode'];
  110.         }
  111.         if (isset($conf['includeLineNumbers'])) {
  112.             $options['includeLineNumbers'] = $conf['includeLineNumbers'];
  113.         }
  114.  
  115.         // default FireCorePHP options
  116.         // @todo replace when a public getOptions() method will be available
  117.         $this->_options = array('maxObjectDepth' => 10,
  118.                                 'maxArrayDepth' => 20,
  119.                                 'useNativeJsonEncode' => true,
  120.                                 'includeLineNumbers' => true);
  121.  
  122.         $this->_options = array_merge($this->_options, $options);
  123.     }
  124.  
  125.     /**
  126.      * Writes $message to firephp console. Also, passes the message
  127.      * along to any Log_observer instances that are observing this Log.
  128.      *
  129.      * @param mixed  $message  String or object containing the message to log.
  130.      * @param string $priority The priority of the message.  Valid values are:
  131.      *                         PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  132.      *                         PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  133.      *                         PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  134.      *
  135.      * @return boolean  True on success or false on failure.
  136.      * @access public
  137.      */
  138.     function log($message, $priority = null)
  139.     {
  140.         /* If a priority hasn't been specified, use the default value. */
  141.         if ($priority === null) {
  142.             $priority = $this->_priority;
  143.         }
  144.  
  145.         /* Abort early if the priority is above the maximum logging level. */
  146.         if (!$this->_isMasked($priority)) {
  147.             return false;
  148.         }
  149.  
  150.         /* Extract valid FirePHP components from $message parameter */
  151.         if (is_array($message)) {
  152.             if (isset($message['type'])) {
  153.                 $type = $message['type'];
  154.             } else {
  155.                 $type = null;
  156.             }
  157.             if (isset($message['message'])) {
  158.                 $object = $message['message'];
  159.             } else {
  160.                 if ($type == PEAR_LOG_FIREPHP_LOG
  161.                     || $type == PEAR_LOG_FIREPHP_INFO
  162.                     || $type == PEAR_LOG_FIREPHP_WARN
  163.                     || $type == PEAR_LOG_FIREPHP_ERROR
  164.                     || $type == PEAR_LOG_FIREPHP_DUMP
  165.                     || $type == PEAR_LOG_FIREPHP_TABLE
  166.                 ) {
  167.                     /* "object" is mandatory for these FirePHP levels :
  168.                        log, info, warn, error, dump, table */
  169.                     return false;
  170.                 }
  171.                 $object = null;
  172.             }
  173.             if (isset($message['label'])) {
  174.                 $label = $message['label'];
  175.             } else {
  176.                 if ($type == PEAR_LOG_FIREPHP_TRACE) {
  177.                     /* "label" is mandatory for FirePHP trace level */
  178.                     return false;
  179.                 }
  180.                 $label = null;
  181.             }
  182.         } elseif (is_scalar($message)) {
  183.             $object = $message;
  184.             $label  = null;
  185.             $type   = PEAR_LOG_FIREPHP_LOG;
  186.         } elseif ($message instanceof Exception) {
  187.             $object = $message;
  188.             $label  = null;
  189.             $type   = PEAR_LOG_FIREPHP_EXCEPTION;
  190.         } else {
  191.             // $message parameter should be either :
  192.             // an array with key-values (object, label, type) or a PHP Exception
  193.             return false;
  194.         }
  195.  
  196.         if (!is_null($label)) {
  197.             /* Build the string containing the complete log line. */
  198.             $label = $this->_format($this->_lineFormat,
  199.                                     strftime($this->_timeFormat),
  200.                                     $priority,
  201.                                     $label);
  202.  
  203.         } elseif (!is_null($object) && is_string($object)) {
  204.             /* Extract the string representation of the message. */
  205.             $msg = $this->_extractMessage($object);
  206.  
  207.             /* normalize line breaks */
  208.             $msg = str_replace("\r\n", "\n", $msg);
  209.  
  210.             /* escape line breaks */
  211.             $msg = str_replace("\n", "\\n\\\n", $msg);
  212.  
  213.             /* escape quotes */
  214.             $msg = str_replace('"', '\\"', $msg);
  215.  
  216.             /* Build the string containing the complete log line. */
  217.             $object = $this->_format($this->_lineFormat,
  218.                                      strftime($this->_timeFormat),
  219.                                      $priority,
  220.                                      $msg);
  221.         }
  222.  
  223.         $firephp = FirePHP::getInstance(true);
  224.         $firephp->setOptions($this->_options);
  225.  
  226.         switch ($type) {
  227.         case PEAR_LOG_FIREPHP_LOG :
  228.         case PEAR_LOG_FIREPHP_INFO :
  229.         case PEAR_LOG_FIREPHP_WARN :
  230.         case PEAR_LOG_FIREPHP_ERROR :
  231.         case PEAR_LOG_FIREPHP_DUMP :
  232.         case PEAR_LOG_FIREPHP_TABLE :
  233.             $firephp->fb($object, $label, $type);
  234.             break;
  235.         case PEAR_LOG_FIREPHP_TRACE :
  236.             $firephp->fb($label, $type);
  237.             break;
  238.         case PEAR_LOG_FIREPHP_EXCEPTION :
  239.             $firephp->fb($object);
  240.             break;
  241.         case PEAR_LOG_FIREPHP_GROUP_START :
  242.             $firephp->group($label);
  243.             break;
  244.         case PEAR_LOG_FIREPHP_GROUP_END :
  245.             $firephp->groupEnd();
  246.             break;
  247.         default :
  248.             $firephp->fb($object, $label, PEAR_LOG_FIREPHP_LOG);
  249.         }
  250.  
  251.         if ($type == PEAR_LOG_FIREPHP_GROUP_START
  252.             || $type == PEAR_LOG_FIREPHP_GROUP_END) {
  253.             // do not inform other observers about a group open/close message
  254.         } else {
  255.             /* Notify observers about this log message. */
  256.             $this->_announce(array('priority' => $priority, 'message' => $message));
  257.         }
  258.  
  259.         return true;
  260.     }
  261.  
  262.     /**
  263.      * A convenience function for logging an info FirePHP event. It will log a
  264.      * message at the PEAR_LOG_INFO log level.
  265.      *
  266.      * @param mixed $message String or object containing the message to log.
  267.      *
  268.      * @return boolean True if the message was successfully logged.
  269.      *
  270.      * @access public
  271.      */
  272.     function info($message)
  273.     {
  274.         if (is_array($message) && !isset($message['type'])) {
  275.             $message['type'] = PEAR_LOG_FIREPHP_INFO;
  276.         }
  277.         return $this->log($message, PEAR_LOG_INFO);
  278.     }
  279.  
  280.     /**
  281.      * A convenience function for logging a warning FirePHP event. It will log a
  282.      * message at the PEAR_LOG_WARNING log level.
  283.      *
  284.      * @param mixed $message String or object containing the message to log.
  285.      *
  286.      * @return boolean True if the message was successfully logged.
  287.      *
  288.      * @access public
  289.      */
  290.     function warning($message)
  291.     {
  292.         if (is_array($message) && !isset($message['type'])) {
  293.             $message['type'] = PEAR_LOG_FIREPHP_WARN;
  294.         }
  295.         return $this->log($message, PEAR_LOG_WARNING);
  296.     }
  297.  
  298.     /**
  299.      * A convenience function for logging an error FirePHP event. It will log a
  300.      * message at the PEAR_LOG_ERR log level.
  301.      *
  302.      * @param mixed $message String or object containing the message to log.
  303.      *
  304.      * @return boolean True if the message was successfully logged.
  305.      *
  306.      * @access public
  307.      */
  308.     function err($message)
  309.     {
  310.         if (is_array($message) && !isset($message['type'])) {
  311.             $message['type'] = PEAR_LOG_FIREPHP_ERROR;
  312.         }
  313.         return $this->log($message, PEAR_LOG_ERR);
  314.     }
  315.  
  316.     /**
  317.      * A convenience function for logging a dump FirePHP event. It will log a
  318.      * message at the PEAR_LOG_DEBUG log level.
  319.      *
  320.      * @param mixed $message String or object containing the message to log.
  321.      *
  322.      * @return boolean True if the message was successfully logged.
  323.      *
  324.      * @access public
  325.      */
  326.     function dump($message)
  327.     {
  328.         if (is_array($message) && !isset($message['type'])) {
  329.             $message['type'] = PEAR_LOG_FIREPHP_DUMP;
  330.         }
  331.         return $this->log($message, PEAR_LOG_DEBUG);
  332.     }
  333.  
  334.     /**
  335.      * A convenience function for logging a trace FirePHP event. It will log a
  336.      * message at the PEAR_LOG_DEBUG log level.
  337.      *
  338.      * @param mixed $message String or object containing the message to log.
  339.      *
  340.      * @return boolean True if the message was successfully logged.
  341.      *
  342.      * @access public
  343.      */
  344.     function trace($message)
  345.     {
  346.         if (is_array($message) && !isset($message['type'])) {
  347.             $message['type'] = PEAR_LOG_FIREPHP_TRACE;
  348.         }
  349.         return $this->log($message, PEAR_LOG_DEBUG);
  350.     }
  351.  
  352.     /**
  353.      * A convenience function for logging a table FirePHP event. It will log a
  354.      * message at the PEAR_LOG_DEBUG log level (by default).
  355.      *
  356.      * @param mixed  $message  String or object containing the message to log.
  357.      * @param string $priority The priority of the message.  Valid values are:
  358.      *                         PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  359.      *                         PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  360.      *                         PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  361.      *
  362.      * @return boolean True if the message was successfully logged.
  363.      *
  364.      * @access public
  365.      */
  366.     function table($message, $priority = PEAR_LOG_DEBUG)
  367.     {
  368.         if (is_array($message) && !isset($message['type'])) {
  369.             $message['type'] = PEAR_LOG_FIREPHP_TABLE;
  370.         }
  371.         return $this->log($message, $priority);
  372.     }
  373.  
  374.     /**
  375.      * Register FirePHP driver as your error handler
  376.      *
  377.      * @return mixed Returns a string containing the previously defined error handler
  378.      *               (if any), or NULL on error. If the previous handler was
  379.      *               a class method, this function will return an indexed array
  380.      *               with the class and the method name.
  381.      * @access public
  382.      */
  383.     function registerErrorHandler()
  384.     {
  385.         return set_error_handler(array($this, 'errorHandler'));
  386.     }
  387.  
  388.     /**
  389.      * FirePHP's error handler
  390.      *
  391.      * Throws exception for each php error that will occur.
  392.      *
  393.      * @param int    $errno      contains the level of the error raised
  394.      * @param string $errstr     contains the error message
  395.      * @param string $errfile    contains the filename that the error was raised in
  396.      * @param int    $errline    contains the line number the error was raised at
  397.      * @param array  $errcontext contain an array of every variable that existed
  398.      *                           in the scope the error was triggered in
  399.      *
  400.      * @return void
  401.      * @access public
  402.      */
  403.     function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  404.     {
  405.         // Don't throw exception if error reporting is switched off
  406.         if (error_reporting() == 0) {
  407.             return;
  408.         }
  409.         // Only catch for errors we are asking for
  410.         if (error_reporting() & $errno) {
  411.  
  412.             /* Map the PHP error to a Log priority. */
  413.             switch ($errno) {
  414.             case E_WARNING:
  415.             case E_USER_WARNING:
  416.                 $priority = PEAR_LOG_WARNING;
  417.                 $type     = PEAR_LOG_FIREPHP_WARN;
  418.                 break;
  419.             case E_NOTICE:
  420.             case E_USER_NOTICE:
  421.                 $priority = PEAR_LOG_NOTICE;
  422.                 $type     = PEAR_LOG_FIREPHP_WARN;
  423.                 break;
  424.             case E_ERROR:
  425.             case E_USER_ERROR:
  426.                 $priority = PEAR_LOG_ERR;
  427.                 $type     = PEAR_LOG_FIREPHP_ERROR;
  428.                 break;
  429.             default:
  430.                 $priority = PEAR_LOG_INFO;
  431.                 $type     = PEAR_LOG_FIREPHP_INFO;
  432.             }
  433.  
  434.             $this->log(array('message' => $errstr
  435.                                        . ' in ' . $errfile
  436.                                        . ' at line ' . $errline,
  437.                              'type' => $type), $priority);
  438.         }
  439.     }
  440.  
  441.     /**
  442.      * Register FirePHP as your exception handler
  443.      *
  444.      * @return string|null Returns the name of the previously defined
  445.      *                     exception handler, or NULL on error. If no  previous
  446.      *                     handler was defined, NULL is also returned.
  447.      * @access public
  448.      */
  449.     function registerExceptionHandler()
  450.     {
  451.         return set_exception_handler(array($this, 'exceptionHandler'));
  452.     }
  453.  
  454.     /**
  455.      * FirePHP's exception handler
  456.      *
  457.      * Logs all exceptions to your firebug console.
  458.      *
  459.      * @param exception $exception contains the exception raised
  460.      *
  461.      * @return void
  462.      * @access public
  463.      */
  464.     function exceptionHandler($exception)
  465.     {
  466.         $this->log($exception, PEAR_LOG_ALERT);
  467.     }
  468.  
  469.     /**
  470.      * Register FirePHP driver as your assert callback
  471.      *
  472.      * @return mixed Returns the original setting or FALSE on errors
  473.      * @access public
  474.      */
  475.     function registerAssertionHandler()
  476.     {
  477.         return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler'));
  478.     }
  479.  
  480.     /**
  481.      * FirePHP's assertion handler
  482.      *
  483.      * Logs all assertions to your firebug console and then stops the script.
  484.      *
  485.      * @param string $file File source of assertion
  486.      * @param int    $line Line source of assertion
  487.      * @param mixed  $code Assertion code
  488.      *
  489.      * @return void
  490.      * @access public
  491.      */
  492.     function assertionHandler($file, $line, $code)
  493.     {
  494.         $this->log(array('message' => 'Assertion Failed: '. $code
  495.                                    . ' in ' . $file
  496.                                    . ' at line ' . $line,
  497.                          'type' => PEAR_LOG_FIREPHP_ERROR), PEAR_LOG_ERR);
  498.     }
  499. }
  500. ?>
generated by Generic Syntax Highlighter - GeSHi 1.0.8.1