...

View Full Version : Error backtrace



chaosprime
08-20-2008, 05:22 PM
For all those like me who are sick to death of trying to debug using useless final-location-only error messages, this snippet makes PHP produce a debug backtrace on warnings and errors. It is suitable for installation as a backtrace.lib.php, which can then be used with no more than a require_once('backtrace.lib.php') added when you want it.



<?php
function process_error_backtrace($errno, $errstr, $errfile, $errline, $errcontext) {
if(!(error_reporting() & $errno))
return;
switch($errno) {
case E_WARNING :
case E_USER_WARNING :
case E_STRICT :
case E_NOTICE :
case E_USER_NOTICE :
$type = 'warning';
$fatal = false;
break;
default :
$type = 'fatal error';
$fatal = true;
break;
}
if(php_sapi_name() == 'cli') {
echo 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
foreach(array_reverse(debug_backtrace()) as $item)
echo ' ' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()' . "\n";
} else {
echo '<p class="error_backtrace">' . "\n";
echo ' Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
echo ' <ol>' . "\n";
foreach(array_reverse(debug_backtrace()) as $item)
echo ' <li>' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()</li>' . "\n";
echo ' </ol>' . "\n";
echo '</p>' . "\n";
}
if($fatal)
exit(1);
}

set_error_handler('process_error_backtrace');
?>

bauhsoj
02-03-2009, 07:52 PM
Thanks! This is exactly what I was needing. Those one line errors were driving me nuts! :thumbsup:

Do you have one of these that will work for error logs?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum