<?php

/***************************************************************************\
*                                                                           *
* Place this code at the top of your script                                 *
*                                                                           *
\***************************************************************************/

/*
 * Sample code to demonstrate a cool way yo debug PHP code including timing
 * details.
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site:
 *  debuglog.inc.php:
 *    http://www.pgregg.com/projects/php/debuglog/debuglog.inc.phps
 *  sample use:
 *    http://www.pgregg.com/projects/php/code/code_debug_timing.php
 *
 * (c) Paul Gregg, 2007
 * 4th April 2007
 * http://www.pgregg.com/projects/
 *
 */

if (!isset($GLOBALS['DEBUGLOG']))
  $GLOBALS['DEBUGLOG'] = array(microtime(true));  // This is our debugging log array.
$GLOBALS['DO_DEBUG'] = true;

if ($GLOBALS['DO_DEBUG']) { // define the function that will record our data

  // debuglog('message' [,'message2') is called at strategic points in your code.
  Function debuglog() {
    #global $DEBUGLOG;
    $args = func_get_args();
    $i = count($GLOBALS['DEBUGLOG']);
    $GLOBALS['DEBUGLOG'][$i] = sprintf("%0.06f: ", microtime(true)-$GLOBALS['DEBUGLOG'][0]);
    foreach($args as $str)
      $GLOBALS['DEBUGLOG'][$i] .= $str;
  }

} else { // ignore calls to debuglog()

  Function debuglog() {}

}


// A function to extract the time of the selected log entry. If no argument
// (or -1) is passed then it returns the time sinxe the start of the script.
Function debuglog_timediff($index = -1) {
  global $DEBUGLOG;
  if ($index > -1)
    list($logtime, $message) = explode(':', $DEBUGLOG[$index], 2);
  else
    $logtime = sprintf("%0.06f", microtime(true)-$DEBUGLOG[0]);
  return $logtime;
}

Function debuglog_print($returntrue=false, $preline='', $colsep=":\t", $postline="\n") {
  // default arguments will print the log in plain text.
  // To output HTML table rows use
  //   debuglog_print(false,'<tr><td>','</td><td>','</td></tr>');

  global $DEBUGLOG;
  $starttime = $DEBUGLOG[0];
  $output = $preline . 'Script started at'
            . $colsep . date('Y/m/d H:i:s', $starttime) . $postline;
  $lastlogtime = 0;
  foreach ($DEBUGLOG as $index => $debugline) {
    if ($index == 0) continue;
    list ($logtime, $message) = explode(':', $debugline, 2);
    $output .= $preline 
             . $logtime*1000 . ' ms'
             . $colsep 
             . '+'.sprintf('%.03f',($logtime-$lastlogtime)*1000) . ' ms'
             . $colsep 
             . $message 
             . $postline;
    $lastlogtime = $logtime;
  }
  if ($returntrue)
    return $output;
  else
    print $output;
  return '';
}



/***************************************************************************\
*                                                                           *
* End of the code to place at the top of your script                        *
*                                                                           *
\***************************************************************************/
?>