Debugger Class - Might find its useful! :P
Posted: Tue Apr 01, 2008 9:01 pm
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
Changelog:
Made it echo Message $i: before each message
Debugger Source, version 1
HOW TO USE:
index.php
Then anywhere you want an echo do this
Turn This
Into This:
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
Changelog:
Made it echo Message $i: before each message
Debugger Source, version 1
Code: Select all
/**
* 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 $i: ".$message[1]."<br>";
}
echo "<br><br><br>";
}
}
HOW TO USE:
index.php
Code: Select all
require_once('debugger.php');
$GLOBALS[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
$GLOBALS[debugger]->setDoEcho('database.php');
$GLOBALS[debugger]->setDoEcho('doSomething.function');
$GLOBALS[debugger]->setDoEcho('a_thing.class');
$GLOBALS[debugger]->setDoEcho('mysql.class');
$GLOBALS[debugger]->setDoEcho('hello.world');
$GLOBALS[debugger]->echoAll();// echo all messages in order
}
Then anywhere you want an echo do this
Code: Select all
$message = "Hello World";
$GLOBALS[debugger]->registerEcho('hello.world', $message);
Turn This
Code: Select all
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:
Code: Select all
function query($sql){
if(!$this->connected)
$this->connect();
$this->query = $sql;
$this->res = mysql_query($sql, $this->con);
if(!$this->res) $this->queryError();
$GLOBALS[debugger]->registerEcho('mysql.class', "QUERYING: ".$this->query);
$GLOBALS[debugger]->registerEcho('mysql.class', "RESULTS: ".$this->res);
}