Advertisement
terorama

WP / inc / utils / debug.inc.php

May 29th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.35 KB | None | 0 0
  1. <?php
  2.  
  3. //----------------------------------------------
  4. //               Debugger
  5. //----------------------------------------------
  6. class Debugger implements IDebug{
  7.  
  8.  
  9.    private $_prepared = false;
  10.    private $_debugFile ='';
  11.    private $_buffer ='';
  12.    private $_debugMode = 0;
  13.    
  14.    //---------------------------------
  15.    private function isDebugFile() {
  16.    
  17.       return (bool)$this->_debugFile;
  18.    }
  19.    //---------------------------------
  20.    private function prepared() {
  21.    
  22.       if ($this->_prepared)
  23.          return true;
  24.          
  25.        if (isset($_SESSION['debug_prepared'])) {
  26.        
  27.           if (isset($_SESSION['debug_file']))            
  28.              $this->_debugFile = $_SESSION['debug_file'];
  29.              
  30.           $this->_prepared = true;
  31.           return true;
  32.        }
  33.        
  34.        return false;
  35.    }
  36.    //-------------------------------------------------save session variable  
  37.    private function save_to_session($name,$value) {
  38.    
  39.        if (function_exists('session_status')) {
  40.          if (session_status()==PHP_SESSION_ACTIVE)
  41.             $_SESSION[$name] = $value;
  42.             }
  43.        else
  44.           if (session_id()!=='')
  45.              $_SESSION[$name] = $value;
  46.    }
  47.    
  48.    //--------------------------------- prepare file
  49.    private function prepare_file() {
  50.    
  51.       $DS = DIRECTORY_SEPARATOR;
  52.       $ROOT = dirname(__FILE__);
  53.  
  54.       if (!file_exists($ROOT.$DS.'logs'))
  55.          mkdir ($ROOT.$DS.'logs');
  56.          
  57.       $this->_debugFile = $ROOT.$DS.'logs'.$DS.'debug_'.date('d_m_Y_H_i_s').rand(100,500).'.txt';
  58.       $this->save_to_session('debug_file', $this->_debugFile);
  59.      
  60.    }
  61.    
  62.    //------------------------------------ output buffer
  63.    private function outputBuffer() {
  64.      
  65.       if ($this->isDebugFile()) {
  66.      
  67.          $link = substr($this->_debugFile, strpos($this->_debugFile, 'logs'.DS));        
  68.          echo 'debug information in file: <a href="'.$link.'">'.$link.'</a>';
  69.       }
  70.       else
  71.          echo '<h2>debug information</h2><div style="font-family:courier new">'.$this->_buffer.'</div>';
  72.    }
  73.    
  74.    //--------------------------------- prepare debugger
  75.    private function prepare() {
  76.      
  77.       if ($this->_debugMode==0)
  78.          return;
  79.      
  80.       if ($this->prepared())
  81.          return;
  82.    
  83.       error_reporting(E_ALL);
  84.       ini_set('display_errors','On');
  85.    
  86.       set_error_handler (array($this,'errorHandler'));
  87.       set_exception_handler (array($this,'exceptionHandler'));
  88.       register_shutdown_function (array($this,'shutdownHandler'));
  89.      
  90.       if ($this->_debugMode>1)
  91.          $this->prepare_file();
  92.        
  93.       $this->_buffer = '';
  94.       $this->_prepared = true;
  95.       $this->save_to_session('debug_prepared', $this->_prepared);    
  96.    }
  97.    
  98.    //--------------------------------- reset debugger
  99.    public function reset() {
  100.      
  101.       if (isset($_SESSION['debug_prepared']))
  102.          unset ($_SESSION['debug_prepared']);
  103.    
  104.       if (isset($_SESSION['debug_file']))
  105.          unset ($_SESSION['debug_file']);
  106.    
  107.       $this->_debugFile = '';  
  108.       $this->_prepared = false;
  109.    }
  110.    //------------------------------------ error handler
  111.    public function errorHandler($number, $message, $file, $line) {
  112.      
  113.       echo '<h2>error occured</h2>';
  114.       echo '<p>'.str_pad('error number: ',30,' ',STR_PAD_RIGHT).$number.'</p>';
  115.       echo '<p>'.str_pad('error message: ',30,' ',STR_PAD_RIGHT).$message.'</p>';
  116.       echo '<p>'.str_pad('error in file: ',30,' ',STR_PAD_RIGHT).$file.'</p>';
  117.       echo '<p>'.str_pad('error in line: ',30,' ',STR_PAD_RIGHT).$line.'</p>';
  118.      
  119.       $this->outputBuffer();
  120.    }
  121.    
  122.    //----------------------------------- exception handler
  123.    
  124.    public function exceptionHandler($exception) {
  125.      
  126.       echo '<h2>exception occured</h2>';
  127.       echo '<p>'.str_pad('exception code: ',30,' ',STR_PAD_RIGHT).$exception->getCode().'</p>';
  128.       echo '<p>'.str_pad('exception message: ',30).$exception->getMessage().'</p>';
  129.       echo '<p>'.str_pad('exception in file: ',30).$exception->getFile().'</p>';
  130.       echo '<p>'.str_pad('exception in line: ',30).$exception->getLine().'</p>';
  131.      
  132.       //echo '<h3>exception trace</h3>';     
  133.       //echo '<pre>'.print_r($exception->getTrace(),true).'</pre>';
  134.      
  135.       $this->outputBuffer();
  136.    }
  137.    
  138.    //----------------------------------- shutdown handler
  139.    
  140.    public function shutdownHandler() {
  141.    
  142.       $error = error_get_last();
  143.      
  144.       echo '<h2>script finished</h2>';
  145.       echo 'error: <pre>'.print_r($error,true).'</pre>';
  146.      
  147.       $this->outputBuffer();
  148.    }
  149.    
  150.    //--------------------------------- handle & write info
  151.    
  152.    public function writeInfo($info, $className, $method) {
  153.    
  154.       if ($this->_debugMode==0)
  155.          return;
  156.                
  157.       $this->prepare();
  158.      
  159.          
  160.       $s = str_repeat('-',50)."\r\n";    
  161.       $s  .= date('d.m.Y H:i:s').'  class: '.$className.' method: '.$method. "\r\n";
  162.       $s  .= str_repeat('-',50)."\r\n";
  163.      
  164.       $inf = array();
  165.       if (!is_array($info)) {
  166.          $inf[] = $info;
  167.       } else
  168.          $inf = $info;
  169.    
  170.       $s .= '<pre>'.print_r($inf, true).'</pre>';
  171.      
  172.       if ($this->isDebugFile()) {
  173.          if (!file_exists($this->_debugFile))
  174.          
  175.             $f = fopen ($this->_debugFile,'w');
  176.          else
  177.             $f = fopen ($this->_debugFile,'a');
  178.  
  179.          fwrite($f, $s);
  180.          fclose($f);         
  181.       }
  182.          
  183.       $this->_buffer .= nl2br($s);
  184.    }
  185.    
  186.    //--------------------------------- Switch Debug On / Off / File
  187.    public function debugMode($mode) {
  188.       $this->_debugMode = $mode;
  189.    }
  190. }
  191.  
  192.  
  193. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement