Jump to content

Dubugger Class You might find this useful :p


sequalit

Recommended Posts

 

Ever look at your code and you have a million echo statements throughout

your code, and some are commented out others are not, and you used these

echo's to help you debug your application during development? I wrote a simple

class to help with debugging! With this class you will never have to uncomment

your echo statements again! Just tell the debugger class to not display

those messages! Its awesome!

 

I'm sure this could use some work to be made even cooler

 

Please give any idea's or suggestions you have so that I can make this cooler

 

If you add something to it, please let me know in this post, so I can merge it

into the official class :)

 

 

Where would be the best place to store this?

 

I choose $_SESSION because thats the only place i know of to put it!

 

Debugger Source, version 1


/**
* Copyright (c) 2008 Thadeus N. Burgess (thadeus.burgess(at)gmail.com)
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*

define("ECHOS", true);
define("ECHO_SEPERATOR", "~|~");
define("debugger", "Debugger");
/**
* Class to handle echo messages used for debugging during development process
*/
class Debugger{
private $echos;
private $echoPages;
/**
 * Constructor.
 * If ECHOS true then we set up the arrays
 */
function Debugger(){
	if(ECHOS){       //if echos is off, we do nothing, to save memory
		$this->echos = array();
		$this->echoPages = array();
	}
}
/**
 * Mark page to be echo'ed
 * 
 * @param string $page Name of message to be echo'ed EX. 'index.php', 'mysql.class', 'foo.function'
 * 
 * @return void
 */
function setDoEcho($page){
	if(ECHOS)        //if echos is off, we do nothing, to save memory
		array_push($this->echoPages, $page);
}
/**
 * Register message to be echo'ed
 * 
 * @param string $page Name of message to be echo'ed EX. 'index.php', 'mysql.class', 'foo.function'
 * @param string $message Message that will be echo'ed
 * 
 * @return void
 */
function registerEcho($page, $message){
	if(ECHOS)        //if echos is off, we do nothing, to save memory
		array_push($this->echos, $page.ECHO_SEPERATOR.$message);
}
/**
 * Echo all messages that have been set to echo
 */
function echoAll(){
	if(ECHOS)//if echos is off, we do nothing, to save memory
		for($i=0;$i<count($this->echos);$i++){
			$message = explode(ECHO_SEPERATOR, $this->echos[$i]);
			if(in_array($message[0], $this->echoPages))// if we have set it to be echo'ed, echo it
				echo $message[1]."<br>";

		}
	echo "<br><br><br>";
}
}

 

HOW TO USE:

index.php

require_once('debugger.php');
$_SESSION[debugger] = new Debugger(); // we set it in session so that everything can access it
if(ECHOS){
	//All you have to do now is comment these out to not display
	//a module!!! How awesome!!

	//examples
	$_SESSION[debugger]->setDoEcho('database.php');
	$_SESSION[debugger]->setDoEcho('doSomething.function');
	$_SESSION[debugger]->setDoEcho('a_thing.class');
	$_SESSION[debugger]->setDoEcho('mysql.class');
	$_SESSION[debugger]->setDoEcho('hello.world');

	$_SESSION[debugger]->echoAll();// echo all messages in order

}

 

Then anywhere you want an echo do this

 

$message = "Hello World";
$_SESSSION[debugger]->registerEcho('hello.world', $message);

 

Turn This

 

function query($sql){
		if(!$this->connected)
			$this->connect();

		$this->query = $sql;
		//echo "<br>QUERYING";	
		//echo "<br>". $this->query;
		$this->res = mysql_query($sql, $this->con);
		//echo "<br>RESULTS: ".$this->res."<br>";
		//return new results($this->res);
	}

 

Into This:

 

function query($sql){
	if(!$this->connected)
		$this->connect();

	$this->query = $sql;

	$this->res = mysql_query($sql, $this->con);
	if(!$this->res) $this->queryError();

	$_SESSION[debugger]->registerEcho('mysql.class', "QUERYING: ".$this->query);
	$_SESSION[debugger]->registerEcho('mysql.class', "RESULTS: ".$this->res);
}

Link to comment
Share on other sites

Won't let me edit post for some odd reason?

 

Anways, a couple of changes

 

Change all instancse of $_SESSION to $GLOBALS - better place to store it.

 

And

 

in the echoAll() class, change this line

 

echo $message[1]."<br>";

 

to this

 

echo "Message $i: ".$message[1]."<br>";

Link to comment
Share on other sites

Or you could:

<?php
//debug.inc.php
$DEBUG = $_REQUEST['DEBUG'];
if$($DEBUG == ""){

     $DEBUG = 0; 

}else{

     $DEBUG = 1;

}
?>

<?php
//index.php
include("debug.inc.php");

if($DEBUG)echo "[This is will display if index.php?DEBUG=1] <br>";
?>

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.