[ Index ]
 

Code source de XOOPS 2.0.17.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/htdocs/class/ -> logger.php (source)

   1  <?php
   2  /**

   3   * XoopsLogger component main class file

   4   *

   5   * See the enclosed file LICENSE for licensing information.

   6   * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html

   7   *

   8   * @copyright    The XOOPS project http://www.xoops.org/

   9   * @license        http://www.fsf.org/copyleft/gpl.html GNU public license

  10   * @author        Kazumi Ono  <onokazu@xoops.org>

  11   * @author        Skalpa Keo <skalpa@xoops.org>

  12   * @since        2.0

  13   * @package        xos_kernel

  14   * @subpackage    XoopsLogger

  15   * @version        $Id: logger.php 694 2006-09-04 11:33:22Z skalpa $

  16   */
  17  
  18  /**

  19   * Collects information for a page request

  20   *

  21   * Records information about database queries, blocks, and execution time

  22   * and can display it as HTML. It also catches php runtime errors.

  23   * @package xos_kernel

  24   */
  25  class XoopsLogger {
  26      /**#@+

  27       * @var array

  28       */
  29      var $queries = array();
  30      var $blocks = array();
  31      var $extra = array();
  32      var $logstart = array();
  33      var $logend = array();
  34      var $errors = array();
  35      /**#@-*/

  36  
  37      var $usePopup = false;
  38      var $activated = true;
  39      
  40      /**@access protected*/

  41      var $renderingEnabled = false;
  42      
  43      function XoopsLogger() {
  44      }
  45      /**

  46       * Get a reference to the only instance of this class

  47       * @return  object XoopsLogger  reference to the only instance

  48       */
  49      function &instance() {
  50          static $instance;
  51  
  52          if ( !isset( $instance ) ) {
  53              $instance = new XoopsLogger();
  54              // Always catch errors, for security reasons

  55              set_error_handler( 'XoopsErrorHandler_HandleError' );
  56          }
  57          return $instance;
  58      }
  59      /**

  60       * Enable logger output rendering

  61       * When output rendering is enabled, the logger will insert its output within the page content.

  62       * If the string <!--{xo-logger-output}--> is found in the page content, the logger output will

  63       * replace it, otherwise it will be inserted after all the page output.

  64       */
  65      function enableRendering() {
  66          if ( !$this->renderingEnabled ) {
  67              ob_start( array( &$this, 'render' ) );
  68              $this->renderingEnabled = true;
  69          }
  70      }
  71      /**

  72       * Returns the current microtime in seconds.

  73       * @return float

  74       */
  75  	function microtime() {
  76          $now = explode( ' ', microtime() );
  77          return (float)$now[0] + (float)$now[1];
  78      }
  79      /**

  80       * Start a timer

  81       * @param   string  $name   name of the timer

  82       */
  83      function startTime($name = 'XOOPS') {
  84          $this->logstart[$name] = $this->microtime();
  85      }
  86      /**

  87       * Stop a timer

  88       * @param   string  $name   name of the timer

  89       */
  90      function stopTime($name = 'XOOPS') {
  91          $this->logend[$name] = $this->microtime();
  92      }
  93      /**

  94       * Log a database query

  95       * @param   string  $sql    SQL string

  96       * @param   string  $error  error message (if any)

  97       * @param   int     $errno  error number (if any)

  98       */
  99      function addQuery($sql, $error=null, $errno=null) {
 100          if ( $this->activated )        $this->queries[] = array('sql' => $sql, 'error' => $error, 'errno' => $errno);
 101      }
 102      /**

 103       * Log display of a block

 104       * @param   string  $name       name of the block

 105       * @param   bool    $cached     was the block cached?

 106       * @param   int     $cachetime  cachetime of the block

 107       */
 108      function addBlock($name, $cached = false, $cachetime = 0) {
 109          if ( $this->activated )        $this->blocks[] = array('name' => $name, 'cached' => $cached, 'cachetime' => $cachetime);
 110      }
 111      /**

 112       * Log extra information

 113       * @param   string  $name       name for the entry

 114       * @param   int     $msg  text message for the entry

 115       */
 116      function addExtra($name, $msg) {
 117          if ( $this->activated )        $this->extra[] = array('name' => $name, 'msg' => $msg);
 118      }
 119  
 120      /**

 121       * Error handling callback (called by the zend engine)

 122       */  
 123      function handleError( $errno, $errstr, $errfile, $errline ) {
 124          $errstr = $this->sanitizePath( $errstr );
 125          $errfile = $this->sanitizePath( $errfile );
 126          if ( $this->activated && ( $errno & error_reporting() ) ) {
 127              // NOTE: we only store relative pathnames

 128              $this->errors[] = compact( 'errno', 'errstr', 'errfile', 'errline' );
 129          }
 130          if ( $errno == E_USER_ERROR ) {
 131              $trace = true;
 132              if ( substr( $errstr, 0, '8' ) == 'notrace:' ) {
 133                  $trace = false;
 134                  $errstr = substr( $errstr, 8 );
 135              }
 136              echo 'This page cannot be displayed due to an internal error.<br/><br/>';
 137              echo "You can provide the following information to the administrators of ";
 138              echo "this site to help them solve the problem:<br /><br />";
 139              echo "Error: $errstr<br />";
 140              if ( $trace && function_exists( 'debug_backtrace' ) ) {
 141                  echo "<div style='color:#ffffff;background-color:#ffffff'>Backtrace:<br />";
 142                  $trace = debug_backtrace();
 143                  array_shift( $trace );
 144                  foreach ( $trace as $step ) {
 145                      if ( isset( $step['file'] ) ) {
 146                          echo $this->sanitizePath( $step['file'] );
 147                          echo ' (' . $step['line'] . ")\n<br />";
 148                      }                    
 149                  }
 150                  echo '</div>';
 151              }
 152              exit();
 153          }
 154      }
 155      /**

 156       * @access protected

 157       */
 158  	function sanitizePath( $path ) {
 159          $path = str_replace( 
 160              array( '\\', XOOPS_ROOT_PATH, str_replace( '\\', '/', realpath( XOOPS_ROOT_PATH ) ) ),
 161              array( '/', '', '' ),
 162              $path
 163          );        
 164          return $path;
 165      }
 166      
 167      /**

 168       * Output buffering callback inserting logger dump in page output

 169       */
 170  	function render( $output ) {
 171          global $xoopsUser;
 172          if ( !$this->activated || !$xoopsUser || !$xoopsUser->isAdmin() ) {
 173              return $output;
 174          }
 175          $this->renderingEnabled = $this->activated = false;
 176          
 177          $log = $this->dump( $this->usePopup ? 'popup' : '' );
 178  
 179          $pattern = '<!--{xo-logger-output}-->';
 180          $pos = strpos( $output, $pattern );
 181          if ( $pos !== false ) {
 182              return substr( $output, 0, $pos ) . $log . substr( $output, $pos + strlen( $pattern ) );
 183          } else {
 184              return $output . $log;
 185          }
 186      }
 187      /**#@+

 188       * @protected

 189       */
 190  	function dump( $mode = '' ) {
 191          include XOOPS_ROOT_PATH . '/class/logger_render.php';
 192          return $ret;
 193      }
 194      /**

 195       * get the current execution time of a timer

 196       *

 197       * @param   string  $name   name of the counter

 198       * @return  float   current execution time of the counter

 199       */
 200      function dumpTime( $name = 'XOOPS' ) {
 201          if ( !isset($this->logstart[$name]) ) {
 202              return 0;
 203          }
 204          $stop = isset( $this->logend[$name] ) ? $this->logend[$name] : $this->microtime();
 205          return $stop - $this->logstart[$name];
 206      }
 207      /**#@-*/

 208      /**#@+

 209       * @deprecated

 210       */
 211      function dumpAll() {            return $this->dump( '' );            }
 212      function dumpBlocks() {            return $this->dump( 'blocks' );        }
 213      function dumpExtra() {            return $this->dump( 'extra' );        }
 214      function dumpQueries() {        return $this->dump( 'queries' );    }
 215      /**#@-*/

 216  }
 217  
 218  /*

 219  * PHP Error handler

 220  *

 221  * NB: You're not supposed to call this function directly, if you dont understand why, then

 222  * you'd better spend some time reading your PHP manual before you hurt somebody

 223  *

 224  * @internal: Using a function and not calling the handler method directly coz old PHP versions

 225  * set_error_handler() have problems with the array( obj,methodname ) syntax

 226  */
 227  function XoopsErrorHandler_HandleError( $errNo, $errStr, $errFile, $errLine, $errContext = null ) {
 228      $logger =& XoopsLogger::instance();
 229      $logger->handleError( $errNo, $errStr, $errFile, $errLine, $errContext );
 230  }
 231  
 232  ?>


Généré le : Sun Nov 25 11:44:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics