PHP 4.0 Bug #4172: php_errormsg is good, but more information is needed

From: Date: Tue, 18 Apr 2000 08:07:12 +0000
Subject: PHP 4.0 Bug #4172: php_errormsg is good, but more information is needed
Groups: php.dev 
Request: Send a blank email to php-dev+get-17997@lists.php.net to get a copy of this message
From: dolecek@ics.muni.cz Operating system: NetBSD (1.4X kernel, 1.4.1 userland) PHP version: 4.0 Release Candidate 1 PHP Bug Type: Feature/Change Request Bug description: php_errormsg is good, but more information is needed php_errormsg is extremely valuable facility. It allows for transparent custom logging of errors. The facility doesn't supply all needed informations though - namely, it's impossible to find out where exactly the error occured and what was the type of error. This means this information has to be tracked in PHP code itself, which is both tedious and inflexible (think "general error handlers"). Passing __FILE__ and __LINE__ to error handler is surely possible, but a bit tedious and generally would produce slighly bogus line numbers. Here is a patch to add several other variables - namely php_error_lineno (int), php_error_filename (string) and php_error_type (string). php_errormsg has been renamed to php_error_msg, to be consistent with others (as this is fairly new feature, it's better to rename it now that having inconsistent interface). There is a bit annoying bit about current php_errmsg in that it's not initialized on startup, so unless php_error() is called, PHP gives an uninitialized warning if it's used. I'm not sure this needs to be addressed (I can send necessary patch upon request). I'd be inclined to explicitly initialize the php_error_* to empty strings/0 on startup. Of course, it would be cool if there would be a possibility to register a function as a custom error handler, which would be called if an error occurs - similarily to e.g. shutdownhooks. But this needs more though and testing, so I don't think such feature is adept for 4.0. XXX --- main.c.orig Sun Apr 16 10:24:15 2000 +++ main.c Tue Apr 18 09:41:43 2000 @@ -299,7 +299,8 @@ PHPAPI void php_error(int type, const char *format, ...) { va_list args; - char *error_filename = NULL; + const char *error_filename = NULL; + const char *error_type_str = NULL; uint error_lineno; char buffer[1024]; int size = 0; @@ -340,31 +341,29 @@ error_filename = "Unknown"; } - if (EG(error_reporting) & type || (type & E_CORE)) { - char *error_type_str; - - switch (type) { - case E_ERROR: - case E_CORE_ERROR: - case E_COMPILE_ERROR: - error_type_str = "Fatal error"; - break; - case E_WARNING: - case E_CORE_WARNING: - case E_COMPILE_WARNING: - error_type_str = "Warning"; - break; - case E_PARSE: - error_type_str = "Parse error"; - break; - case E_NOTICE: - error_type_str = "Warning"; - break; - default: - error_type_str = "Unknown error"; - break; - } + switch (type) { + case E_ERROR: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + error_type_str = "Fatal error"; + break; + case E_WARNING: + case E_CORE_WARNING: + case E_COMPILE_WARNING: + error_type_str = "Warning"; + break; + case E_PARSE: + error_type_str = "Parse error"; + break; + case E_NOTICE: + error_type_str = "Warning"; + break; + default: + error_type_str = "Unknown error"; + break; + } + if (EG(error_reporting) & type || (type & E_CORE)) { /* get include file name */ if (PG(log_errors) || PG(display_errors) || (!module_initialized)) { va_start(args, format); @@ -413,13 +412,48 @@ va_end(args); buffer[sizeof(buffer) - 1] = 0; + /* error_msg */ ALLOC_ZVAL(tmp); INIT_PZVAL(tmp); tmp->value.str.val = (char *) estrndup(buffer, size); tmp->value.str.len = size; tmp->type = IS_STRING; - zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(pval *), NULL); + zend_hash_update(EG(active_symbol_table), "php_error_msg", sizeof("php_error_msg"), (void **) & tmp, sizeof(pval *), NULL); + + /* error_type_str */ + size = strlen(error_type_str); + ALLOC_ZVAL(tmp); + INIT_PZVAL(tmp); + /* error_type_str is const, no need to copy it */ + tmp->value.str.val = (char *) error_type_str; + tmp->value.str.len = size; + tmp->type = IS_STRING; + + zend_hash_update(EG(active_symbol_table), "php_error_type", sizeof("php_error_type"), (void **) & tmp, sizeof(pval *), NULL); + + /* error_lineno */ + ALLOC_ZVAL(tmp); + INIT_PZVAL(tmp); + tmp->value.lval = error_lineno; + tmp->type = IS_LONG; + + zend_hash_update(EG(active_symbol_table), "php_error_lineno", sizeof("php_error_lineno"), (void **) & tmp, sizeof(pval *), NULL); + + /* error_filename */ + if (error_filename) + size = strlen(error_filename); + else { + size = 0; + error_filename = ""; + } + ALLOC_ZVAL(tmp); + INIT_PZVAL(tmp); + tmp->value.str.val = (char *) estrndup(error_filename, size); + tmp->value.str.len = size; + tmp->type = IS_STRING; + + zend_hash_update(EG(active_symbol_table), "php_error_filename", sizeof("php_error_filename"), (void **) & tmp, sizeof(pval *), NULL); } } XXXX missing a

« previous php.dev (#17997) next »