public
Last active

Simple human readable stack trace for PHP scripts or Web

  • Download Gist
stack_trace.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/**
* Print out a stack trace from entry point to wherever this function was called.
* @param boolean $show_args Show arguments passed to functions? Default False.
* @param boolean $for_web Format text for web? Default True.
* @param boolean $return Return result instead of printing it? Default False.
*/
public static function stack_trace($show_args=false, $for_web=true, $return=false){
if ($for_web){
$before = '<b>';
$after = '</b>';
$tab = '&nbsp;&nbsp;&nbsp;&nbsp;';
$newline = '<br>';
}
else{
$before = '<';
$after = '>';
$tab = "\t";
$newline = "\n";
}
$output = '';
$ignore_functions = array('include','include_once','require','require_once');
$backtrace = debug_backtrace();
$length = count($backtrace);
 
for ($i=0; $i<$length; $i++){
$function = $line = '';
$skip_args = false;
$caller = @$backtrace[$i+1]['function'];
// Display caller function (if not a require or include)
if(isset($caller) && !in_array($caller, $ignore_functions)){
$function = ' in function '.$before.$caller.$after;
}
else{
$skip_args = true;
}
$line = $before.$backtrace[$i]['file'].$after.$function .' on line: '.$before.$backtrace[$i]['line'].$after.$newline;
if ($i < $length-1){
if ($show_args && $backtrace[($i+1)]['args'] && !$skip_args){
$params = ($for_web) ? htmlentities(print_r($backtrace[($i+1)]['args'], true))
: print_r($backtrace[($i+1)]['args'], true);
$line .= $tab.'Called with params: '.preg_replace('/(\n)/',$newline.$tab,trim($params)).$newline.$tab.'By:'.$newline;
unset($params);
}
else{
$line .= $tab.'Called By:'.$newline;
}
}
if ($return){
$output .= $line;
}
else{
echo $line;
}
}
if ($return){
return $output;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.