How to Debug Code in Hook Functions

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
gmusgrave
Posts: 25
Joined: 2013-08-15 17:27
Location: Mexico

How to Debug Code in Hook Functions

Post by gmusgrave » 2013-10-12 14:09

No matter at what level your coding skills, your first attempt to code anything significant almost never works as you expect it to. Computers are literal in the extreme, and do precisely what they're told. Most humans don't think that way (we make mental assumptions).

When your code doesn't work, you meed to debug it (find out why it doesn't work). One of the most common debugging tools is to print strings to the display telling you where you are in the code, and what the value of various variables are at that point.

For example, you might have some code inside an 'if' statement that doesn't appear to be working. You can place a debug statement just before the 'if' statement that prints the value of the variables in the 'if' condition. You can also put another debug statement just inside the 'if' statement. These will quickly tell you whether the 'if' statement code is even being executed and, if not, whether the values of the variables in the 'if' condition are what you expect them to be.

This is pretty standard stuff.

However, not all the functions in the TABLENAME.php file in the hooks directory are necessarily called at a point in the creation of the web page where it's possible to cause anything to be displayed in the web browser!

Here are some techniques to use for each type of hook function:

Technique #1:- TABLENAME_init(), TABLENAME_header(), TABLENAME_footer(), TABLENAME_before_delete(), TABLENAME_after_delete(), and TABLENAME_dv():

These functions are called at a point when the page is being created, and you could simply use the traditional debug method of using the echo function.

In all cases but one, your debug string will be seen at the immediate top of the page before the main navigation bar.

Example (this tells you you're just before the 'if' statement and shows you the value of three variables):

Code: Select all

echo "<div><p><strong>Before IF</strong></br>tblName: |$tblName|</br>csvOK: |$csvOK|</br>memInfoGroup: |$memInfoGroup|</p></div>";
NOTE: with the exception of TABLENAME_footer(), this string will appear before anything on the page including the header with the <html> tag. This technically generates a malformed page. It should, however, display in most modern browsers. If you're a purist, or prefer something more elegant, then one of the next two solutions might be a better alternative.


Technique #2:- TABLENAME_header(), TABLENAME_footer(), and TABLENAME_dv():

If you want to do something a bit more elegant and standards compliant than the echo function in these three functions, your can easily display your debug string by appending it to the end of another string that the function either passes by reference or is in its return value. This string will be used by the AppGini code to render the web page.

These strings are, respectively: $header, $footer, and $html.

In all cases, append your debug code to these strings immediately before the 'return' statement in these functions.

Example for TABLENAME_footer():

Code: Select all

$footer .= "<div><p><strong>Before IF</strong></br>tblName: |$tblName|</br>csvOK: |$csvOK|</br>memInfoGroup: |$memInfoGroup|</p></div>";
Technique #3:- All the other hook functions (before/after insert, before/after update, and CSV):

These remaining hook functions provide no access to the browser output, and so you need to adopt a different strategy.

Pass your debug string to one of the other functions mentioned in Technique #2 that append the debug string to another string (_header(), _footer(), or _dv()). The best choice is either _header() or _footer() because these are called whenever a page is generated. TABLENAME_dv() is only called when a detail view is to be generated, and this does not always happen. I prefer _footer() because it displays the debug string at the bottom of the page.

Here's how it works: iIn the function that you want to debug, assign your debug string to a session variable. Then the other function displays the contents of the session variable when the page is rendered.

Example code in the function you want to debug:

Code: Select all

$_SESSION['dbgStr'] = "<div><p><strong>Before IF</strong></br>tblName: |$tblName|</br>csvOK: |$csvOK|</br>memInfoGroup: |$memInfoGroup|</p></div>";
Example debug code in TABLENAME_footer():

Code: Select all

global $dbgHooksMode;
if ($dbgHooksMode === true) {
      $footer .= $_SESSION['dbgStr'];
}
This code goes after the close of the 'switch' statement, and before the 'return'.

You have to define the global variable $dbgHooksMode at the top of the TABLENAME.php file before the TABLENAME_init() function. You initialise it to 'false'. Under normal operation the debug code in TABLENAME_footer() does nothing. As soon as you change this variable to 'true,' you turn on your debugging code.

This technique can also be used in the TABLENAME_init(), TABLENAME_before_delete(), and TABLENAME_after_delete() functions to provide a more elegant alternative to the echo function.

That's it! Hope this helps someone.

Happy debugging,
Garry

KSan
AppGini Super Hero
AppGini Super Hero
Posts: 252
Joined: 2013-01-08 20:17

Re: How to Debug Code in Hook Functions

Post by KSan » 2013-12-30 03:30

This is great! Thanks for sharing this method.

Post Reply