Login   Register  
PHP Classes
elePHPant
Icontem

File: Debugger.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Stanislav Okhvat  >  PageNavigator  >  Debugger.php  >  Download  
File: Debugger.php
Role: ???
Content type: text/plain
Description: Helper class for debugging purposes
Class: PageNavigator
Author: By
Last change:
Date: 12 years ago
Size: 5,704 bytes
 

Contents

Class file image Download
<?php
/**
* Script debugging object with timing, loop-breaking and control message
* printing capabilities.
*
* @author   Stanislav Okhvat <stanis@ngs.ru>
* @version  $Id: Debugger.php,v 1.00 2002/03/11 17:15:00 stasokhvat Exp $
* @package  Debugger
* @access   public
*/
class Debugger
{
	var $sf_timer_markers = array(); // Snapshots of current time saved from timed scripts.
	
	var $_name = "Debugger";

    /**
    * Constructor. Outputs a debug message
    *
    * @param    string    message to print
	* @param    string    header to print before message, in bold (optional)
	* @param    boolean   if true, print as an HTML comment (optional)
    * @return   void
    * @access   public
    */
	function Debug($msg, $header = "", $comment = false)
	{
		print ($comment ? "<!-- " : "").
			  ($header ? "<b>$header</b>: " : "").$msg.
			  ($comment ? " -->\n" : "<br>\n");
	} // end func Debug

    /**
    * Prints all the information about the current state of the inherited object
    *
    * @return   void
    * @access   public
    */
	function DebugObject()
	{
		die($this->_name.'::DebugObject is being developed');
	}

    /**
    * Prints array structure
    *
    * @param    array    array whose structure needs to be printed
	* @return   void
    * @access   public
    */
	function DebugArray($a)
	{
		if(is_array($a)) {
			print '<table cellpadding=0 cellspacing=0 border=1 width=100%>';
			print '<tr><td colspan=3><b>Array {</b></td></tr>';
			
			$indent_str = "&nbsp;&nbsp;&nbsp;";

			while(list($one,$two) = each($a))
			{
				$depth = 0;
				printf ("\n<tr><td>${indent_str}${one}</td><td width=15 nowrap><b><font color=red>=&gt;</font></b></td><td> %s</td></tr>\n", $this->sprint_array($two, $depth));
			}
			print '<tr><td colspan=3><b>}</b></td></tr>';
			print '</table>';
		} 
		else
		{
			print $a;
		} 
    } // end func DebugArray

    /**
    * Starts the timer
    *
    * @param    string    benchmark marker name
	* @return   void
    * @access   public
    */
	function TimerStart($test="start")
	{
		$this->sf_timer_markers[$test."_start"] = 
							$this->sf_currtime();
	} // end func TimerStart

    /**
    * Stops the timer
    *
    * @param    string    benchmark marker name
	* @return   void
    * @access   public
    */
	function TimerStop($test="stop")
	{
		$this->sf_timer_markers[$test."_stop"] = 
							$this->sf_currtime();
	} // end func TimerStop

    /**
    * Profiles the execution from one marker to another
    *
    * @param    string    start benchmark marker
    * @param    string    stop benchmark marker
	* @return   void
    * @access   public
    */
	function TimerPrint($start="start",$end="stop")
	{
		if (!isset($this->sf_timer_markers[$end."_stop"]))
		{
			$this->Debug("You forgot to stop the timer", $this->_name . "::TimerProfile");
			return;
		}
		elseif (!isset($this->sf_timer_markers[$start."_start"]))
		{
			$this->Debug("You forgot to start the timer", $this->_name . "::TimerProfile");
			return;
		}

		if (function_exists('bcsub')) {
			$diff = bcsub($this->sf_timer_markers[$end."_stop"], 
						$this->sf_timer_markers[$start."_start"],6);
		} else {
			$diff = $this->sf_timer_markers[$end."_stop"] - $this->sf_timer_markers[$start."_start"];
		}
		$this->Debug("Execution time from marker '$start' to marker '$end' - <u>$diff</u>", $this->_name . "::TimerPrint");
		
		return $diff;
	} // end func TimerPrint

    /**
    * Allows to exit the loop if it has run longer than the time
    * (in seconds) specified by the developer.
    *
    * @param    integer  maximum run time allowed for loop in milliseconds
    * @param    string   loop id
    * @param    string   name of the calling function (needed for printing debug string only)
	* @return   void
    * @access   public
    */
	function BreakLoop($max_run_time, $loop_name, $caller_func)
	{
		static $start;
		static $loop_id;

		if (!$loop_id || $loop_id != $loop_name)
		{
			$start = $this->sf_currtime();
			$loop_id = $loop_name;
		}
		else
		{
			if (($this->sf_currtime() - $start) > $max_run_time)
			{
				$this->Debug("Loop $loop_name has exceeded the maximum allowed time limit of $max_run_time seconds!", $this->_name."::BreakLoop from $caller_func");
				exit;
			}
		}
		return;		
	} // end func BreakLoop

// PRIVATE

    /**
    * Prints array structure for certain depth
    *
    * @param    array    array whose structure needs to be printed
    * @param    integer  depth of the current array relative to the entry point of DebugArray
	* @return   string   formatted array structure
    * @access   private
    */
    function sprint_array($a, $depth)
	{
		if(is_array($a)) {
			
			$indent_str = "&nbsp;&nbsp;&nbsp;";

			$out = '<table cellpadding=0 cellspacing=0 border=1 width=100%>';
			$out .= '<tr valign=top><td colspan=3><b>Array {</b></td></tr>';

			while(list($one,$two)=each($a))
			{
				$depth += 2;
				$out .= sprintf("\n<tr><td>${indent_str}${one}</td><td width=15 nowrap><b><font color=red>=&gt;</font></b></td><td> %s</td></tr>\n", $this->sprint_array($two, $depth));
				$depth -= 2;
			}
			
			$out .= '<tr><td colspan=3><b>}</b></td></tr>';
			$out .= '</table>';
			return $out;
		}
		else
		{
			return $a;
		}
	}

    /**
    * Get current microtime
    *
	* @return   string   formatted microtime
    * @access   private
    */
	function sf_currtime()
	{
		$microtime = explode(" ", microtime());
		return $microtime[1] . substr($microtime[0],1);
	} // end func

} // end class Debugger
?>