Debug My Debugger

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Debug My Debugger

Post 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?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You might find this thread useful.
Post Reply