Attached is a small patch that allows for a custom error handler to be used instead of php_log_err. This is useful for custom logging of error types that can't be handled with a user-space error handler (such as E_ERROR, E_PARSE, etc.).

In order to use a custom error handler set error_log to so:/path/to/so in the php.ini file, where the shared object has a function called error_handler with the following prototype:

int error_handler(int type,
                  const char *error_filename,
                  const unsigned int error_lineno,
                  const char *format,
                  va_list args)

error_handler should return a non-zero integer if the error was successfully handled and zero otherwise.

We are using this patch to log errors to a MySQL database for our defect tracking system.

Regards,

Blake

--
Blake Matheny
3GUpload.com, Inc.
Director of Technology
317-472-4962 (W)
317-201-2840 (C)
[EMAIL PROTECTED]
Index: main/main.c
===================================================================
RCS file: /repository/php-src/main/main.c,v
retrieving revision 1.512.2.63
diff -u -r1.512.2.63 main.c
--- main/main.c 16 May 2005 08:55:31 -0000      1.512.2.63
+++ main/main.c 16 May 2005 18:54:31 -0000
@@ -645,6 +645,11 @@
 
                if (!module_initialized || PG(log_errors)) {
                        char *log_buffer;
+                       char *logger;
+                       DL_HANDLE dl_handle;
+                       struct stat statbuf;
+                       int error_handler_retval = 0;
+                       int (*error_handler)(int, const char *, const uint, 
const char *, va_list);
 
 #ifdef PHP_WIN32
                        if (type==E_CORE_ERROR || type==E_CORE_WARNING) {
@@ -652,7 +657,27 @@
                        }
 #endif
                        spprintf(&log_buffer, 0, "PHP %s:  %s in %s on line 
%d", error_type_str, buffer, error_filename, error_lineno);
-                       php_log_err(log_buffer TSRMLS_CC);
+                       if ( (logger = strstr(PG(error_log), "so:")) != NULL ) {
+                               logger = strchr(logger, ':');
+                               *logger++;
+                               if ( stat(logger, &statbuf) == 0 ) {
+                                       dl_handle = DL_LOAD(logger);
+                                       if ( dl_handle ) {
+                                               error_handler = 
DL_FETCH_SYMBOL(dl_handle, "error_handler");
+                                               if ( error_handler != NULL ) {
+                                                       error_handler_retval = 
(*error_handler)(type, error_filename, error_lineno, format, args);
+                                                       if ( 
error_handler_retval )
+                                                               
DL_UNLOAD(dl_handle);
+                                                       else
+                                                               
php_log_err(log_buffer TSRMLS_CC);
+                                               } else
+                                                       php_log_err(log_buffer 
TSRMLS_CC);
+                                       } else
+                                               php_log_err(log_buffer 
TSRMLS_CC);
+                               } else
+                                       php_log_err(log_buffer TSRMLS_CC);
+                       } else
+                               php_log_err(log_buffer TSRMLS_CC);
                        efree(log_buffer);
                }
                if (module_initialized && PG(display_errors)

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to