Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Error backtrace

  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •