Last active
August 29, 2015 13:56
-
-
Save dojohnso/9193558 to your computer and use it in GitHub Desktop.
simple_backtrace() is an easier to read, configurable version of debug_backtrace(), making debugging your code more fun... or at least just easier. Read more about it here: [link coming soon]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* a simpler version of debug_backtrace() | |
* | |
* @param boolean $show_args Pass true to display argument values in the output | |
* @param boolean $expand_objects when $show_args == true, pass true to expand object values, default to false to avoid over-verbosity | |
* | |
*/ | |
function simple_backtrace( $show_args = false, $expand_objects = false ) | |
{ | |
$tracers = debug_backtrace(); | |
$new_trace = array(); | |
foreach ( $tracers AS $tracer ) | |
{ | |
$line = ''; | |
if ( isset( $tracer['class'] ) ) | |
{ | |
$line .= $tracer['class'] . $tracer['type']; | |
} | |
$args = array(); | |
if ( !empty( $tracer['args'] ) ) | |
{ | |
foreach ( $tracer['args'] AS $arg ) | |
{ | |
$args[] = gettype( $arg ); | |
} | |
} | |
$line .= $tracer['function'] . '(' . implode( ', ', $args ) . ') in '; | |
if ( !empty( $tracer['file'] ) ) | |
{ | |
$line .= $tracer['file']; | |
} | |
else | |
{ | |
$line .= 'Unknown'; | |
} | |
if ( !empty( $tracer['line'] ) ) | |
{ | |
$line .= ':' . $tracer['line']; | |
} | |
else | |
{ | |
$line .= ':Unknown'; | |
} | |
if ( $tracer['function'] == 'mysqlquery' ) | |
{ | |
$line .= "\n Trying the following sql:\n " . $tracer['args'][0]; | |
} | |
if ( $show_args ) | |
{ | |
$line .= "\n Argument values:"; | |
foreach ( $tracer['args'] AS $k => $arg ) | |
{ | |
$k = $k+1; | |
if ( gettype($arg) != 'object' || (gettype($arg) == 'object' && $expand_objects) ) | |
{ | |
$line .= "\n #$k (" . gettype($arg) . "): " . print_r($arg, true); | |
} | |
else | |
{ | |
$line .= "\n #$k (" . gettype($arg) . "): " . get_class( $arg ); | |
} | |
} | |
} | |
$new_trace[] = $line; | |
} | |
return $new_trace; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment