Présentation
- utilisez un autoloader de classe pour vous simplifier la vie
- j'utilise ça preque exclusivement en version statique, pour faire des sites spaghettis
- Debug::var_dump()
- Debug::here() si vous utilisez des output buffer (ob_start)
- Debug::printStack()
- Debug::describeState()
- c'est documenté pour phpdocumentor (enfin la partie la plus utile = statique )
- s'active et se desactive partout sans pb de visibilité avec Debug::$inhibit
- fonctionne dans le shell Debug::outputHTML
je suis pointilleux pour le coté GPL2. je répond au message par MP si vous trouvez un bug (dans la classe, pas dans votre code).
<?php /** * @package framework * @author Pierre-Emmanuel Périllon * @license GPL2 * * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. **/ /** * @see __autoload() * */ require_once('autoload.inc.php'); /** * @package framework */ //class DebugException extends MyException{}; /** * This class allow to print lots of debug info in different context (with static method) * and allow to debug $_SESSION by initialising $_SESSION = new Debug(); * @package framework */ class Debug extends ArrayObject implements Serializable { /** * @var boolean inhibe le fonctionnement de la classe lorsqu'à vrai. * */ static $inhibit; /** * @var this is a filename where i can up ArrayObject info * */ static $debug_outup; /** * @var ressource this is a file * */ private $fp; /** * @var string identifiant unique de l'objet. * */ private $uid; /*****/ static $outputHTML = true; /* Méthodes */ function __construct ( ) { $this->init(); $this->writeLog(__FUNCTION__); parent::__construct(); } function __destruct( ) { $this->writeLog(__FUNCTION__); { } $this->fp = null; } function __toString() { return $this->uid; } function append ( $value ) { $this->writeLog(__FUNCTION__); parent::append($values); } { $this->writeLog(__FUNCTION__); } //function count ( ) function exchangeArray (&$input ) { $this->writeLog(__FUNCTION__); return parent::exchangeArray ( $input ); } //function getFlags ( void ) //function getIterator ( void ) //function getIteratorClass ( void ) function init() { { if ( $this->fp === false ) { throw new Exception("ne peut ouvrir le fichier de log"); } } else { $this->fp = null; } } { $this->writeLog(__FUNCTION__); } { $this->writeLog(__FUNCTION__); } { $this->writeLog(__FUNCTION__); } // function offsetExists ( mixed $index ) // function offsetGet ( mixed $index ) function offsetSet ( $index , $newval ) { return parent::offsetSet( $index , $newval); } function offsetUnset ( $index ) { $this->writeLog(__FUNCTION__, $index ); parent::offsetUnset ( $index ); } { $this->writeLog(__FUNCTION__); } // function setFlags ( int $flags ) // function setIteratorClass ( string $iterator_class ) { $this->writeLog(__FUNCTION__); } { $this->writeLog(__FUNCTION__); } /** * ce serrait tres beau si ça marchait comme ça. * */ { $this->init(); $this->writeLog(__FUNCTION__); } /*** * tue le script en cours. * * @access public * @static * @return void * @uses Debug::getCaller() ***/ static public function kill() { if ( self::$inhibit ) return ; echo ' <!-- SCRIPT KILLED !!! --> ',$tmp = self::getCaller(),' <!-- will kown unload outputBuffer --> '; { echo ' <!-- new Buffer --> } echo '<!-- SCRIPT KILLED !!! ' ,$tmp ,' -->'; exit; } static function describeState( $e = null ) { Debug::printCaller(1); echo ' <div> <strong>Informations</strong> <p> <strong>Exception</strong> <strong>Input: Session</strong> <strong>Input: GET</strong> <strong>Input: POST</strong> <strong>Input: FILE</strong> <strong>Input: COOKIE</strong> <strong>Input: Included files</strong> </div>'; } /** * renvoie la pile d'appel purgée des appels internes dans la classe. * @static * @uses Debug::ignoreDebugCall() * @return array */ { self::ignoreDebugCall($x); return $x; } /*** * renvoie une chaine formatée en xhtml qui contient le nom de la * fonction dans la pile d'appel à la profondeur passée en paramettre. * attention, cette pile est dépourvue des appels internes à la classe. * @static * @access public * @param integer $i (optionnel) profondeur supplementaire * @uses Debug::printCaller() * @return string */ static public function getCaller($i = 0) { self::printCaller($i); } /** * revoie une pile formatée en xhtml pour affichage * @static * @access private * @param boolean $full * @uses Debug::$inhibit * @uses Debug::ignoreDebugCall() * @uses Debug::printStackLine() * @uses Debug::var_dump() **/ static function getStack( $full = false) { if ( self::$inhibit ) return ; self::ignoreDebugCall($tab); // ($tab); if ( self::$outputHTML ) { echo "<ol class=\"debugStack\">\n"; } foreach( $tab as $y => $x ) { //on se moque pas mal des appels à l'intérieur de la classe //Ceci dit c'est + pratique de tester le fichier que la classe. echo self::$outputHTML?'<li>':"\t"; self::printStackLine($x); if ( $full ) { } echo self::$outputHTML?'</li>':"\n"; } if ( self::$outputHTML ) { echo "</ol>\n"; } } /** * affiche immédiatement (en option ou pas) ou est le script en * s'affranchissant des éventuels tampons de capture de sortie * @static * @param mixed $msg affiche le contenu d'une variable (avec le type, via serialize) * @param boolean $unloadBuffer si vrai s'affranchir des ob_start * @uses Debug::unloadOutBuffer() * @uses Debug::printStackLine() * @uses Debug::reloadOutBuffer() * @return void * */ static public function here( $msg = null, $unloadBuffer = true ) { if ( self::$inhibit ) return ; if ( $unloadBuffer ) $buffer = self::unloadOutBuffer(); self::printNewLine(); self::printStackLine($tab[0]); self::printNewLine(); if ( $unloadBuffer ) self::reloadOutBuffer($buffer); } static protected function printNewLine() { echo self::$outputHTML ?'<br/>':"\n"; } /** * enlève les appels interne à à la pile d'un bactrace * @static * @access protected * @param array $tab * @return void * */ static protected function ignoreDebugCall(&$tab ) { while ( or { } } /** * affiche la ligne et le fichier responsable de l'appel à cette fonction * @static * @param integer $i profondeur supplémentaire, si trop grand l'appel du bas de la pile * @uses Debug::ignoreDebugCall() * @uses Debug::printStackLine() * @uses self::$inhibit() * return void * */ static public function printCaller($i = 0) { if ( self::$inhibit ) return ; self::ignoreDebugCall($tab); { self::printStackLine($tab[$i]); } else { } } function printSimpleBackTrace($full = false) { if ( self::$inhibit ) return ; self::ignoreDebugCall($tab); foreach( $tab as $tmp ) { foreach( $tmp as $k => $v ) { echo " ",$k,'=>'; if ( !$full and $k !== 'object' ) { echo $v; } else { } } self::printNewLine(); } } /*** * affiche une exception en html, css embeded * @static * @uses Debug::printExceptionHTML() * @uses Debug::printExceptionTXT() * @uses Debug::$inhibit * @return void * */ static public function printException(Exception $e) { if ( self::$inhibit ) return ; self::$outputHTML?printExceptionHTML($e):printExceptionTXT($e); } /*** * affiche une exception en html, css embeded * @static * @uses Debug::printStackLinkHTML() * @uses Exception::getFile() * @uses Exception::getLine() * @uses Exception::getTraceAsString() * @return void * */ static protected function printExceptionHTML(&$e) { echo ' <pre style="color:red" class="debugStack"> Exception lancée dans'; self::printStackLinkHTML( $e->getFile(), $e->getLine() ); '</pre>'; } /*** * affiche une exception en html, css embeded * @static * @uses Exception::getFile() * @uses Exception::getLine() * @uses Exception::getTraceAsString() * @return void * */ static protected function printExceptionTXT(&$e) { echo ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Exception lancée dans', $e->getFile(),':', $e->getLine(),' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! '; } /*** * affiche l'état de la pile dans le flux, utilise des css du site. * @static * @access public * @return void * @uses Debug::$inhibit * @uses Debug::getStack() * */ static public function printStack($full = false) { if ( self::$inhibit ) return ; echo self::getStack($full); } /** * affiche une ligne de pile d'appel * @param array $x une ligne de backtrace * @access protected * @static * @return void * @uses Debug::printStackLinkHTMLHTML() * @uses Debug::printStackLineTXT() * */ static protected function printStackLine($x) { if ( self::$outputHTML ) { self::printStackLineHTML($x); } else { self::printStackLineTXT($x); } } static protected function printStackLineHTML($x) { { $class = $x['class'].$x['type']; } else { $class = null; } { echo '<b>',$class,$x['function'],'</b> appelé en '; self::printStackLinkHTML($x['file'], $x['line']); } { echo '<b>',$class,$x['function'],'</b> appelé en callback'; { echo ' depuis '; self::printStackLinkHTML( $x['args'][0], $x['args'][1]); } } else { echo '<b>',$class,$x['function'],'</b>'; } return true; } static protected function printStackLineTXT($x) { { $class = $x['class'].$x['type']; } else { $class = null; } { echo $class,$x['function'],' appelé en ',$x['file'],':',$x['line']; } { echo $class,$x['function'],' appelé en callback'; { echo ' depuis ',$x['args'][0],':',$x['args'][1]; } } else { echo $class,$x['function']; } return true; } /** * cree le p'tit lien dans la pile pour aller vers ma doc sur BddSql * @access protected * @static * @uses Debug::uri2url() * @uses SourceCode::$param * @param filename $file * @param integer $line * @return void * */ static protected function printStackLinkHTML($file, $line) { echo '<a href="',self::uri2url($file),'?',SourceCode::$param, '#line',$line,'" title="voir le code">',$file,'</a>:',$line; } /** * recrée une pile de outputBuffer à partir d'un tableau de chaine * @static * @param $array le contenu à remettre en buffer. * @uses Debug::$inhibit * @see self::unloadOutBuffer() * */ static public function reloadOutBuffer($buff) { if ( self::$inhibit ) return ; for( $it = $n ; $it >= 0 ; $it--) { echo $buff[$it]; } } /** * dépile les outputbuffer vers un tableau * @static * @uses Debug::$inhibit * @return array * @see self::reloadOutBuffer() * */ static public function unloadOutBuffer() { if ( self::$inhibit ) return ; { } return $buff; } /*** * replace un nom de fichier local en ce qu'il devrait être sur le web * pet'te que ça marche pas. * @param filename $file * @return string * */ static protected function uri2url( $file ) { $hide[] = '/var/www'; $hide[] = $_SERVER['DOCUMENT_ROOT']; } /*** * Affiche la définition et les valeurs d'un objet (type inconnu) * @param $mixed $objet * @param boolean $here * @uses Debug::ignoreDebugCall() * @uses Debug::printStackLine( * */ { if ( self::$inhibit) return; if ( self::$outputHTML ) { echo '<pre style="background-color:WhiteSmoke;color:gray">'; } if ( $here ) { self::ignoreDebugCall($x); self::printStackLine($x[0]); } echo "\n"; if ( self::$outputHTML ) { '</pre>'; } } /*** * affiche les stats d'utilisation de la mémoire. * */ static public function printMemoryUsage() { //Let's do some exotic piece of code... function calculerTailleUniteOctets($taille) { if ( $taille < 1024 ) { return $taille.' O'; } else if ( $taille < 1048576 ) { } else if ( $taille < 1073741824 /*2^30*/ ) { } else if ( $taille < 1099511627776 ) { } else //1125899906842624 { } } if ( self::$inhibit ) return ; global $start_time; echo ( self::$outputHTML )?' <pre style="background-color:WhiteSmoke;color:gray"> ':' ------------------------------------------------------------------------ '; echo 'memory usage (current): ',$n,' - ',calculerTailleUniteOctets($n),"\n", 'memory usage (peak): ',$m,' - ',calculerTailleUniteOctets($m),"\n", echo self::$outputHTML ? ' </pre> ':' ------------------------------------------------------------------------ '; } /** * @param string $action * */ protected function writeLog($action, $option = null) { { } } } Debug::$debug_outup = 'DEBUG_ARRAY.log'; Debug::$inhibit = false;
Developpez.com décline toute responsabilité quant à l'utilisation des différents éléments téléchargés.




La discussion sur le forum
Poster une réponse