Last active
February 23, 2022 09:35
-
-
Save llagerlof/f61e0ff61de2a3a9fd4651f7eed27172 to your computer and use it in GitHub Desktop.
PHP function to generate a human readable call trace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* generateCallTrace() function | |
* | |
* A function for getting a nice and comprehensible call trace. | |
* It is probably more resource-intensive than some other alternatives but it is short, | |
* understandable, and gives nice output (Exception->getTraceAsString()). | |
* | |
* @package generateCallTrace | |
* @version 1.0 | |
* @author jurchiks101 at gmail dot com | |
* @copyright 2014 jurchiks101 at gmail dot com | |
* @link https://secure.php.net/manual/en/function.debug-backtrace.php#112238 | |
*/ | |
function generateCallTrace() | |
{ | |
$e = new Exception(); | |
$trace = explode("\n", $e->getTraceAsString()); | |
// reverse array to make steps line up chronologically | |
$trace = array_reverse($trace); | |
array_shift($trace); // remove {main} | |
array_pop($trace); // remove call to this method | |
$length = count($trace); | |
$result = array(); | |
for ($i = 0; $i < $length; $i++) | |
{ | |
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering | |
} | |
return "\t" . implode("\n\t", $result); | |
} | |
/* | |
Example output: | |
1) /var/www/test/test.php(15): SomeClass->__construct() | |
2) /var/www/test/SomeClass.class.php(36): SomeClass->callSomething() | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment