Onion Debug

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Onion Debug

Post by onion2k »

Very rough initial version of my latest toy .. a script profiler/debugging toy. It's only a couple of hours work so far, but it does something..

index.php:

Code: Select all

<?php

	include_once("odebug.php");
	
	$odebug->odebug_breakPoint("Start");

	for ($x=0;$x<10;$x++) {
		$odebug->odebug_breakPoint("Loop","xloop");
	}

	$odebug->odebug_breakPoint("End");

?>
odebug.php

Code: Select all

<?php

	$odebug =& new odebug;

	define("SCRIPTWD",getcwd());
	define("ODEBUG_DISPLAY_DEBUG_TRACE",1);
	define("ODEBUG_DISPLAY_DEBUG_SUMMARY",1);
	define("ODEBUG_DISPLAY_DEBUG_CHART",1);
	define("ODEBUG_DISPLAY_DEBUG_CLOSEREFS",1);
	define("ODEBUG_DISPLAY_DEBUG_GRAPH",1);
	define("ODEBUG_TICK_PRECISION",5);
	
	class odebug {
	
		var $odebug_arrBreakBuffer = array();
		var $odebug_startTime;
		var $odebug_endTime;
		
		function odebug() {
			$odebug_startTime = $this->odebug_microtime();
			register_shutdown_function( array( &$this, "odebug_shutdown") );
		}

		function odebug_breakPoint($comment="",$ref="") {

			$this->odebug_arrBreakBuffer[] = array(
				"comment"=>$comment,
				"ref"=>$ref,
				"tickTime"=>$this->odebug_microtime()
			);

		}

		function odebug_microtime() {
		   list($usec, $sec) = explode(" ", microtime());
		   return ((float)$usec + (float)$sec);
		}

		function odebug_shutdown() {

			$lastTickTime = 0;
			$odebug_trace = md5(uniqid(rand(),true));

			$fh = fopen(SCRIPTWD."/odebug_trace_".$odebug_trace.".txt","w");
			$s = serialize($this->odebug_arrBreakBuffer);
			fwrite($fh,$s);
			fclose($fh);

			if (ODEBUG_DISPLAY_DEBUG_TRACE==1) {

				echo "<div style=\"position:absolute; left:100%; top:0px; overflow:hidden; width:620px; margin:0 0 0 -620px;  z-index:1000000000; float:none; background-color: #FFFFFF; border: 0; padding: 0;\">";

				echo "<div style=\"border-width: 0 0 1px 1px; border-style: solid; border-color: #000000; padding: 5px;\">";
					echo "<div style=\"float: left; width: 400px; font-family: arial; font-size: 12px; padding-bottom: 5px;\"><strong>Onion PHP Debug</strong></div>";
					echo "<div style=\"float: left; width: 200px; font-family: arial; font-size: 12px; padding-bottom: 5px; text-align: right;\">";
					if (ODEBUG_DISPLAY_DEBUG_SUMMARY) { $pages[] = "<a href=\"#\" onClick=\"javascript: togglePage('tickSummary');\">Summary</a>"; }
					if (ODEBUG_DISPLAY_DEBUG_CHART) { $pages[] = "<a href=\"#\" onClick=\"javascript: togglePage('tickChart');\">Tick chart</a>"; }
					if (ODEBUG_DISPLAY_DEBUG_GRAPH) { $pages[] =  "<a href=\"#\" onClick=\"javascript: togglePage('tickGraph');\">Tick Graph</a>"; }
					echo is_array($pages) ? implode(" | ", $pages) : "&nbsp;";
					echo "</div>";
					echo "<div style=\"clear: both;\"></div>\n";
				echo "</div>";
					
					if (ODEBUG_DISPLAY_DEBUG_CHART) {
						echo "<div id=\"tickChart\" style=\"border-width: 0 0 1px 1px; border-style: solid; border-color: #000000; padding: 5px; display: block;\">";
							echo "<div style=\"float: left; width: 30px; font-family: arial; font-size: 12px;\"><strong>Tick</strong></div>";
							echo "<div style=\"float: left; width: 450px; font-family: arial; font-size: 12px;\"><strong>Comment</strong></div>";
							echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\"><strong>S <sub>last</sub></strong></div>";
							echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\"><strong>S <sub>start</sub></strong></div>";
							echo "<div style=\"clear: both;\"></div>\n";

							if (is_array($this->odebug_arrBreakBuffer)) {
								foreach ($this->odebug_arrBreakBuffer as $breakPoint) {
									$startTickTime = ($lastTickTime==0) ? $breakPoint['tickTime'] : $startTickTime;
									if ($breakPoint['ref']!=$lastRef and ODEBUG_DISPLAY_DEBUG_CLOSEREFS) {
										if ($lastRef!="") { echo "</div>"; }
										if ($breakPoint['ref']!="") {
											echo "<div style=\"float: left; width: 30px; font-family: arial; font-size: 12px; text-align: center;\">+</div>";
											echo "<div style=\"float: left; width: 450px; font-family: arial; font-size: 12px;\"><a href=\"#\" onClick=\"javascript: toggleBlock('".$breakPoint['ref']."');\">".$breakPoint['ref']."</a></div>";
											echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\">&nbsp;</div>";
											echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\">&nbsp;</div>";
											echo "<div style=\"clear: both;\"></div>\n";
											echo "<div id=\"".$breakPoint['ref']."\" style=\"display: none;\">";
										}
									}
									echo "<div style=\"float: left; width: 30px; font-family: arial; font-size: 12px; text-align: center;\">".++$bpCount."</div>";
									echo "<div style=\"float: left; width: 450px; font-family: arial; font-size: 12px;\">".$breakPoint['comment']."</div>";
									echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\">".number_format(($breakPoint['tickTime']-(($lastTickTime==0)?$startTickTime:$lastTickTime)),ODEBUG_TICK_PRECISION)."</div>";
									echo "<div style=\"float: left; width: 60px; font-family: arial; font-size: 12px;\">".number_format($breakPoint['tickTime']-$startTickTime,ODEBUG_TICK_PRECISION)."</div>";
									echo "<div style=\"clear: both;\"></div>\n";
									$lastRef = $breakPoint['ref'];
									$lastTickTime = $breakPoint['tickTime'];

								}
								if ($lastRef!="" and ODEBUG_DISPLAY_DEBUG_CLOSEREFS) { echo "</div>"; }
							}

						echo "</div>";
					}

					if (ODEBUG_DISPLAY_DEBUG_GRAPH) {
						echo "<div id=\"tickGraph\" style=\"border-width: 0 0 1px 1px; border-style: solid; border-color: #000000; padding: 5px; display: none; text-align: center;\">";
						echo "<img src=\"odebug_graph.php?odebug_trace=".$odebug_trace."\">";
						echo "</div>";
					}

				echo "</div>";

				echo "<script>";
				echo "function togglePage(pageRef) {\n";
				echo "	if (pageRef=='tickChart') {\n";
				echo "		document.getElementById('tickGraph').style.display='none';\n";
				echo "		document.getElementById('tickChart').style.display='block';\n";
				echo "	} else { \n";
				echo "		document.getElementById('tickChart').style.display='none';\n";
				echo "		document.getElementById('tickGraph').style.display='block';\n";
				echo "	}\n";
				echo "}\n";
				echo "function toggleBlock(blockRef) {\n";
				echo "var myBlock = document.getElementById(blockRef);";
				echo "	if (myBlock.style.display=='block') { myBlock.style.display='none'; } else { myBlock.style.display='block'; }\n";
				echo "}\n";
				echo "</script>";

			}

		}

	}

