debug_backtrace()
Nick Halstead beschreibt seinen Einsatz von debug_backtrace().
With that, we can easily get complete debug information from anywhere. The output will show the function or class/method in which the debug() statement was called, as well as the file and line number where it was called. We can also label each output independently if we want to, and hide it from users viewing the page so that we can debug a production site without taking it off line.
Seine Funktion sieht so aus:
function debug($msg, $label = 'DEBUG', $stealth = FALSE) {
if (defined('DEBUG') && DEBUG) {
if (is_bool($msg)) {
$msg = $msg ? 'TRUE' : 'FALSE';
}
$display = $stealth ? ' style="display: none;"' : '';
$backtrace = debug_backtrace();
$debug = array();
$stack = (isset($backtrace[1]['class']) ? "{$backtrace[1]['class']}::" : '')
. (isset($backtrace[1]['function']) ? "{$backtrace[1]['function']}" : '');
if ($stack) {
$debug[] = $stack;
}
$debug[] = "Line {$backtrace[0]['line']} of {$backtrace[0]['file']}";
$debug = implode('<br />', $debug);
print "<pre{$display}>{$label}: {$debug}:<br />".print_r($msg, 1)."
\n”;
}
}
