Location: PHPKode > scripts > Simple Debug > simple-debug/Debug.php
<?php
// $Id: Debug.php,v 1.2 2003/01/22 12:25:30 k1m Exp $
// +----------------------------------------------------------------------+
// | Debug 0.1                                                            |
// +----------------------------------------------------------------------+
// | Author: Keyvan Minoukadeh - hide@address.com - http://www.keyvan.net   |
// +----------------------------------------------------------------------+
// | Very simple (at the moment) class to use for debugging               |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or        |
// | modify it under the terms of the GNU General Public License          |
// | as published by the Free Software Foundation; either version 2       |
// | of the License, or (at your option) any later version.               |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// +----------------------------------------------------------------------+

define('DEBUG_OUTPUT_OFF', 0);
define('DEBUG_OUTPUT_ON', 1);
define('DEBUG_OUTPUT_FILE', 2); // full file path
define('DEBUG_OUTPUT_FILENAME', 4); // just file name
define('DEBUG_OUTPUT_LINE', 8); // line number
define('DEBUG_OUTPUT_ALL', DEBUG_OUTPUT_FILE | DEBUG_OUTPUT_LINE);

/**
* Debug class
*
* Class for outputting debug info.
*
* Example:
*   // enable debug to be outputted
*   Debug::on();
*   // set debug level to output debug message with filename and line name
*   Debug::level(DEBUG_OUTPUT_FILENAME | DEBUG_OUTPUT_LINE);
*   // print debug info (optional filename (__FILE__) and line number (__LINE__) can be passed)
*   Debug::debug('debug text here', __FILE__, __LINE__);
*   // suppress output of further debug() calls
*   Debug::off();
*   // no output will be produced
*   Debug::debug('testing');
*
* @author Keyvan Minoukadeh <hide@address.com>
* @version 0.1
*/
class Debug
{
    /**
    * Is on - returns true if debuggin is on, false otherwise
    * @return bool
    * @static
    */
    function is_on()
    {
        return (Debug::level() > 0);
    }

    /**
    * On - turn debugging on
    * @static
    */
    function on()
    {
        Debug::level(DEBUG_OUTPUT_ON);
    }

    /**
    * Off - turn debugging off
    * @static
    */
    function off()
    {
        Debug::level(DEBUG_OUTPUT_OFF);
    }

    /**
    * Debug - output debug info
    */
    function debug($text, $file='', $line='')
    {
        if (Debug::is_on()) {
            echo "* $text";
            if ($file) { 
                if (Debug::level() & DEBUG_OUTPUT_FILE) {
                    echo " (file \"$file\"";
                } elseif (Debug::level() & DEBUG_OUTPUT_FILENAME) {
                    echo ' (file "'.basename($file).'"';
                }
                if ($line && (Debug::level() & DEBUG_OUTPUT_LINE)) echo " line $line";
                echo ')';
            }
            echo "\n";
        }
    }

    /**
    * Level - set or get debug level
    * @param int $new_level
    * @return int
    */
    function level($new_level=null)
    {
        static $level = DEBUG_OUTPUT_OFF;
        $old_level = $level;
        if (isset($new_level)) $level = $new_level;
        return $old_level;
    }
}
?>
Return current item: Simple Debug