Page 1 of 1

Debug My Debugger

Posted: Wed Jun 15, 2005 4:02 am
by harrison
Hello, I am recently making my own debugging functions,including sending myself an email indicating the details of errors occured when user viewed my page.

Code: Select all

// the function looks like this 
function ReportError($msg){
    ini_set('sendmail_from','user@mail.com');
    mail('own@mail.com','Error Report!',$msg);
}
// and i call it this way in another script
funtion DoStuff(){
    // some code ...
    if($ErrorCode!=0){
        $msg = "An ".getErrorMsg($ErrorCode)." error occured while calling ".__FUNCTION__." of ".__FILE__;
        ReportError($msg)
    }
}
Notice that I can use the predefined constants, __FUNCTION__ and __FILE__, to point the current function and file where the error occured. However, if I will use this function repeatdly, I would also write the whole message format many times, including the predefined constants.
One solution I thought of is to get this message format inside ReportError() so that only the $ErrorCode will be passed.

Code: Select all

function ReportError($ErrorCode){
    $details = "An ".getErrorMsg($ErrorCode)." error occured while calling ".__FUNCTION__." of ".__FILE__;
    ini_set('sendmail_from','user@mail.com');
    mail('own@mail.com','Error Report!',$details);
}
But the problem is that the predefined constants will now point to ReportError() instead of DoStuff() function where the error occured.

Is there any way to tell ReportError() what function called him?

Posted: Wed Jun 15, 2005 4:33 am
by Syranide
__FUNCTION__ and __FILE__ and so on only points to where it is now, has nothing to do with errors etc.
what you might want to look at is debug_backtrace() (or something like that), which gives you the current callstack... meaning that you get the line of every call, parameter, function etc which was require to get to the "depth" you are at now, I'll bet you find it easy to understand.

EDIT: btw, if you've gone up to PHP5 you should really look at exceptions instead.

Posted: Wed Jun 15, 2005 9:56 am
by Weirdan
You might find this thread useful.