Forum Moderators: coopster

Message Too Old, No Replies

Detect calling function with debug_backtrace()

         

BlackDex

12:09 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Hello,

I want to use debug_backtrace() in the function called by register_shutdown_function. But it only displays the function we are currently in (the shutdown function).

Is there any way to trace what class, methode or line caused php to call the
shutdown function?

Thx in advanced

jezzer300

12:38 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



I do this...

During an error I display $error_backtrace

$error_backtrace = back_trace();

Use the below routine...

function back_trace() {

$output = ''; //"Trace @ ".sql_datetime()."<br>\n";
$backtrace = debug_backtrace();

$indent = 0;
foreach ($backtrace as $bt) {
$args = '';
if (is_array($bt['args'])) {
foreach ($bt['args'] as $a) {
if (!empty($args)) {
$args .= ', ';
}
switch (gettype($a)) {
case 'integer':
case 'double':
$args .= $a;
break;
case 'string':
$a = htmlspecialchars(substr($a, 0, 64)).((strlen($a) > 64)? '...' : '');
$args .= "\"$a\"";
break;
case 'array':
$args .= 'Array('.count($a).')';
break;
case 'object':
$args .= 'Object('.get_class($a).')';
break;
case 'resource':
$args .= 'Resource('.strstr($a, '#').')';
break;
case 'boolean':
$args .= $a? 'True' : 'False';
break;
case 'NULL':
$args .= 'Null';
break;
default:
$args .= 'Unknown';
}
}
}
if ((basename($bt['file'])!= 'inc_error_handler.php')) { // only trace this error handler if the error originated in here
$output .= str_repeat(">",$indent);
$indent++;
$output .= basename($bt['file'])." @ {$bt['line']}\t{$bt['class']}\t{$bt['type']}\t{$bt['function']}\t($args)\t\n";
}
}

return $output;
}

-----

The test for "inc_error_handler.php" stop me tracing all the way into my error handler.