Source for file Debugtoolbar.php

Documentation is available at Debugtoolbar.php

  1. <?php
  2. /** Ethna_Plugin_Logwriter_DebugToolbar
  3.  *
  4.  * @author  Sotaro KARASAWA <sotaro.k@gmail.com>
  5.  * @date    2008/12/01
  6.  * @package    Ethna
  7.  */
  8.  
  9.  
  10. {
  11.     var $log_array = array();
  12.  
  13.     /**
  14.      *  ログを出力する
  15.      *
  16.      *  @access public
  17.      *  @param  int     $level      ログレベル(LOG_DEBUG, LOG_NOTICE...)
  18.      *  @param  string  $message    ログメッセージ(+引数)
  19.      */
  20.     function log($level$message)
  21.     {
  22.         $c Ethna_Controller::getInstance();
  23.  
  24.         $prefix $this->ident;
  25.         if (array_key_exists("pid"$this->option)) {
  26.             $prefix .= sprintf('[%d]'getmypid());
  27.         }
  28.         $pre_prefix '<div class="ethna-debug-log ethna-debug-log-' $this->_getLogLevelName($level'">';
  29.         $prefix .= sprintf($c->getGateway(!= GATEWAY_WWW '(%s): ' '(<span class="ethna-debug-log-loglevel ethna-debug-log-loglevel-' $this->_getLogLevelName($level' ">%s</span>): ',
  30.             $this->_getLogLevelName($level)
  31.         );
  32.         $post_prefix '</div>';
  33.  
  34.         $tracer '';
  35.         if ($this->_getLogLevelName($level!= 'DEBUG'
  36.             && preg_match('/in (template ")?(\/.+\.(php|tpl))"?\s+on line (\d+)/'$message$match)) {
  37.             list(, , $file, ,$line$match;
  38.             $line intval($line);
  39.             if (file_exists($file)) {
  40.                 $tracer .= ($c->getGateway(!= GATEWAY_WWW "" '<pre class="ethna-debug-pre">');
  41.                 $f new SplFileObject($file);
  42.                 $min ($line 0$line 4;
  43.                 $i $min;
  44.                 foreach (new LimitIterator($f$min7as $line_str{
  45.                     $l = ++$i;
  46.                     if ($l == $line{
  47.                         $tracer .= '<span class="ethna-debug-pre-blink">';
  48.                     }
  49.                     $tracer .= $l ': ' htmlspecialchars($line_str($c->getGateway(!= GATEWAY_WWW "" '<br />');
  50.                     if ($l == $line{
  51.                         $tracer .= '</span>';
  52.                     }
  53.                 }
  54.                 $tracer .= ($c->getGateway(!= GATEWAY_WWW "" '</pre>');
  55.             }
  56.         }
  57.  
  58.         if (array_key_exists("function"$this->option||
  59.             array_key_exists("pos"$this->option)) {
  60.             $tmp "";
  61.             $bt $this->_getBacktrace();
  62.             if ($bt && array_key_exists("function"$this->option&& $bt['function']{
  63.                 $tmp .= $bt['function'];
  64.             }
  65.             if ($bt && array_key_exists("pos"$this->option&& $bt['pos']{
  66.                 $tmp .= $tmp sprintf('(%s)'$bt['pos']$bt['pos'];
  67.             }
  68.             if ($tmp{
  69.                 $prefix .= $tmp ": ";
  70.             }
  71.         }
  72.  
  73.         $br $c->getGateway(!= GATEWAY_WWW "" "<br />";
  74.  
  75.         $log_content ($pre_prefix $prefix $message $tracer $post_prefix "\n");
  76.         $this->log_array[$log_content;
  77.  
  78.         return $log_content;
  79.     }
  80.  
  81.     function end()
  82.     {
  83.         $ctl Ethna_Controller::getInstance();
  84.         if ((!is_null($view $ctl->getView()) && !$view->has_default_header)
  85.             || $ctl->getGateway(!= GATEWAY_WWW{
  86.             $this->log_array = array();
  87.             return null;
  88.         }
  89.  
  90.         echo '<div class="ethna-debug" id="ethna-debug-logwindow">';
  91.         echo '<div class="ethna-debug-title">Log</div>';
  92.         foreach ($this->log_array as $log{
  93.             echo $log;
  94.         }
  95.         echo '</div>';
  96.  
  97.         $this->log_array = array();
  98.     }
  99.  
  100.     public function __destruct()
  101.     {
  102.         if (!empty($this->log_array)) {
  103.             $ctl Ethna_Controller::getInstance();
  104.             if ((!is_null($view $ctl->getView()) && !$view->has_default_header)
  105.                 || $ctl->getGateway(!= GATEWAY_WWW{
  106.                 $this->log_array = array();
  107.                 return null;
  108.             }
  109.  
  110.             echo "<h1>Script shutdown unexpectedly</h1>";
  111.             if (is_array($this->log_array)) foreach ($this->log_array as $log{
  112.                 echo $log;
  113.             }
  114.         }
  115.     }
  116.  
  117.     /**
  118.      *  ログ出力箇所の情報(関数名/ファイル名等)を取得する
  119.      *
  120.      *  @access private
  121.      *  @return array   ログ出力箇所の情報
  122.      */
  123.     function _getBacktrace()
  124.     {
  125.         $skip_method_list array(
  126.             array('ethna''raise'),
  127.             array(null'raiseerror'),
  128.             array(null'handleerror'),
  129.             array('ethna_logger'null),
  130.             array('ethna_plugin_logwriter'null),
  131.             array('ethna_error'null),
  132.             array('ethna_apperror'null),
  133.             array('ethna_actionerror'null),
  134.             array('ethna_backend''log'),
  135.             array(null'ethna_error_handler'),
  136.             array(null'trigger_error'),
  137.             array('ether_plugin_logwriter'null)// この1行を足すためにメソッドまるごとコピーですよ・・・
  138.         );
  139.  
  140.         if ($this->have_backtrace == false{
  141.             return null;
  142.         }
  143.  
  144.         $bt debug_backtrace();
  145.         $i 0;
  146.         while ($i count($bt)) {
  147.             if (isset($bt[$i]['class']== false{
  148.                 $bt[$i]['class'null;
  149.             }
  150.             $skip false;
  151.  
  152.             // メソッドスキップ処理
  153.             foreach ($skip_method_list as $method{
  154.                 $class $function true;
  155.                 if ($method[0!= null{
  156.                     $class preg_match("/^$method[0]/i"$bt[$i]['class']);
  157.                 }
  158.                 if ($method[1!= null{
  159.                     $function preg_match("/^$method[1]/i"$bt[$i]['function']);
  160.                 }
  161.                 if ($class && $function{
  162.                     $skip true;
  163.                     break;
  164.                 }
  165.             }
  166.  
  167.             if ($skip{
  168.                 $i++;
  169.             else {
  170.                 break;
  171.             }
  172.         }
  173.  
  174.  
  175.         $c Ethna_Controller::getInstance();
  176.         $basedir $c->getBasedir();
  177.  
  178.         $function sprintf("%s.%s"isset($bt[$i]['class']$bt[$i]['class''global'$bt[$i]['function']);
  179.  
  180.         if (!isset($bt[$i])) {
  181.             return null;
  182.         }
  183.         else {
  184.             $file $bt[$i]['file'];
  185.         }
  186.         $orig_file $file;
  187.         if (strncmp($file$basedirstrlen($basedir)) == 0{
  188.             $file substr($filestrlen($basedir));
  189.         }
  190.         if (strncmp($fileETHNA_BASEstrlen(ETHNA_BASE)) == 0{
  191.             $file preg_replace('#^/+#'''substr($filestrlen(ETHNA_BASE)));
  192.         }
  193.         $line $bt[$i]['line'];
  194.         return array('function' => $function'pos' => sprintf('%s:%s'$file$line)'file' => $orig_file);
  195.     }
  196. }

Documentation generated on Fri, 11 Nov 2011 03:57:58 +0900 by phpDocumentor 1.4.3