* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage debugger */ /** * global debug section */ if( !defined( 'WBSITE_DEBUG_SECTION' ) ) { define( 'WBSITE_DEBUG_SECTION', 'Global' ); } if( !defined( 'WBDEBUG_INFO_SECTION' ) ) { define( 'WBDEBUG_INFO_SECTION', 'Debug Information' ); } /** * remember timestamp of genesis */ list( $usec, $sec ) = explode( ' ', microtime() ); $GLOBALS['_wbDebugger'] = array( 'singleton' => null, 'timestampGensis' => (float) $usec + (float) $sec ); /** * Debugger * * Abstract debugger and implements static (public) interface * * @version 1.0.0 * @package wombatSite * @subpackage debugger */ class wbDebugger { /** * list of debug messages * @access private * @var array $_msg */ var $_msg = array( 'Error' => array(), 'Warning' => array(), 'Notice' => array(), 'Strict' => array(), WBSITE_DEBUG_SECTION => array(), WBDEBUG_INFO_SECTION => array() ); /** * switch debugger on|off * @access private * @var bool $_active */ var $_active = true; /** * how much additional information should be added to debug out * @access private * @var int $_infoLevel */ var $_infoLevel = 1; /** * constructor * * @access public * @param bool $active switch on */ function __construct( $active = true ) { $this->_active = $active; } /** * Constructor for PHP 4 * * @access public * @param bool $active switch on */ function wbDebugger( $active = true ) { $this->__construct( $active ); } /** * start the debugger for static usage * * @access private * @param boolean $active switch on or off debugger * @param string $debugger * @return boolean $result true on success * @see singleton() */ function start( $active = true, $debugger = 'Html' ) { wbDebugger::singleton( $active, $debugger ); return TRUE; } /** * create a single instance of the debugger * * @static * @param boolean $active switch on or off debugger * @param string $debugger * @return debugger object */ function &singleton( $active = true, $debugger = 'Html' ) { if( $GLOBALS['_wbDebugger']['singleton'] ) { $GLOBALS['_wbDebugger']['singleton']->setActive( $active ); return $GLOBALS['Debugger']['singleton']; } require_once dirname( __FILE__ ) . '/wbDebugger/' . $debugger . '.php'; $class = 'wbDebugger_' . $debugger; $deb =& new $class( $active ); $GLOBALS['_wbDebugger']['singleton'] =& $deb; return $deb; } /** * allow to adjust internal timestamp of genesis * * The timestamp should be calculated as early as possible, * otherwise calucalation based on it may be missleading * * @static * @param float $time * @return boolean $result true on succes * @see getUTime() */ function adjustGenesis( $time ) { $GLOBALS['_wbDebugger']['timestampGenesis'] = $time; return true; } /** * switch on or off debugger * * Set debugger to active or not active * and optional select debugging information level * * @static * @param boolean $active * @param mixed $infoLEvel * @return boolean $result true on succes */ function setActive( $active = true, $infoLevel = null ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } $GLOBALS['_wbDebugger']['singleton']->_active = $active; // set infoLevel if( $infoLevel !== null ) { $GLOBALS['_wbDebugger']['singleton']->_infoLevel = $infoLevel; } return true; } /** * see whether debugger is switched on * * @static * @return boolean $active true if devugger is active */ function isActive() { return $GLOBALS['_wbDebugger']['singleton']->_active; } /** * get info level * * @static * @return boolean $active true if devugger is active */ function getInfoLevel() { return $GLOBALS['_wbDebugger']['singleton']->_infoLevel; } /** * print out debug messages * * @static * @param string $part named part * @return boolean $result true on success */ function printMsg( $part = null ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } // don't show anything if not active if( !$GLOBALS['_wbDebugger']['singleton']->isActive() ) { return true; } // additional debug information? if( $GLOBALS['_wbDebugger']['singleton']->_infoLevel >= 1 ) { // add global processing time $info = 'Processing time: ' . wbDebugger::getUTime( 'genesis', 1000, 6 ) . ' ms'; $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, $info, 'Time' ); // add memory usage if( function_exists('memory_get_usage' ) ) { $info = ceil( memory_get_usage() / 1024 ). ' kByte'; $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, $info, 'Memory' ); } // add php version $info = 'Version: ' . phpversion(); $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, $info, 'PHP' ); // add included php files /* $files = get_included_files(); $info = 'PHP Files: ' . count( $files ). $GLOBALS['_wbDebugger']['singleton']->sprint_r( $files ); $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, $info, 'Included' ); */ // add included php classes $files = wbFactory::getParam( 'loadedClasses' ); $info = 'Loaded classes: ' . count( $files ). $GLOBALS['_wbDebugger']['singleton']->sprint_r( $files ); $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, $info, 'Classes' ); // are we running in CLI mode? if( isset( $_SERVER['_'] ) ) { $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, wbDebugger::sprint_r( $_SERVER['argv'] ), 'Argv' ); } else { $GLOBALS['_wbDebugger']['singleton']->addMessage( WBDEBUG_INFO_SECTION, wbDebugger::sprint_r( $_REQUEST ), 'Request' ); } } return $GLOBALS['_wbDebugger']['singleton']->printMessages( $part ); } /** * format string for output * * @static * @param mixed &$var * @param string $type print_r|var_dump */ function sprint( $var ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return $GLOBALS['_wbDebugger']['singleton']->sprint( $var ); } /** * capture output of print_r to add it to debug info * * @static * @param mixed &$var * @param string $type print_r|var_dump */ function sprint_r( &$var, $type = 'print_r' ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'Debugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return $GLOBALS['_wbDebugger']['singleton']->sprint_r( $var, $type ); } /** * get micortime as float * * Recieve either current time or difference if start time is given. * This function is useful to meassure processing times * * @static * @param mixed $start either a float timestamp or "genesis" or NULL * @param int $factor * @return float $time */ function getUTime( $start = NULL, $factor = NULL, $digits = 10 ) { list( $usec, $sec ) = explode( ' ', microtime() ); $end = ( ( float ) $usec + ( float ) $sec ); if( $start === NULL ) { return $end; } // use genesis timestamp! if( $start == 'genesis' ) { $start = $GLOBALS['_wbDebugger']['timestampGenesis']; } $proc = ( $end - $start ); if( $factor !== NULL ) { $proc *= $factor; } $proc = number_format( $proc, $digits ); return $proc; } /** * static interface to add debug messages * * @static * @param string $part named part * @param string $text debugging message * @param string $title optional title * @return boolean $result true on success * @see addMessages */ function addMsg( $part, $text, $title = null ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return $GLOBALS['_wbDebugger']['singleton']->addMessage( $part, $text, $title ); } /** * start the debugger for static usage * * @access private * @param string $part named part * @param string $text debugging message * @return boolean $result true on success */ function addMessage( $part, $text, $title = null ) { if( !$this->_active ) { return true; } if( !isset( $this->_msg[$part] ) ) { $this->_msg[$part] = array(); } $msg = array( 'text' => $text, 'title' => $title ); array_push( $this->_msg[$part], $msg ); return true; } /** * recieve debug messages * * * @access private * @param string $part named part * @return array $msg list of debug messages */ function &getMessages( $part = null ) { if( $part === null ) { return $this->_msg; } if( !isset( $this->_msg[$part] ) ) { $msg = array(); return $msg; } return $this->_msg[$part]; } /** * echo debug messages * * * @abstract * @access pubic * @param string $part named part * @return boolean $result true on success */ function printMessages( $part = null ) { } } ?>