Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //----------------------------------------------
- // Debugger
- //----------------------------------------------
- class Debugger implements IDebug{
- private $_prepared = false;
- private $_debugFile ='';
- private $_buffer ='';
- private $_debugMode = 0;
- //---------------------------------
- private function isDebugFile() {
- return (bool)$this->_debugFile;
- }
- //---------------------------------
- private function prepared() {
- if ($this->_prepared)
- return true;
- if (isset($_SESSION['debug_prepared'])) {
- if (isset($_SESSION['debug_file']))
- $this->_debugFile = $_SESSION['debug_file'];
- $this->_prepared = true;
- return true;
- }
- return false;
- }
- //-------------------------------------------------save session variable
- private function save_to_session($name,$value) {
- if (function_exists('session_status')) {
- if (session_status()==PHP_SESSION_ACTIVE)
- $_SESSION[$name] = $value;
- }
- else
- if (session_id()!=='')
- $_SESSION[$name] = $value;
- }
- //--------------------------------- prepare file
- private function prepare_file() {
- $DS = DIRECTORY_SEPARATOR;
- $ROOT = dirname(__FILE__);
- if (!file_exists($ROOT.$DS.'logs'))
- mkdir ($ROOT.$DS.'logs');
- $this->_debugFile = $ROOT.$DS.'logs'.$DS.'debug_'.date('d_m_Y_H_i_s').rand(100,500).'.txt';
- $this->save_to_session('debug_file', $this->_debugFile);
- }
- //------------------------------------ output buffer
- private function outputBuffer() {
- if ($this->isDebugFile()) {
- $link = substr($this->_debugFile, strpos($this->_debugFile, 'logs'.DS));
- echo 'debug information in file: <a href="'.$link.'">'.$link.'</a>';
- }
- else
- echo '<h2>debug information</h2><div style="font-family:courier new">'.$this->_buffer.'</div>';
- }
- //--------------------------------- prepare debugger
- private function prepare() {
- if ($this->_debugMode==0)
- return;
- if ($this->prepared())
- return;
- error_reporting(E_ALL);
- ini_set('display_errors','On');
- set_error_handler (array($this,'errorHandler'));
- set_exception_handler (array($this,'exceptionHandler'));
- register_shutdown_function (array($this,'shutdownHandler'));
- if ($this->_debugMode>1)
- $this->prepare_file();
- $this->_buffer = '';
- $this->_prepared = true;
- $this->save_to_session('debug_prepared', $this->_prepared);
- }
- //--------------------------------- reset debugger
- public function reset() {
- if (isset($_SESSION['debug_prepared']))
- unset ($_SESSION['debug_prepared']);
- if (isset($_SESSION['debug_file']))
- unset ($_SESSION['debug_file']);
- $this->_debugFile = '';
- $this->_prepared = false;
- }
- //------------------------------------ error handler
- public function errorHandler($number, $message, $file, $line) {
- echo '<h2>error occured</h2>';
- echo '<p>'.str_pad('error number: ',30,' ',STR_PAD_RIGHT).$number.'</p>';
- echo '<p>'.str_pad('error message: ',30,' ',STR_PAD_RIGHT).$message.'</p>';
- echo '<p>'.str_pad('error in file: ',30,' ',STR_PAD_RIGHT).$file.'</p>';
- echo '<p>'.str_pad('error in line: ',30,' ',STR_PAD_RIGHT).$line.'</p>';
- $this->outputBuffer();
- }
- //----------------------------------- exception handler
- public function exceptionHandler($exception) {
- echo '<h2>exception occured</h2>';
- echo '<p>'.str_pad('exception code: ',30,' ',STR_PAD_RIGHT).$exception->getCode().'</p>';
- echo '<p>'.str_pad('exception message: ',30).$exception->getMessage().'</p>';
- echo '<p>'.str_pad('exception in file: ',30).$exception->getFile().'</p>';
- echo '<p>'.str_pad('exception in line: ',30).$exception->getLine().'</p>';
- //echo '<h3>exception trace</h3>';
- //echo '<pre>'.print_r($exception->getTrace(),true).'</pre>';
- $this->outputBuffer();
- }
- //----------------------------------- shutdown handler
- public function shutdownHandler() {
- $error = error_get_last();
- echo '<h2>script finished</h2>';
- echo 'error: <pre>'.print_r($error,true).'</pre>';
- $this->outputBuffer();
- }
- //--------------------------------- handle & write info
- public function writeInfo($info, $className, $method) {
- if ($this->_debugMode==0)
- return;
- $this->prepare();
- $s = str_repeat('-',50)."\r\n";
- $s .= date('d.m.Y H:i:s').' class: '.$className.' method: '.$method. "\r\n";
- $s .= str_repeat('-',50)."\r\n";
- $inf = array();
- if (!is_array($info)) {
- $inf[] = $info;
- } else
- $inf = $info;
- $s .= '<pre>'.print_r($inf, true).'</pre>';
- if ($this->isDebugFile()) {
- if (!file_exists($this->_debugFile))
- $f = fopen ($this->_debugFile,'w');
- else
- $f = fopen ($this->_debugFile,'a');
- fwrite($f, $s);
- fclose($f);
- }
- $this->_buffer .= nl2br($s);
- }
- //--------------------------------- Switch Debug On / Off / File
- public function debugMode($mode) {
- $this->_debugMode = $mode;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement