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