Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2008, 04:22 PM   PM User | #1
chaosprime
Regular Coder

 
Join Date: Apr 2008
Location: New Jersey
Posts: 116
Thanks: 0
Thanked 29 Times in 29 Posts
chaosprime is an unknown quantity at this point
Error backtrace

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 Code:
<?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');
?>
__________________
Chaos
Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes
chaosprime is offline   Reply With Quote
Old 02-03-2009, 06:52 PM   PM User | #2
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 470
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Thanks! This is exactly what I was needing. Those one line errors were driving me nuts!

Do you have one of these that will work for error logs?
bauhsoj is offline   Reply With Quote
Reply

Bookmarks

Tags
debug, debugging, error handling, errors, warnings

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:37 PM.


Advertisement
Log in to turn off these ads.