Is there a Pretty Print Stack Dump?

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

    Here's a "pretty print" var_dump

    function vdump() {
    
        $args = func_get_args();
    
        $backtrace = debug_backtrace();
        $code = file($backtrace[0]['file']);    
    
        echo "
    ";
    
        echo "".htmlspecialchars(trim($code[$backtrace[0]['line']-1]))."\n";
    
        echo "\n";
    
            ob_start();
    
                foreach ($args as $arg)
                    var_dump($arg);
    
                $str = ob_get_contents();
    
            ob_end_clean();
    
            $str = preg_replace('/=>(\s+)/', ' => ', $str);
            $str = preg_replace('/ => NULL/', ' → NULL', $str);
            $str = preg_replace('/}\n(\s+)\[/', "}\n\n".'$1[', $str);
            $str = preg_replace('/ (float|int)\((\-?[\d\.]+)\)/', " $1 $2", $str);
    
            $str = preg_replace('/array\((\d+)\) {\s+}\n/', "array•$1 []", $str);
            $str = preg_replace('/ string\((\d+)\) \"(.*)\"/', " str•$1 '$2'", $str);
            $str = preg_replace('/\[\"(.+)\"\] => /', "'$1' → ", $str);
            $str = preg_replace('/object\((\S+)\)#(\d+) \((\d+)\) {/', "obj•$2 $1[$3] {", $str);
            $str = str_replace("bool(false)", "bool•false", $str);
            $str = str_replace("bool(true)", "bool•true", $str);
    
            echo $str;
    
        echo "
    "; echo "
    "; echo "Sizes: "; foreach ($args as $k => $arg) { if ($k > 0) echo ","; echo count($arg); } echo "
    "; }

提交回复
热议问题