PHP Print Call stack information

Source: Internet
Author: User
Tags php print
Wrong me in PHP, we use set_error_handler for processing, if an exception occurs, we use Set_exception_handler, but in debugging we can also use the

Debug_print_backtrace and Debug_backtrace Call stack information printing

Use of Set_error_handler

 My ERROR[$errno] $errstr
\ n "; echo "Fatal error on line $errline in file $errfile"; echo ", PHP". Php_version. " (" . Php_os. ")
\ n "; echo "Aborting ...
\ n "; Exit (1); Break Case E_user_warning:echo "My WARNING[$errno] $errstr
\ n "; Break Case E_user_notice:echo "My NOTICE[$errno] $errstr
\ n "; Break Default:echo "Unknown error type: [$errno] $errstr
\ n "; Break }/* Don ' t execute PHP Internal Error handler */return TRUE;} function to test the error handlingfunction Scale_by_log ($vect, $scale) {if (!is_numeric ($scale) | | $scale <= 0) {Trigger_error ("log (x) for x <= 0 are undefined, you used:scale = $scale", e_user_error); } if (!is_array ($vect)) {trigger_error ("Incorrect input vector, array of values expected", e_user_warning); return null; } $temp = Array (); foreach ($vect as $pos = + $value) {if (!is_numeric ($value)) {Trigger_error ("value at position $pos I s not a number, using 0 (zero) ", e_user_notice); $value = 0; } $temp [$pos] = log ($scale) * $value; } return $temp;} $old _error_handler = Set_error_handler ("MyErrorHandler");? >

Use of Set_exception_handler

 Gettrace (); foreach ($trace as $key + $stackPoint) {//Returns an exception similar to the exception description information $trace [$key] [' args '] = Array_map (' GetType ', $tra    ce[$key [' args ']);    }//Format exception information $result = Array ();            foreach ($trace as $key = + $stackPoint) {$result [] = sprintf ($traceline, $key, $stackPoint [' file '], $stackPoint [' line '], $stackPoint [' function '], implode (', ', $stack    point[' args ')); }//Trace always ends with {main} $result [] = ' # '. + + $key.    ' {main} '; Write tracelines into main template $msg = sprintf ($msg, Get_class ($exception), $exception-&gt ; GetMessage (), $exception->getfile (), $exception->getline (), implode ("\ n", $result), $ex    Ception->getfile (), $exception->getline ()); Error_log ($msg);}? >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.