* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage debugger */ /** * global debug section */ if( !defined( 'WBSITE_DEBUG_SECTION' ) ) { define( 'WBSITE_DEBUG_SECTION', 'Global' ); } /** * remember timestamp of genesis */ list( $usec, $sec ) = explode( ' ', microtime() ); $GLOBALS['_wbDebugger'] = array( 'singleton' => null, 'timestampGensis' => (float) $usec + (float) $sec ); /** * Debugger * * @version 0.2 * @package wombatSite * @subpackage debugger */ class wbDebugger { /** * list of debug messages * @access private * @var array $_msg */ var $_msg = array( 'Error' => array(), 'Warning' => array(), 'Notice' => array(), WBSITE_DEBUG_SECTION => array() ); /** * switch debugger on|off * @access private * @var bool $_active */ var $_active = true; /** * 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 * * @static * @param boolean $active * @return boolean $result true on succes */ function setActive( $active = true ) { if( !$GLOBALS['_wbDebugger']['singleton'] ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } $GLOBALS['_wbDebugger']['singleton']->_active = $active; return true; } /** * see whether debugger is switched on * * @static * @return boolean $active true if devugger is active */ function isActive( ) { return $GLOBALS['_wbDebugger']['singleton']->_active; } /** * 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' ); } 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 ) { } } ?>