Post by xendex » Mon Feb 28, 2011 10:16 pm

Hi,
I noticed that there's no solution for debugging execution time, memory and sql queries in OpenCart.
This is how you can add this useful(especially for developers) feature:
1. Extract attached archive in /system/helper/ directory.
2. Open index.php file(located in root directory).
3. Add the following block at the beginning of the file:

Code: Select all

// 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:

Code: Select all

// 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:

Code: Select all

// 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:

Code: Select all

public function query($sql) {
add the following line:

Code: Select all

if(defined('SQL_DEBUG')) { $caller = debug_backtrace(); Debug::trigger('sql', $sql, $caller); }
That's all. As you can see you can enable/disable debug by (un)commenting DEBUG and SQL_DEBUG constant definition.
In SQL queries debugger you will see 3-column table where:
#1 is query number
#2 is query itself
#3 file and the line in the brackets, where query() method was called; Class name :: method(), which called query()

Hope the debug feature will be added by default in future versions.
Last edited by xendex on Tue Mar 01, 2011 12:01 am, edited 1 time in total.

Newbie

Posts

Joined
Sun Feb 27, 2011 11:36 pm
Location - Ukraine

Post by SapporoGuy » Mon Feb 28, 2011 11:01 pm

Does this also count the number of sql queries?

930sc ... because it is fun!


User avatar
Active Member

Posts

Joined
Mon Nov 01, 2010 7:29 pm

Post by xendex » Mon Feb 28, 2011 11:07 pm

Yes, when SQL_DEBUG is enabled.

Newbie

Posts

Joined
Sun Feb 27, 2011 11:36 pm
Location - Ukraine

Post by SapporoGuy » Mon Feb 28, 2011 11:25 pm

Great! Your mod will now replace my code :)

Thanks!

930sc ... because it is fun!


User avatar
Active Member

Posts

Joined
Mon Nov 01, 2010 7:29 pm

Post by xendex » Mon Feb 28, 2011 11:44 pm

P.S. If you need to disable debugging on ajax request, just replace:

Code: Select all

define('DEBUG', true); // comment to disable debug mode
define('SQL_DEBUG', true); // comment to disable sql-debugging
with:

Code: Select all

if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
	define('DEBUG', true); // comment to disable debug mode
	define('SQL_DEBUG', true); // comment to disable sql-debugging
}

Newbie

Posts

Joined
Sun Feb 27, 2011 11:36 pm
Location - Ukraine

Post by SapporoGuy » Sun Mar 13, 2011 6:29 pm

Finally added this to my system.

Very nice!

Before, I was using a combination of my own code and Q's query logger.

I see that each query doesn't have a time.
Can this be added?

I changed your code a little:

I added to index.php

Code: Select all

	define('SQL_DEBUG_SHOW', true); // comment to disable showing of the actual sql query
And changed the debug.php function to:

Code: Select all

	public static function output() {
		$sql_num = count(self::$log['sql']);
		echo '<tr><td colspan="3">SQL Queries Executed: ' . $sql_num . '<br/>';

		if (defined('SQL_DEBUG_SOW')) {
			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>';
			}
		}
	}
This allows you to turn on/off the showing of all those queries.

However, I really would like to move these queries to a file so that you can still see them in the admin like the errors.
Also, would like to be able to control turning on/off parts of the debugging in the admin like the errors.

To bad the timing needs to be declared before you can check settings from the DB.

930sc ... because it is fun!


User avatar
Active Member

Posts

Joined
Mon Nov 01, 2010 7:29 pm

Post by orkney » Sat Feb 01, 2014 1:02 am

I've been having trouble getting this to work on OpenCart v.1.5.6.1. There are no errors reported in Opencarts Error Log, but no debug output is ever printed below our site footer. Does anyone have this running on the latest OC?

User avatar
Newbie

Posts

Joined
Tue Nov 13, 2012 1:39 pm
Who is online

Users browsing this forum: No registered users and 50 guests