* @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() ); $genesis = (float) $usec + (float) $sec; /** * Debugger * * Abstract debugger and implements static (public) interface * * @version 1.0.0 * @package wombatSite * @subpackage debugger */ abstract class wbDebugger { /** * instance of myself * @var wbDebugger */ protected static $_instance = null; /** * instance of myself * @var wbDebugger */ protected static $_timestampGensis = null; /** * list of debug messages * @var array $_msg */ protected $_msg = array( 'Error' => array(), 'Warning' => array(), 'Notice' => array(), 'Strict' => array(), WBSITE_DEBUG_SECTION => array(), WBDEBUG_INFO_SECTION => array() ); /** * switch debugger on|off * @var bool $_active */ protected $_active = true; /** * how much additional information should be added to debug out * @var int $_infoLevel */ protected $_infoLevel = 1; /** * constructor * * @access public * @param bool $active switch on */ protected function __construct( $active = true ) { $this->_active = $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() */ public 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 */ public function &singleton( $active = true, $debugger = 'Html' ) { if( self::$_instance ) { self::$_instance->setActive( $active ); return self::$_instance; } require dirname( __FILE__ ) . '/wbDebugger/' . $debugger . '.php'; $class = 'wbDebugger_' . $debugger; $deb = new $class( $active ); self::$_instance = $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 * * @param float $time * @return boolean $result true on succes * @see getUTime() */ public static function adjustGenesis( $time ) { self::$_timestampGensis = $time; return true; } /** * switch on or off debugger * * Set debugger to active or not active * and optional select debugging information level * * @param boolean $active * @param mixed $infoLEvel * @return boolean $result true on succes */ public static function setActive( $active = true, $infoLevel = null ) { if( !self::$_instance ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } self::$_instance->_active = $active; // set infoLevel if( $infoLevel !== null ) { self::$_instance->_infoLevel = $infoLevel; } return true; } /** * see whether debugger is switched on * * @return boolean $active true if devugger is active */ public static function isActive() { return self::$_instance->_active; } /** * get info level * * @return boolean $active true if devugger is active */ public static function getInfoLevel() { return self::$_instance->_infoLevel; } /** * print out debug messages * * @param string $part named part * @return boolean $result true on success */ public static function printMsg( $part = null ) { if( !self::$_instance ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } // don't show anything if not active if( !self::$_instance->isActive() ) { return true; } // additional debug information? if( self::$_instance->_infoLevel >= 1 ) { // add global processing time $info = 'Processing time: ' . wbDebugger::getUTime( 'genesis', 1000, 6 ) . ' ms'; self::$_instance->addMessage( WBDEBUG_INFO_SECTION, $info, 'Time' ); // add memory usage if( function_exists('memory_get_usage' ) ) { $info = ceil( memory_get_usage() / 1024 ). ' kByte'; self::$_instance->addMessage( WBDEBUG_INFO_SECTION, $info, 'Memory' ); } // add php version $info = 'Version: ' . phpversion(); self::$_instance->addMessage( WBDEBUG_INFO_SECTION, $info, 'PHP' ); // add included php classes $files = wbFactory::getParam( 'loadedClasses' ); $info = count( $files ) . ' loaded: ' . implode( ', ', $files ); self::$_instance->addMessage( WBDEBUG_INFO_SECTION, $info, 'Classes' ); // are we running in CLI mode? if( isset( $_SERVER['_'] ) ) { self::$_instance->addMessage( WBDEBUG_INFO_SECTION, wbDebugger::sprint_r( $_SERVER['argv'] ), 'Argv' ); } else { self::$_instance->addMessage( WBDEBUG_INFO_SECTION, wbDebugger::sprint_r( $_REQUEST ), 'Request' ); } } return self::$_instance->printMessages( $part ); } /** * format string for output * * @static * @param mixed &$var * @param string $type print_r|var_dump */ public static function sprint( $var ) { if( !self::$_instance ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return self::$_instance->doSprint( $var ); } /** * capture output of print_r to add it to debug info * * @param mixed &$var * @param string $type print_r|var_dump */ public static function sprint_r( &$var, $type = 'print_r' ) { if( !self::$_instance ) { return patErrorManager::raiseError( 'Debugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return self::$_instance->doSprint_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 * * @param mixed $start either a float timestamp or "genesis" or NULL * @param int $factor * @return float $time */ public static 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 = self::$_timestampGensis; } $proc = ( $end - $start ); if( $factor !== NULL ) { $proc *= $factor; } $proc = number_format( $proc, $digits ); return $proc; } /** * static interface to add debug messages * * @param string $part named part * @param string $text debugging message * @param string $title optional title * @return boolean $result true on success * @see addMessages */ public static function addMsg( $part, $text, $title = null ) { if( !self::$_instance ) { return patErrorManager::raiseError( 'wbDebugger:1', 'Debugger instance is required', 'use Debugger::start() first' ); } return self::$_instance->addMessage( $part, $text, $title ); } /** * start the debugger for static usage * * @param string $part named part * @param string $text debugging message * @return boolean $result true on success */ protected 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 * * * @param string $part named part * @return array $msg list of debug messages */ protected function &getMessages( $part = null ) { if( $part === null ) { return $this->_msg; } if( !isset( $this->_msg[$part] ) ) { $msg = array(); return $msg; } return $this->_msg[$part]; } /** * format string for output * * @param string $var */ abstract protected function doSprint( $var ); /** * capture output of print_r to add it to debug info * * @param mixed &$var * @param string $type print_r|var_dump */ abstract protected function doSprint_r( &$var, $type = 'print_r' ); /** * echo debug messages * * @param string $part named part * @return boolean $result true on success */ abstract protected function printMessages( $part = null ); } wbDebugger::adjustGenesis( $genesis ); ?>