Page 1 of 1

logging in function calls, parameters and return values

Posted: Tue Jul 18, 2006 7:20 pm
by wtf
Greetings,

I was wondering if there's more intelligent way of logging in function calls as opposed to this

Code: Select all

function Hi ( $name )
{
  _log( __FUNCTION__, func_get_args());
  return 'Hi ' . $name; 
}
What I'm looking for is some way to set up a "listener" that will log function call every time some (any) function is called. Something equivalent to set_error_handler()???

Code: Select all

set_error_handler('onError');
  
  function onError( $errno, $errstr, $errfile, $errline, $vars )
  {
  	$error_msg = $errno . " : " . $errstr . "\n" . $errfile . "\n" .
        				 "LINE: . . . . . " . $errline . "\n" .	
        				 "FUNCTION: . . . " . __FUNCTION__ . "\n" . 
        				 "FILE: . . . . . " . $_SERVER['SCRIPT_NAME'] . "\n" .
        				 "QUERY:. . . . . " . $_SERVER['QUERY_STRING'] . "\n" .
        				 "SERVER: . . . . " . $_SERVER['SERVER_NAME'] . "\n" .
        				 "IP: . . . . . . " . $_SERVER['REMOTE_ADDR'] . "\n" .
        				 "BROWSER:. . . . " . $_SERVER['HTTP_USER_AGENT'] . "\n\n" . 
                 "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
  
  	error_log( $error_msg, 3, '/var/www/intranet/php.log' ); // log error to file
  }

Thx
~w

Posted: Tue Jul 18, 2006 7:28 pm
by Ollie Saunders
I was wondering if there's more intelligent way of logging in function calls as opposed to this
Nope.
What I'm looking for is some way to set up a "listener" that will log function call every time some (any) function is called. Something equivalent to set_error_handler()???
If your goal here is to get more information about the cause of errors. You may find Exceptions (PHP 5) are what you are after. If you throw an exception it takes with it a stacktrace which shows all the various calls that have lead it to that point. As it comes in PHP you get some pretty rubbish output, but if you go and download Pretty Blue Screen from sitepoint you get an excellent debugging system which I am making extensive use of currently.

Hope this helps.

Posted: Tue Jul 18, 2006 9:44 pm
by wtf
Hi ole,

Thanks for recomandation. We're on PHP 4.3 so exceptions are not usefull for me, set_error_handler was just an example. My goal is to log every function call regardless if an error had occured or not. What I want to see in my log file is every function that's been called, with the parameters passed to it and possibly functions return value. I was told PERL was capable of doing that so I'm hoping PHP is as well.

Thanks,

~w

Posted: Wed Jul 19, 2006 3:08 am
by Ollie Saunders
ok well you might want to have a look at:

Code: Select all

debug_backtrace()
but its really hacky.

Posted: Wed Jul 19, 2006 10:51 am
by wtf
Ole,

debug_backtrace would work as well, however what I'm trying to avoid is to hardcode a call to debug_backtrace or error_log within each one of my function calls.

Is it possible to "silently" call debag_backtrace or error_log or ... every time any function is executed without hardcoding it within each function body? For example set_error_handler("errorHandler") is called every time an error occurs regardless of where or who caused it. What I'm looking for is something along these lines. For example
set_any_function_handler("debug_backtrace");


Thanks for taking your time to answer my questions. I greatly appreciate it.


~w

Posted: Wed Jul 19, 2006 11:05 am
by onion2k
wtf wrote:I was told PERL was capable of doing that so I'm hoping PHP is as well.
I would kill for the ability to tie() variables in PHP. Sadly it's not possible.

Posted: Wed Jul 19, 2006 11:17 am
by Ollie Saunders
Is it possible to "silently" call debag_backtrace or error_log or ... every time any function is executed without hardcoding it within each function body?
I can say with total certainity that you cannot without some very very creative code that would preprocess your code and add in the necessary log calls to each function body.

I had another idea that you could call a_logged() when a() is defined catch the error in the callback for set_error_handler() and make the corrected call after having logged it but that doesn't work because calling an undefined function is a fatal error.

The only other suggestion I have for you it to modify PHP do to this, i imagine it wouldn't be difficult if you are familiar with the PHP source or you know someone who is. Of course that's a big if. The preprocessing is your best option.

Posted: Wed Jul 19, 2006 3:03 pm
by wtf
awww... that's a bummer

Thank you guys.


~w

Posted: Wed Jul 19, 2006 4:21 pm
by daedalus__
Does

Code: Select all

set_error_handler('FunctionName')
run all errors through that function???????????????

Posted: Wed Jul 19, 2006 4:47 pm
by Ollie Saunders
non fatal ones yes

Posted: Wed Jul 19, 2006 4:49 pm
by daedalus__
hawt.

I wrote a class and some neato functions to handle errors and I was trying to figure out an easy way to implement it.

I didn't think wrapping everything in conditional statements would be a great idea. lol

Posted: Thu Jul 20, 2006 12:52 pm
by wtf
I just came accross this one. Does anyone use it. It looks like it's capable of doing exactly what I'm looking for.

http://www.xdebug.org/screens.php

Posted: Fri Jul 21, 2006 9:48 am
by Ollie Saunders
cool man, glad you sorted that wtf.

Posted: Fri Jul 21, 2006 9:52 am
by onion2k
wtf wrote:I just came accross this one. Does anyone use it. It looks like it's capable of doing exactly what I'm looking for.

http://www.xdebug.org/screens.php
XDebug is awesome. But.. it's also quite complicated (understatement of the day). I actually find it more useful for profiling code than debugging stuff.

Posted: Fri Jul 21, 2006 9:59 am
by jmut
maybe you want to take a look at this.
I like it :)

viewtopic.php?t=51633