Is there a Pretty Print Stack Dump?

后端 未结 8 1866
半阙折子戏
半阙折子戏 2021-02-07 17:41

Let\'s face it, debug_backtrace() output is not very pretty. Did anyone code a wrapper?

And what\'s your favourite pretty var_dump() (which is

8条回答
  •  忘了有多久
    2021-02-07 18:22

    Here is my pretty print wrapper that is intended for non-browser output, ie error logs or the console:

    function stackTrace() {
        $stack = debug_backtrace();
        $output = '';
    
        $stackLen = count($stack);
        for ($i = 1; $i < $stackLen; $i++) {
            $entry = $stack[$i];
    
            $func = $entry['function'] . '(';
            $argsLen = count($entry['args']);
            for ($j = 0; $j < $argsLen; $j++) {
                $func .= $entry['args'][$j];
                if ($j < $argsLen - 1) $func .= ', ';
            }
            $func .= ')';
    
            $output .= $entry['file'] . ':' . $entry['line'] . ' - ' . $func . PHP_EOL;
        }
        return $output;
    }
    

提交回复
热议问题