(first posted: Jul 05, 2006)
(1571 Reads)
keywords: block layout debugging php
Permalink
Strategies for Debugging in Xaraya
There are numerous types of bugs and ways the manifest themselves. Some examples,
- Logic errors in template -- not seeing the output you expected
- Block layout syntax error -- you get an error page with the template name and line number
- "white page of death" -- usually indicates a php syntax error
- Lots of missing content -- like the "white page" but you lose only one template
The white page can be caused by (just some examples I can think of):
- missing #, such as <p>#$body</p>
- missing closing parenthesis, such as <xar:set name="arts">xarModApiFunc('articles','user','get', array('aid'=>'14')</xar:set>
- invalid syntax within #'s, such as #$item['title'])# (the parenthesis causes white death)
Dumping Variables from a Template
print_r()
To dump a variable to the current page:
<pre><xar:set name="asdf">print_r($var)</xar:set></pre>
If things are really messing up, you can stop the output generation (die) immedately after the dump:
<pre><xar:set name="asdf">print_r($var); die()</xar:set></pre>
var_dump()
Like print_r() but also shows the value "types" (int, array, etc).
dump into comments
If you prefer to not mess up your page (especially on a live site), you can output the dump as an html comment. BL doesn't like us using xar:set with a value that looks like a comment :) so you have to work around with an embedded php hack
<xar:set name="asdf">1; $opencomment="<!-- "; $closecomment=" -->";</xar:set>
#$opencomment#
The value of $var is #$var#
#$closecomment#
Error Logs
var/logs/log.txt
When things look like they're not going well, be sure to check your Xaraya log file. By default, there is no logging. To start debug logging, simply touch (create 0 length file) at var/logs/log.txt . And make sure it is web-server writeable (e.g. permissions 774)
The file contains a trace log of everything you do when loading each page. Ignore most of it, just look for ERRORs rather than DEBUG statements.
To stop logging, delete the file.
php error log
You might want to turn on php error logging. That's set in the php.ini file.
log_errors = on
and something like
; Log errors to specified file.
error_log = php_error.log
If you dont specify a path, it will (should) write it into the current web root. You can also tell php to output it as html so you can view it in your browser, though I dont use that.
Cache
One source of frustration is when you think you've fixed something but its still broken.
Clear template cache
from command line:
rm -f var/cache/templates/*.php CACHEKEYS
or from the SiteTools module.
Clear browser cache
yup.
Clear session cookies
This can be helpful for example if there's session vars hanging around and you want to start fresh. The FireFox webdeveloper plugin can do this directly.
Debugging PHP
Even if you're not a module developer, you sometimes might need to trace through the php code to see what's going on or help track down a bug.
You can output directly to the current page using echo, print_r(), printf(), and var_dump() . Note, these will also cause a warning message on your page that you have output that has not gone through the normal buffering. Examples:
echo 'you have entered articles_userapi_getall';
printf( "the value of x is %s", $x );
print_r( $arr );
var_dump( $arr );
Output to log.txt
To send your debugging info to log.txt instead of the page:
xarLogMessage( $string );
xarLogVariable($name, $var );
for example,
xarLogMessage( 'entered articles_userapi_getall');
xarLogVariable('args', $args);
I suppose these functions can be called from a template too though I havent had occation to do that yet.
Using xdebug
Disclaimer, i've never tried they yet myself, but dropped these notes in here. One day I'll give it a try and update this section.
in php.ini:
extension=xdebug.so
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.trace_output_name=timestamp
in template:
<xar:set name="dummy1">1;xdebug_start_trace()</xar:set>
...
<xar:set name="dummy2">1;xdebug_stop_trace()</xar:set>




Better PHP error messages
Posted by: Hb on March 05, 2008 03:55 PM#