Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Created November 12, 2012 00:51
Show Gist options
  • Select an option

  • Save hkulekci/4056960 to your computer and use it in GitHub Desktop.

Select an option

Save hkulekci/4056960 to your computer and use it in GitHub Desktop.
<?php
class Debug {
private static $log = array();
public static function trigger($type, $data, &$caller) {
if(!isset(self::$log[$type])) self::$log[$type] = array();
if(!isset(self::$log[$type.'_func'])) self::$log[$type.'_func'] = array();
self::$log[$type][] = $data;
self::$log[$type.'_func'][] = array($caller[0]['file'],$caller[0]['line'],(!empty($caller[1]['class']) ? '<br/>'.$caller[1]['class'].'::'.$caller[1]['function'] : ''));
}
public static function output() {
$sql_num = count(self::$log['sql']);
echo '<tr><td colspan="3">'.$sql_num.' sql queries executed:</td></tr>';
for($i = 0; $i < $sql_num; $i++) {
echo '<tr><td>'.($i+1).'.</td><td>'.self::$log['sql'][$i].'</td><td>'.self::$log['sql_func'][$i][0].' <i>('.self::$log['sql_func'][$i][1].')</i>'.(self::$log['sql_func'][$i][2] ? '<b>'.self::$log['sql_func'][$i][2].'</b>' : '').'</td></tr>';
}
}
}
?>
<?php
// From : http://forum.opencart.com/viewtopic.php?t=27918
/*
1. Extract attached archive
debug.zip
(493 Bytes) Downloaded 335 times
in /system/helper/ directory.
2. Open index.php file(located in root directory).
3. Add the following block at the beginning of the file:
*/
// debug hack start
define('DEBUG', true); // comment to disable debug mode
define('SQL_DEBUG', true); // comment to disable sql-debugging
if(defined('DEBUG')) {
$start_time = microtime();
$start_mem = memory_get_usage();
}
// debug hack endd
// 4. Right after require_once('config.php'); line add:
// debug hack start
if(defined('SQL_DEBUG'))
require_once(DIR_SYSTEM . 'helper/debug.php');
// debug hack end
// 5. And at the end of the file (after $response->output(); line) add:
// debug hack start
if(defined('DEBUG')) {
$end_time = (microtime() - $start_time);
$end_mem = (memory_get_usage() - $start_mem);
echo '<style>#debug tr:hover {background:#FFC}</style><table id="debug" border="1" width="100%">';
echo '<tr><td colspan="3">Memory Usage: '.round((($end_mem / 1024) / 1024), 6).' MB</td></tr>';
echo '<tr><td colspan="3">Execution Time: '.abs(round($end_time, 6)).' seconds</td></tr>';
if(defined('SQL_DEBUG'))
Debug::output();
echo '</table>';
}
// debug hack end
// 6. Open /system/library/db.php file and after line:
public function query($sql) {
// add the following line:
if(defined('SQL_DEBUG')) { $caller = debug_backtrace(); Debug::trigger('sql', $sql, $caller); }
//// ....
?>
@iLevye
Copy link
Copy Markdown

iLevye commented Jun 16, 2016

something wrong with microtime(). i changed microtime() to time()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment