Community Forums

[SOLUTION] Debugging mode in OpenCart

Community created contributions & mods for OpenCart 1.x

[SOLUTION] Debugging mode in OpenCart

Postby xendex » Mon Feb 28, 2011 9:16 am

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
debug.zip
(493 Bytes) Downloaded 1035 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:
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 Mon Feb 28, 2011 11:01 am, edited 1 time in total.
xendex
 
Posts: 3
Joined: Sun Feb 27, 2011 10:36 am
Location: Ukraine

Re: [SOLUTION] Debugging mode in OpenCart

Postby SapporoGuy » Mon Feb 28, 2011 10:01 am

Does this also count the number of sql queries?
930sc ... because it is fun!
User avatar
SapporoGuy
 
Posts: 1132
Joined: Mon Nov 01, 2010 6:29 am

Re: [SOLUTION] Debugging mode in OpenCart

Postby xendex » Mon Feb 28, 2011 10:07 am

Yes, when SQL_DEBUG is enabled.
xendex
 
Posts: 3
Joined: Sun Feb 27, 2011 10:36 am
Location: Ukraine

Re: [SOLUTION] Debugging mode in OpenCart

Postby SapporoGuy » Mon Feb 28, 2011 10:25 am

Great! Your mod will now replace my code :)

Thanks!
930sc ... because it is fun!
User avatar
SapporoGuy
 
Posts: 1132
Joined: Mon Nov 01, 2010 6:29 am

Re: [SOLUTION] Debugging mode in OpenCart

Postby xendex » Mon Feb 28, 2011 10:44 am

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
}
xendex
 
Posts: 3
Joined: Sun Feb 27, 2011 10:36 am
Location: Ukraine

Re: [SOLUTION] Debugging mode in OpenCart

Postby SapporoGuy » Sun Mar 13, 2011 5:29 am

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
SapporoGuy
 
Posts: 1132
Joined: Mon Nov 01, 2010 6:29 am

Re: [SOLUTION] Debugging mode in OpenCart

Postby orkney » Fri Jan 31, 2014 12:02 pm

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
orkney
 
Posts: 2
Joined: Tue Nov 13, 2012 12:39 am


Return to Free Contributions

Who is online

Users browsing this forum: No registered users and 21 guests

Hosted by Arvixe Web Hosting