?>
odebug_graph.php:

Code: Select all

<?php

	//odebug graph
	
	$odebug_trace = $_GET['odebug_trace'];
	define("ODEBUG_TICK_PRECISION",5);
	
	$fh = file_get_contents("odebug_trace_".$odebug_trace.".txt");
	$odebug_arrBreakBuffer = unserialize($fh);
	
	$width = 600;
	$height = 300;
	
	$graph = imagecreate($width,$height);
	$white = imagecolorallocate($graph,255,255,255);
	$black = imagecolorallocate($graph,0,0,0);
	$lightgrey = imagecolorallocate($graph,192,192,192);
	$red = imagecolorallocate($graph,255,0,0);

	imageline($graph,($width*0.25),0,($width*0.25),$height,$lightgrey);
	imageline($graph,($width*0.5),0,($width*0.5),$height,$lightgrey);
	imageline($graph,($width*0.75),0,($width*0.75),$height,$lightgrey);
	imagerectangle($graph,0,0,$width-1,$height-1,$black);

	if (is_array($odebug_arrBreakBuffer)) {

		foreach ($odebug_arrBreakBuffer as $breakPoint) {

			$startTickTime = ($lastTickTime==0) ? $breakPoint['tickTime'] : $startTickTime;
			$lastTickTime = $breakPoint['tickTime'];

		}
		
		$execTime = number_format($lastTickTime-$startTickTime,ODEBUG_TICK_PRECISION)*10000;
	
		$ticks = count($odebug_arrBreakBuffer);
		$t_width = (($width-20)/$ticks);
		$t_height = 300/$execTime;

		foreach ($odebug_arrBreakBuffer as $breakPoint) {

			$startTickTime = ($lastTickTime==0) ? $breakPoint['tickTime'] : $startTickTime;
			$tickCount++;

			if ($breakPoint['ref']!=$lastRef) {
				if ($breakPoint['ref']!="") {
					
				}
			}

			//number_format(($breakPoint['tickTime']-(($lastTickTime==0)?$startTickTime:$lastTickTime)),ODEBUG_TICK_PRECISION)
			
			$tstart = number_format($breakPoint['tickTime']-$startTickTime,ODEBUG_TICK_PRECISION)*10000;
			imageline($graph,(($t_width*$tickCount)+10),300,(($t_width*$tickCount)+10),300-($t_height*$tstart),$red);

			$lastRef = $breakPoint['ref'];
			$lastTickTime = $breakPoint['tickTime'];

		}

	}

	header("Content-type: image/png");
	imagepng($graph);
	
	imagedestroy($graph);

	unlink("odebug_trace_".$odebug_trace.".txt");
	
?>
Stacks of work still to do .. needs to be a lot more flexible, and I'm intending to make it so you can register variables and get info about them .. and I really need to replace all of the HTML styling with CSS references. :)
Post Reply