Software Developer

Return Human-Readable debug_backtrace With Line Number

Get a human-readable debug backtrace including the the file, the function which calls it, and its line number.

/**
 * Returns a human readable line by line backtrace
 * including the file, function which calls, and the line #.
 */
function isa_get_backtrace() {
    $o = '';
    $file = '';
    $func = '';
    $line = '';
    $trace = debug_backtrace();

    foreach ( $trace as $each ) {
    	if ( isset( $each['file'] ) )
    		$file = $each['file'];
    	if ( isset( $each['function'] ) )
    		$func = $each['function'];
    	if ( isset( $each['line'] ) )
    		$line = $each['line'];
	    $o .= $file . ": ";
	    $o .= ($func != '') ? $func . "(): " : "";
	    $o .= ($line != '') ? $line . "\n" : "\n";
    }
    return($o);
}

Usage:

The following will print it directly to your WordPress error log (/wp-content/debug.log).

error_log( isa_get_backtrace() );

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]