Is there a Pretty Print Stack Dump?

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

    Gonna add mine to the rest of the answers here.

    If you have bootstrap and jquery installed, it's even more useful and compact, but its not neccessary.

    function prettyPrintBackTrace() {
        $backtrace = "\n<b><u>Full Backtrace</u></b>\n<script>function toggleBackTraceTwirl(self) {\$('span', self).toggleClass('glyphicon-menu-up glyphicon-menu-down');}</script>";
        foreach (debug_backtrace() as $key => $trace) {
            if(($trace['function'] ==__FUNCTION__) || ($trace['function'] == "fail")) {continue;}
            $class = (array_key_exists('class', $trace) ? "class <u>({$trace['class']})</u>" : false);
            $exp = explode("/",$trace['file']);
            $exp[count($exp)-1] = "<b>" . end($exp) . "</b>";;
            $filename = implode("/",array_splice($exp, -4));    
            $backtrace .=  "/{$filename}:{$trace['line']}, ";
            if((isset($trace['args'])) && (is_array($trace['args']))) {
    
    
                if( (is_string($trace['args'][0])) && (substr($trace['args'][0],-4) == ".php") && (count($trace['args'] == 1)) ) {
                    // It was most likely a php include of some sort.
                    $exp = explode("/",$trace['args'][0]);
                    $filename = implode("/",array_splice($exp, -2));
                    $backtrace .= "function <i>{$trace['function']}(<b>{$filename}</b>)</i>\n";
                } else { 
                    // Finish the line and move on.
                    $backtrace .= "function <i>{$trace['function']}()</i>&nbsp;<a href='#' data-target='#backtraceparameters{$key}' onClick='toggleBackTraceTwirl(this)' data-toggle='collapse'><span class='glyphicon glyphicon-menu-down'></span></a>\n";
                    $backtrace .=  "<div id='backtraceparameters{$key}' class='collapse'>";
                    $args = array();
                    foreach($trace['args'] as $key => $val) {
                        if($val) $args[(!is_numeric($key) ? "key" : false)] = $val;
                    }
                    foreach($args as $count =>  $a) {
                        $backtrace .= ($count != (count($args) -1) ? "&boxvr;" : "&boxur;");
                        $value = $a;
                        if($a === true) $value = "<i>true</i>";
                        if($a === false) $value = "<i>f alse</i>";
                        $backtrace .= "&boxh; ".(!is_numeric($count) ? $count." " : false).var_export($value,1)."\n";
                    }
                    $backtrace .=  "</div>";
                }
            }
        }
        return $backtrace;
    }
    

    I hope that helps someone. I've tried to make it as compact as possible.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题