Skip to content

Instantly share code, notes, and snippets.

@michaelxor
Forked from dojohnso/simple_backtrace.php
Created February 26, 2014 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelxor/9238091 to your computer and use it in GitHub Desktop.
Save michaelxor/9238091 to your computer and use it in GitHub Desktop.
/**
* 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