I **LOVE** it!
The code below generates the following log file entry (contants starting with "G" are defined elsewhere:
2003.07.06 07:20:13 (Sunday): Error in function: badfunction [Line 10; File: /var/www/html/app_name/core/phpscratch/caller.php3]<-b<-a; Error ID: 99
Description: I blew up!
Code:
// ************************************************** **
function a() {
b();
}
// ************************************************** **
function b() {
badFunction();
}
// ************************************************** **
function badFunction() {
logError(99, "I blew up!");
print "LOG ENTRY ATTEMPT DONE<br>";
print GerrorLog . "<br>";
}
// ************************************************** **
function logError($errID = 0, $description = "") {
$backtrace = debug_backtrace();
$callPath = $backtrace[1]['function'];
$callPath .= ' [Line ' . $backtrace[1]['line'];
$callPath .= '; File: ' . $backtrace[1]['file'] . ']';
for ($n=2; $n<=20; $n++) {
if (isset($backtrace[$n]['function'])) {
$callPath .= '<-' . $backtrace[$n]['function'];
}
}
$logEntry = date("Y.m.d H:i:s (l)") . ": Error in function: " . $callPath;
if ( strlen($description) > 0 ) {
$logEntry .= "; Error ID: $errID Description: " . $description;
}
$logEntry .= "\n";
logWrite($logEntry);
}
// ************************************************** **
function logWrite($logEntry) {
if ($theFile = fopen(GerrorLog,"a")) {
$bytesWritten = fputs($theFile, $logEntry);
if ($bytesWritten <= 0) {
print ("**ERROR: WRITING TO [APP_NAME] ERROR LOG<BR>\n");
}
} else {
print ("**ERROR: WRITING TO [APP_NAME] ERROR LOG<BR>\n");
}
// Send a notification e-mail
$to=GdefaultEMail;
$headers = "From: ".GdefaultEMail."\n";
$subj = "[APP_NAME] System Error Notification\n";
$body = $logEntry;
mail($to,$subj,$body,$headers);
}
// ************************************************** **
a();
// ************************************************** **
On Sun, 06 Jul 2003 00:36:33 +0100, Andy Hassall <an**@andyh.co.uk>, wrote:
On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
<dr***********************@mindspring.com> wrote:
I'm sure I saw this somewhere but can't remember where and can't find it now...
Is there a PHP function or global variable that will return name of the calling function?
http://groups.google.co.uk/groups?hl...1fc2871&rnum=1
------------------------------------------------
Dan Hansen
------------------------------------------------