Is there a Pretty Print Stack Dump?

后端 未结 8 2005
半阙折子戏
半阙折子戏 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:05

    jhurliman's pretty print stackTrace method above is really great. But for me it was generating lots of PHP Warnings that were also cluttering up the log. I added a little more error and type checking which results in a very nice stack trace in the logs. Here is the modified version of jhurliman's code:

    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++) {
                $my_entry = $entry['args'][$j];
                if (is_string($my_entry)) {
                    $func .= $my_entry;
                }
                if ($j < $argsLen - 1) $func .= ', ';
            }
            $func .= ')';
    
            $entry_file = 'NO_FILE';
            if (array_key_exists('file', $entry)) {
                $entry_file = $entry['file'];               
            }
            $entry_line = 'NO_LINE';
            if (array_key_exists('line', $entry)) {
                $entry_line = $entry['line'];
            }           
            $output .= $entry_file . ':' . $entry_line . ' - ' . $func . PHP_EOL;
        }
        return $output;
    }
    

提交回复
热议问题