Thoughts, tips and ideas on next-gen technology by: Moshe Brevda


Debugging FreePBX: the dbug() function

So, you've figured out the whole bootstrap thing, and now you are starting to poke around and trying to write some stuff of your own. And then you hit a snag. Here a tip that should help when need to debug FreePBX.

First, meet dbug(), the official debug function. The dbug function is your friend and in invaluable resourse when trying to figure out why that query isn't returning the results you excepted or why the post seems to be returned blank. Think of dbug() as php's builtin print_r()/var_dump(), time stamping, and logging all rolled in to one simple function.

To get started with dbug(), first ensure that dbug() output is not disabled. To do so, go to Advanced Setting and set "Disable FreePBX dbug Logging" to false. Hit the green accept button. dbug() output is now  enabled. By default, dbug() stores all its output to /tmp/freepbx_debug.log - but you dont have to remember this path. You can use this handy shortcut from the linux cli to bring up the debug output:

amportal a dbug

This tails the log file for you, continuously reading from it. It will also attempt to read from the httpd error log, assuming it is in the default location.

So we have dbug logging enabled and we have the log up. Now we need to insert a dbug call. Using dbug() is relay quite simple, you can pass it 1-3 arguments:


Will output your string. The logfile will also include the filename and even the line name where the dbug() function is located. You can use this to "step through" your code and see at what php bombs out (this is especially useful for the 'white screen of death').

dbug('my var', $var);

In this case, you will probably want to set the string to something informative (although you dont have to), and include the variable to be output. This will allow you to see what going on "inside" your variable. This is especially useful with arrays.

dbug('my var', $var, 1);

This last example is most extreme and while its basically a repeat of the last example, it outputs and array using var_dump (instead of print_r()) which give a more accurate expression of whats in side that array.

When developing, this function is one of the most handy to have laying around.