Page 1 of 1

Debugger Class - Might find its useful! :P

Posted: Tue Apr 01, 2008 9:01 pm
by Sequalit
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

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);
    }
   

Re: Debugger Class - Might find its useful! :P

Posted: Tue Apr 01, 2008 9:03 pm
by John Cartwright
$_SESSION is certainly not the correct place for it. try $GLOBALS instead

Re: Debugger Class - Might find its useful! :P

Posted: Tue Apr 01, 2008 9:26 pm
by Sequalit
K thanx.

For some reason I was under the impression that $GLOBALS was insecure and can be compromised...?

Re: Debugger Class - Might find its useful! :P

Posted: Tue Apr 01, 2008 10:06 pm
by Christopher
You might want to make this a have an internal static so you just do Debugger::setDoEcho('database.php');. This is really not a "debugger" class -- it is a Logger. You should search for logging classes. There are many good examples around.

Re: Debugger Class - Might find its useful! :P

Posted: Wed Apr 02, 2008 5:09 am
by Mordred
Sequalit wrote: Turn this
(snip)
into this
(snip)
It's a joke, right? You can't sell anything with that many characters in it as "cool", unless we're talking T-shirts.

You shoud aim at about 5 characters more per line compared to echo, counting every time you have to press Shift as two.

Re: Debugger Class - Might find its useful! :P

Posted: Wed Apr 02, 2008 10:38 pm
by Sequalit
arborint wrote:You might want to make this a have an internal static so you just do Debugger::setDoEcho('database.php');. This is really not a "debugger" class -- it is a Logger. You should search for logging classes. There are many good examples around.
I will be implementing this, thank you... I didn't know there were other logging classes out there (but then again I didn't ever think to search)

Having an internal static will make it so I do not have to keep referencing by $GLOBALS?
Mordred wrote:
Sequalit wrote: Turn this
(snip)
into this
(snip)
It's a joke, right? You can't sell anything with that many characters in it as "cool", unless we're talking T-shirts.

You shoud aim at about 5 characters more per line compared to echo, counting every time you have to press Shift as two.
Feel powerful now?

I do need a way to make it easier to type out, instead of having to type $GLOBALS all the time have a suggestion for me ?

Re: Debugger Class - Might find its useful! :P

Posted: Thu Apr 03, 2008 1:54 am
by Mordred
Sequalit wrote:Feel powerful now?
Always do. :twisted: And even when I jest, I'm true to my word, you do use too much smurfing characters.
Sequalit wrote:I do need a way to make it easier to type out, instead of having to type $GLOBALS all the time have a suggestion for me ?
Yes, but since we have to deal with the severe limitations of PHP in the globals area it ain't be as pretty as I would like.

Code: Select all

myecho("Debuggity debug, the value is $foo");
You'll notice that this adds only four characters to "echo" - "my" and the two parentheses. We're not counting double penalty for them, as you have to hold shift for the quotes anyway.

Instead of defining debug categories by hardcoding them (and having to type them out every time in the echo call), use the callstack. Use debug_backtrace() to walk the call stack, and choose the debug category based on the functions, classes and include files found there.

Re: Debugger Class - Might find its useful! :P

Posted: Thu Apr 03, 2008 2:18 am
by onion2k
I wrote one of these things for my sites. Stuff I learnt while writing it:

1. Echoing things inline is actually quite annoying. They can break HTML. I prefer to echo all the debug stuff at the end of the script. I did that by having a function in the debug code that creates an absolutely positioned div that sites over the site in the corner of the browser.

2. It's useful to attach a time to everything. When you're debugging a script that's slow adding some calls that output a time is handy to figure out where the script is slowing down. If all your debug calls have a timestamp anyway that's really simple. Plus, if you log the time the script was started and the time the error handler is called to output the errors you can calculate the difference to get the total time the script took to run. That's really useful.

3. $GLOBALS isn't really the right place to store debug data either. In mine I create a debug object at the start of the script and then pass it (by reference) to every other object and function in the code. It limits debugging things like SwiftMailer and ADODB Lite, but that's not been a problem so far.

4. Error levels are very useful. If you use something like "$errorHandler->addDebug("User 'womble' logged in.", NOTICE);" and "$errorHandler->addDebug("Uh oh, an error", CRITICAL);" you can then configure the error handler to only output certain things (just like PHP's error handling).

5. To log what's happening on a live website you don't want to be outputting errors in the HTML so a function that can log to a database or a file is helpful. Personally I tied that to the error levels code eg only log to the file if there's a CRITICAL error.