From: Rob Richards Date: Fri, 12 Dec 2003 13:54:06 +0000 (+0000) Subject: consolidate error handling X-Git-Tag: php-5.0.0b3RC1~108 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4dd0dbfd791a650148c31194f96a0f0620243f64;p=php consolidate error handling --- diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index f9e6ded171..11a76a4294 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -43,6 +43,10 @@ #include "php_libxml.h" +#define PHP_LIBXML_ERROR 0 +#define PHP_LIBXML_CTX_ERROR 1 +#define PHP_LIBXML_CTX_WARNING 2 + /* a true global for initialization */ int _php_libxml_initialized = 0; @@ -294,22 +298,33 @@ int php_libxml_streams_IO_close(void *context) return php_stream_close((php_stream*)context); } -static void php_libxml_error_handler(void *ctx, const char *msg, ...) +static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg TSRMLS_DC) +{ + xmlParserCtxtPtr parser; + + parser = (xmlParserCtxtPtr) ctx; + + if (parser != NULL && parser->input != NULL) { + if (parser->input->filename) { + php_error_docref(NULL TSRMLS_CC, level, "%s in %s, line: %d", msg, parser->input->filename, parser->input->line); + } else { + php_error_docref(NULL TSRMLS_CC, level, "%s in Entity, line: %d", msg, parser->input->line); + } + } +} + +static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap) { - va_list ap; char *buf; int len, len_iter, output = 0; TSRMLS_FETCH(); - va_start(ap, msg); - len = vspprintf(&buf, 0, msg, ap); - va_end(ap); - + len = vspprintf(&buf, 0, *msg, ap); len_iter = len; /* remove any trailing \n */ - while (len && buf[--len_iter] == '\n') { + while (len_iter && buf[--len_iter] == '\n') { buf[len_iter] = '\0'; output = 1; } @@ -319,12 +334,46 @@ static void php_libxml_error_handler(void *ctx, const char *msg, ...) efree(buf); if (output == 1) { - php_error(E_WARNING, "%s", (char *) LIBXML(error_buffer)); + switch (error_type) { + case PHP_LIBXML_CTX_ERROR: + php_libxml_ctx_error_level(E_WARNING, ctx, (char *) LIBXML(error_buffer) TSRMLS_CC); + break; + case PHP_LIBXML_CTX_WARNING: + php_libxml_ctx_error_level(E_NOTICE, ctx, (char *) LIBXML(error_buffer) TSRMLS_CC); + break; + default: + php_error(E_WARNING, "%s", (char *) LIBXML(error_buffer)); + } smart_str_free(&LIBXML(error_buffer)); LIBXML(error_buffer) = NULL; } } +void php_libxml_ctx_error(void *ctx, const char *msg, ...) +{ + va_list args; + va_start(args, msg); + php_libxml_internal_error_handler(PHP_LIBXML_CTX_ERROR, ctx, &msg, args); + va_end(args); +} + +void php_libxml_ctx_warning(void *ctx, const char *msg, ...) +{ + va_list args; + va_start(args, msg); + php_libxml_internal_error_handler(PHP_LIBXML_CTX_WARNING, ctx, &msg, args); + va_end(args); +} + +void php_libxml_error_handler(void *ctx, const char *msg, ...) +{ + va_list args; + va_start(args, msg); + php_libxml_internal_error_handler(PHP_LIBXML_ERROR, ctx, &msg, args); + va_end(args); +} + + PHP_LIBXML_API void php_libxml_initialize() { if (!_php_libxml_initialized) { /* we should be the only one's to ever init!! */ diff --git a/ext/libxml/php_libxml.h b/ext/libxml/php_libxml.h index 11a63609e9..8dc5407675 100644 --- a/ext/libxml/php_libxml.h +++ b/ext/libxml/php_libxml.h @@ -73,6 +73,9 @@ int php_libxml_decrement_doc_ref(php_libxml_node_object *object TSRMLS_DC); void php_libxml_node_free_resource(xmlNodePtr node TSRMLS_DC); /* When object dtor is called as node may still be referenced */ void php_libxml_node_decrement_resource(php_libxml_node_object *object TSRMLS_DC); +void php_libxml_error_handler(void *ctx, const char *msg, ...); +void php_libxml_ctx_warning(void *ctx, const char *msg, ...); +void php_libxml_ctx_error(void *ctx, const char *msg, ...); #endif /* HAVE_LIBXML */