note 96418 modified in function.debug-backtrace by danbrown

From: Date: Fri, 09 Apr 2010 00:24:52 +0000
Subject: note 96418 modified in function.debug-backtrace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168049@lists.php.net to get a copy of this message
Everybody seems to have their favorite use. I substitute this function for die(). It gives a message to the user and emails me a PrettyPrint of what went wrong. $info is set by me, and it does a special check in the database object. <?php // var_format function var_format($v) // pretty-print var_export { return (str_replace(array("\n"," ","array"), array("<br>","&nbsp;","&nbsp;<i>array</i>"), var_export($v,true))."<br>"); } function myDie($info) { $mysqlerr=strpos($info,"ERROR=You have an error in your SQL syntax"); if($mysqlerr>0)$info=substr($info,0,$mysqlerr)." mySql format error"; $out="<br>MSG='$info'<br>".var_format($_REQUEST)."<br>"; $bt=debug_backtrace(); $sp=0; $trace=""; foreach($bt as $k=>$v) { extract($v); $file=substr($file,1+strrpos($file,"/")); if($file=="db.php")continue; // the db object $trace.=str_repeat("&nbsp;",++$sp); //spaces(++$sp); $trace.="file=$file, line=$line, function=$function<br>"; } $out.="<br>".backTrace(); if(substr($info,0,4)=="XXX ") // special errrors when db is inaccessible { $out=str_replace("<br>","\n",$out); $out=str_replace("&nbsp;"," ",$out); mail("me@somewhere.com","Database Execution Error for user ".$REMOTE_ADDR,"$out"); exit("Database Access Error. Please try again later."); } mail("me@somewhere.com",'Error Monitor','Execution Error',$out); exit("DANG! An execution error in the program has been sent to the webmaster. If you don't get an email from him soon, please call him."); } ?> This produces an output like this file=badmode.php, line=5, function=backTrace file=login.php, line=209, function=require file=midScreen.php, line=264, function=require file=masterindex.php, line=161, function=require file=production2.php, line=121, function=require file=index.php, line=16, function=require --was-- Everybody seems to have their favorite use. I substitute this function for die(). It gives a message to the user and emails me a PrettyPrint of what went wrong. $info is set by me, and it does a special check in the database object. <? // var_format function var_format($v) // pretty-print var_export { return (str_replace(array("\n"," ","array"), array("<br>","&nbsp;","&nbsp;<i>array</i>"), var_export($v,true))."<br>"); } function myDie($info) { $mysqlerr=strpos($info,"ERROR=You have an error in your SQL syntax"); if($mysqlerr>0)$info=substr($info,0,$mysqlerr)." mySql format error"; $out="<br>MSG='$info'<br>".var_format($_REQUEST)."<br>"; $bt=debug_backtrace(); $sp=0; $trace=""; foreach($bt as $k=>$v) { extract($v); $file=substr($file,1+strrpos($file,"/")); if($file=="db.php")continue; // the db object $trace.=str_repeat("&nbsp;",++$sp); //spaces(++$sp); $trace.="file=$file, line=$line, function=$function<br>"; } $out.="<br>".backTrace(); if(substr($info,0,4)=="XXX ") // special errrors when db is inaccessible { $out=str_replace("<br>","\n",$out); $out=str_replace("&nbsp;"," ",$out); mail("me@somewhere.com","Database Execution Error for user ".$REMOTE_ADDR,"$out"); exit("Database Access Error. Please try again later."); } mail("me@somewhere.com",'Error Monitor','Execution Error',$out); exit("DANG! An execution error in the program has been sent to the webmaster. If you don't get an email from him soon, please call him."); } ?> This produces an output like this file=badmode.php, line=5, function=backTrace file=login.php, line=209, function=require file=midScreen.php, line=264, function=require file=masterindex.php, line=161, function=require file=production2.php, line=121, function=require file=index.php, line=16, function=require http://php.net/manual/en/function.debug-backtrace.php

« previous php.notes (#168049) next »