SysTools
<?php // dbgtrace.php
/*
Debug call trace
helps to understand the code workflow
especially useful in large projects
argument <f> is a bit flags mask:
1 - direct trace order (do not reverse)
2 - text trace output (do not HTML escape)
4 - return trace output (do not call die)
8 - don't send headers for output (when 4 not specified)
example (return trace as unescaped text):
$t = dbgtrace(4 | 2);
*/
if (!function_exists('dbgtrace')) {
function dbgtrace($f = 0) {
$f = max(intval($f), 0);
$l = array();
$t = debug_backtrace();
array_shift($t);
while (count($t)) {
extract(array_pop($t));
$l[] = sprintf(
'[%u] %s:%u: %s()',
count($l) + 1, basename($file), $line, $function
);
}
if (!($f & 1)) {
$l = array_reverse($l);
}
$l = implode(PHP_EOL, $l);
if (!($f & 2)) {
$l = nl2br(htmlspecialchars($l, ENT_COMPAT, 'cp1251'));
}
if (!($f & 4)) {
if ((!($f & 8)) && (!headers_sent())) {
header(
'Content-Type: '.
(($f & 2) ? 'text/plain' : 'text/html').
'; charset=windows-1251'
);
}
die($l);
}
return($l);
}
}
// example of usage
function last_func() { dbgtrace(); }
function deep_func() { last_func(); }
function call_func() { deep_func(); }
call_func();
/* output:
[3] dbgtrace.php:53: last_func()
[2] dbgtrace.php:55: deep_func()
[1] dbgtrace.php:57: call_func()
*/
2022.06.14