Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file debug.php

Documentation is available at debug.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: debug.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  * @subpackage Debug
  12.  */
  13.  
  14.  
  15. /**
  16.  * log a string to the designated output destination
  17.  *
  18.  * @param file             The file (passed from assertion handler)
  19.  * @param line             The line (passed from assertion handler)
  20.  * @param assert_trigger   The assert trigger (passed from assertion handler)
  21.  */
  22. if (!function_exists('pn_assert_callback_function'))
  23. {
  24.     function pn_assert_callback_function ($file$line$assert_trigger)
  25.     {
  26.         //pn_exit ('assertion failed', $file, $line, $assert_trigger);
  27.         return pn_exit ('assertion failed');
  28.     }
  29. }
  30.  
  31. /**
  32.  * Exit the program after displaying the appropriate messages
  33.  *
  34.  * @param msg         The messgage to show
  35.  * @param html        whether or not to generate HTML (can be turned off for command line execution)
  36.  */
  37. if (!function_exists('pn_exit'))
  38. {
  39.     function pn_exit ($msg$html=true)
  40.     {
  41.         global $PNConfig;
  42.         if (defined('_PNINSTALLVER'&& $PNConfig['System']['development']{
  43.             print ("Install Error:" $msg '<br />');
  44.             prayer (debug_backtrace());
  45.             return false;
  46.         }
  47.  
  48.         //Loader::loadClass('LogUtil');
  49.         $msg   "Exit-Handler: $msg\nStacktrace:\n";
  50.         $msg2  str_replace ("\n"'<br />'$msg);
  51.         $debug _prayer (debug_backtrace());
  52.  
  53.         LogUtil::log ($msg $debug'FATAL');
  54.  
  55.         global $PNConfig;
  56.         if ($PNConfig['System']['development'])
  57.         {
  58.             print ($msg2 $debug);
  59.             pnShutDown();
  60.         }
  61.  
  62.         return LogUtil::registerError ($msg);
  63.     }
  64. }
  65.  
  66.  
  67. /**
  68.  * Serialize the given data in an easily human-readable way for debug purposes
  69.  *
  70.  * Taken from http://dev.nexen.net/scripts/details.php?scripts=707
  71.  *
  72.  * @param data        The object to serialize
  73.  * @param functions   whether to show function names for objects (default=false) (optional)
  74.  *
  75.  * @return string A string containing serialized data
  76.  */
  77. if (!function_exists('_prayer'))
  78. {
  79.     function _prayer ($data$functions=false$recursionLevel=0)
  80.     {
  81.         if ($recursionLevel 5{
  82.           return 'Max recursion level reached.';
  83.           return;
  84.         }
  85.  
  86.         global $PNConfig;
  87.         if (defined('_PNINSTALLVER'&& !$PNConfig['System']['development']{
  88.             return;
  89.         }
  90.  
  91.         $text '';
  92.  
  93.         if ($functions != 0{
  94.             $sf 1;
  95.         else {
  96.             $sf ;
  97.         }
  98.  
  99.         if (isset ($data)) {
  100.             if (is_array($data|| is_object($data)) {
  101.                 $datatype gettype($data);
  102.                 if (count ($data)) {
  103.                     $text .= "<ol>\n";
  104.  
  105.                     foreach ($data as $key => $value)  {
  106.                         $type gettype($value);
  107.  
  108.                         if ($type == 'array' || ($type == 'object' && get_object_vars($value))) {
  109.                             $text .= sprintf ("<li>(%s) <strong>%s</strong>:\n"$type$key);
  110.                             $text .= _prayer ($value$sf$recursionLevel+1);
  111.                             $text .= '</li>';
  112.                         elseif (eregi ('function'$type)) {
  113.                             if ($sf{
  114.                                 $text .= sprintf ("<li>(%s) <strong>%s</strong> </li>\n"$type$key$value);
  115.                                 /*  There doesn't seem to be anything traversable inside functions. */
  116.                             }
  117.                         else {
  118.                             if (!isset($value)) {
  119.                                 $value='(none)';
  120.                             }
  121.  
  122.                             // You cannot do dataUtil::formatForDisplay on an object, so just display object type
  123.                             if (is_object($value))
  124.                                 $value gettype($value);
  125.  
  126.                             if ($datatype == 'array'{
  127.                                 $text .= sprintf ("<li>(%s) <strong>%s</strong> = %s</li>\n"$type$keydataUtil::formatForDisplay($value));
  128.                             }
  129.                             elseif ($datatype == 'object'{
  130.                                 $text .= sprintf ("<li>(%s) <strong>%s</strong> -> %s</li>\n"$type$keydataUtil::formatForDisplay($value));
  131.                             }
  132.                         }
  133.                     }
  134.  
  135.                     $text .= "</ol>\n";
  136.                 else {
  137.                     $text .= '(empty)';
  138.                 }
  139.             else {
  140.                 $text .= $data;
  141.             }
  142.         }
  143.         return $text;
  144.     }
  145. }
  146.  
  147.  
  148. /**
  149.  * Serialize the given data in an easily human-readable way for debug purposes
  150.  *
  151.  * Taken from http://dev.nexen.net/scripts/details.php?scripts=707
  152.  *
  153.  * @param data        The object to serialize
  154.  * @param functions   whether to show function names for objects (default=false) (optional)
  155.  *
  156.  * @return nothing, the data is directly printed
  157.  */
  158. if (!function_exists('prayer'))
  159. {
  160.     function prayer ($data$functions=false)
  161.     {
  162.         global $PNConfig;
  163.         if (defined('_PNINSTALLVER'&& !$PNConfig['System']['development']{
  164.             return;
  165.         }
  166.  
  167.         $text  '<div style="text-align:left">';
  168.         $text .= _prayer ($data$functions);
  169.         $text .= '</div>';
  170.         print ($text);
  171.     }
  172. }
  173.  
  174.  
  175. /**
  176.  * Simple timer class to measure code execution times.
  177.  *
  178.  * You can take multiple snapshots by calling the snap() function.
  179.  * For multiple measurements with 1 Timer, some basic statistics
  180.  * are computed.
  181.  *
  182.  * @package Zikula_Core
  183.  * @subpackage Debug
  184.  */
  185. if (!class_exists('Timer'))
  186. {
  187.     class Timer
  188.     {
  189.         var $name;
  190.         var $times;
  191.  
  192.  
  193.         /**
  194.          * Constructor
  195.          *
  196.          * @param name    The name of the timer
  197.          */
  198.         function Timer ($name='')
  199.         {
  200.             $this->name  = $name;
  201.             $this->times = array();
  202.             $this->start ();
  203.         }
  204.  
  205.  
  206.         /**
  207.          * reset the timer
  208.          *
  209.          * @param name    The name of the timer
  210.          */
  211.         function reset ($name='')
  212.         {
  213.             $this->name  = $name;
  214.             $this->times = array();
  215.             $this->start ();
  216.         }
  217.  
  218.  
  219.         /**
  220.          * return the current microtime
  221.          */
  222.         function get_microtime()
  223.         {
  224.             $tmp split(" ",microtime());
  225.             $rt  $tmp[0]+$tmp[1];
  226.             return $rt;
  227.         }
  228.  
  229.  
  230.         /**
  231.          * start the timer
  232.          */
  233.         function start ()
  234.         {
  235.             $this->times[$this->get_microtime();
  236.         }
  237.  
  238.  
  239.         /**
  240.          * stop the timer
  241.          */
  242.         function stop ($insertNewRecord=true)
  243.         {
  244.             if ($insertNewRecord)
  245.                 $this->times[$this->get_microtime();
  246.  
  247.             if (count($this->times<= 2)
  248.                 return $this->stop_single();
  249.  
  250.             return $this->stop_multiple ();
  251.         }
  252.  
  253.  
  254.         /**
  255.          * print the timer results for a single measurement
  256.          */
  257.         function stop_single()
  258.         {
  259.             $start $this->times[0];
  260.             $stop  $this->times[1];
  261.             $diff  $stop $start;
  262.  
  263.             $data['count'1;
  264.             $data['start'$start;
  265.             $data['stop']  $stop;
  266.             $data['diff']  $diff;
  267.  
  268.             return $data;
  269.         }
  270.  
  271.  
  272.         /**
  273.          * print the timer results for multiple measurement
  274.          */
  275.         function stop_multiple ()
  276.         {
  277.             $min   9999999;
  278.             $max   = -9999999;
  279.             $sum   0;
  280.             $size  count($this->times);
  281.             $start $this->times[0];
  282.             $d     0;
  283.             $data  array();
  284.  
  285.             for ($i=1$i<$size$i++)
  286.             {
  287.                 $last $this->times[$i-1];
  288.                 $stop $this->times[$i];
  289.  
  290.                 $diff $stop $last;
  291.  
  292.                 if ($diff $min{
  293.                     $min $diff;
  294.                 }
  295.  
  296.                 if ($diff $max{
  297.                     $max $diff;
  298.                 }
  299.  
  300.                 $d += $diff;
  301.             }
  302.  
  303.             $start $this->times[0];
  304.             $stop  $this->times[$size-1];
  305.  
  306.             $avg $d/$size;
  307.  
  308.             $data['count'$size;
  309.             $data['start'$start;
  310.             $data['stop']  $stop;
  311.             $data['min']   $min;
  312.             $data['max']   $max;
  313.             $data['avg']   $avg;
  314.             $data['diff']  $d;
  315.             //$stats = sprintf ("(%d runs, min=%f, max=%f, avg=%f)\n", $size, $min, $max, $avg);
  316.  
  317.             //print ("PNTimer: $diff $stats $this->name\n");
  318.             return $data;
  319.         }
  320.  
  321.  
  322.         /**
  323.          * take a snapshot while continuing the timing run
  324.          */
  325.         function snap ($doStats=false)
  326.         {
  327.             $this->times[$this->get_microtime();
  328.             if ($doStats{
  329.                 return $this->stop_multiple();
  330.             }
  331.         }
  332.     }
  333. }

Documentation generated on Fri, 18 Jul 2008 21:44:52 +0200 by phpDocumentor 1.4.1