patched to redirrect all "out of context" error messages to
a reconfigurable routine. The changes are:
* xmlerror.h : added the export of an error context type (void *)
  an error handler type xmlGenericErrorFunc there is an interface
  xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler);
  to reset the error handling routine and its argument
  (by default it's equivalent to respectively fprintf and stderr.
* all the c files: all wild accesses to stderr or stdout within
  the library have been replaced to calls to the handler.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 88bcf6b..38d5aff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Wed Oct 25 21:31:10 CEST 2000 Daniel Veillard <Daniel.Veillard@w3.org>
+
+	* patched to redirrect all "out of context" error messages to
+	  a reconfigurable routine. The changes are:
+	* xmlerror.h : added the export of an error context type (void *)
+	  an error handler type xmlGenericErrorFunc there is an interface
+	  xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler);
+	  to reset the error handling routine and its argument
+	  (by default it's equivalent to respectively fprintf and stderr.
+        * all the c files: all wild accesses to stderr or stdout within
+	  the library have been replaced to calls to the handler.
+
 Wed Oct 25 15:27:19 CEST 2000 Daniel Veillard <Daniel.Veillard@w3.org>
 
 	* configure.in: release 2.2.6
diff --git a/HTMLparser.c b/HTMLparser.c
index 4ded544..5900116 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -70,7 +70,8 @@
         ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,		\
 	             ctxt->name##Max * sizeof(ctxt->name##Tab[0]));	\
         if (ctxt->name##Tab == NULL) {					\
-	    fprintf(stderr, "realloc failed !\n");			\
+	    xmlGenericError(xmlGenericErrorContext,			\
+		    		"realloc failed !\n");			\
 	    return(0);							\
 	}								\
     }									\
@@ -689,9 +690,9 @@
     int i;
 
 #ifdef DEBUG
-    fprintf(stderr,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
+    xmlGenericError(xmlGenericErrorContext,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
     for (i = 0;i < ctxt->nameNr;i++) 
-        fprintf(stderr,"%d : %s\n", i, ctxt->nameTab[i]);
+        xmlGenericError(xmlGenericErrorContext,"%d : %s\n", i, ctxt->nameTab[i]);
 #endif
 
     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
@@ -703,7 +704,7 @@
 	info = htmlTagLookup(ctxt->name);
 	if ((info == NULL) || (info->endTag == 1)) {
 #ifdef DEBUG
-	    fprintf(stderr,"htmlAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
+	    xmlGenericError(xmlGenericErrorContext,"htmlAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
 #endif
         } else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
@@ -717,7 +718,7 @@
 	oldname = htmlnamePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG
-	    fprintf(stderr,"htmlAutoCloseOnClose: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"htmlAutoCloseOnClose: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
 	}	
@@ -742,14 +743,14 @@
     while ((newtag != NULL) && (ctxt->name != NULL) && 
            (htmlCheckAutoClose(newtag, ctxt->name))) {
 #ifdef DEBUG
-	fprintf(stderr,"htmlAutoClose: %s closes %s\n", newtag, ctxt->name);
+	xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: %s closes %s\n", newtag, ctxt->name);
 #endif
 	if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
 	    ctxt->sax->endElement(ctxt->userData, ctxt->name);
 	oldname = htmlnamePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG
-	    fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
         }
@@ -764,14 +765,14 @@
 	    (xmlStrEqual(ctxt->name, BAD_CAST"body")) ||
 	    (xmlStrEqual(ctxt->name, BAD_CAST"html")))) {
 #ifdef DEBUG
-	fprintf(stderr,"htmlAutoClose: EOF closes %s\n", ctxt->name);
+	xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: EOF closes %s\n", ctxt->name);
 #endif
 	if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
 	    ctxt->sax->endElement(ctxt->userData, ctxt->name);
 	oldname = htmlnamePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG
-	    fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"htmlAutoClose: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
         }
@@ -846,7 +847,7 @@
 	return;
     if (ctxt->nameNr <= 0) {
 #ifdef DEBUG
-	fprintf(stderr,"Implied element html: pushed html\n");
+	xmlGenericError(xmlGenericErrorContext,"Implied element html: pushed html\n");
 #endif    
 	htmlnamePush(ctxt, xmlStrdup(BAD_CAST"html"));
 	if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
@@ -866,14 +867,14 @@
 	     * assumed !
 	     */
 #ifdef DEBUG
-	    fprintf(stderr,"Implied element head: pushed head\n");
+	    xmlGenericError(xmlGenericErrorContext,"Implied element head: pushed head\n");
 #endif    
 	    htmlnamePush(ctxt, xmlStrdup(BAD_CAST"head"));
 	    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
 		ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
 	} else {
 #ifdef DEBUG
-	    fprintf(stderr,"Implied element body: pushed body\n");
+	    xmlGenericError(xmlGenericErrorContext,"Implied element body: pushed body\n");
 #endif    
 	    htmlnamePush(ctxt, xmlStrdup(BAD_CAST"body"));
 	    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
@@ -912,7 +913,7 @@
     for (i = 0; htmlNoContentElements[i] != NULL; i++) {
 	if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
 #ifdef DEBUG
-	    fprintf(stderr,"Implied element paragraph\n");
+	    xmlGenericError(xmlGenericErrorContext,"Implied element paragraph\n");
 #endif    
 	    htmlAutoClose(ctxt, BAD_CAST"p");
 	    htmlCheckImplied(ctxt, BAD_CAST"p");
@@ -1279,7 +1280,7 @@
                     sizeof(html40EntitiesTable[0]));i++) {
         if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
 #ifdef DEBUG
-            fprintf(stderr,"Found entity %s\n", name);
+            xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", name);
 #endif
             return(&html40EntitiesTable[i]);
 	}
@@ -1310,13 +1311,14 @@
 	    if ((unsigned int) html40EntitiesTable[i].value > value)
 		break;
 #ifdef DEBUG
-	    fprintf(stderr,"Found entity %s\n", html40EntitiesTable[i].name);
+	    xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", html40EntitiesTable[i].name);
 #endif
             return(&html40EntitiesTable[i]);
 	}
 #ifdef DEBUG
 	if (lv > html40EntitiesTable[i].value) {
-	    fprintf(stderr, "html40EntitiesTable[] is not sorted (%d > %d)!\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "html40EntitiesTable[] is not sorted (%d > %d)!\n",
 		    lv, html40EntitiesTable[i].value);
 	}
 	lv = html40EntitiesTable[i].value;
@@ -1762,7 +1764,8 @@
      */
     cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewDoc : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDoc : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlDoc));
@@ -1881,7 +1884,7 @@
 	buf[len++] = CUR;
 	NEXT;
 	if (len >= HTML_MAX_NAMELEN) {
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	       "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
 	    while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
 		   (CUR == '.') || (CUR == '-') ||
@@ -2052,7 +2055,7 @@
 	buf[len++] = CUR;
 	NEXT;
 	if (len >= HTML_MAX_NAMELEN) {
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	       "htmlParseNmtoken: reached HTML_MAX_NAMELEN limit\n");
 	    while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
 		   (CUR == '.') || (CUR == '-') ||
@@ -2490,7 +2493,8 @@
     SKIP(4);
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	ctxt->instate = state;
 	return;
     }
@@ -2507,7 +2511,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		ctxt->instate = state;
 		return;
 	    }
@@ -2953,7 +2958,8 @@
 	        maxatts = 10;
 	        atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
 		if (atts == NULL) {
-		    fprintf(stderr, "malloc of %ld byte failed\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "malloc of %ld byte failed\n",
 			    maxatts * (long)sizeof(xmlChar *));
 		    if (name != NULL) xmlFree(name);
 		    return;
@@ -2963,7 +2969,8 @@
 	        atts = (const xmlChar **) xmlRealloc((void *) atts,
 			                             maxatts * sizeof(xmlChar *));
 		if (atts == NULL) {
-		    fprintf(stderr, "realloc of %ld byte failed\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "realloc of %ld byte failed\n",
 			    maxatts * (long)sizeof(xmlChar *));
 		    if (name != NULL) xmlFree(name);
 		    return;
@@ -3004,7 +3011,7 @@
      */
     htmlnamePush(ctxt, xmlStrdup(name));
 #ifdef DEBUG
-    fprintf(stderr,"Start of element %s: pushed %s\n", name, ctxt->name);
+    xmlGenericError(xmlGenericErrorContext,"Start of element %s: pushed %s\n", name, ctxt->name);
 #endif    
     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
         ctxt->sax->startElement(ctxt->userData, name, atts);
@@ -3090,7 +3097,7 @@
      */
     if (!xmlStrEqual(name, ctxt->name)) {
 #ifdef DEBUG
-	fprintf(stderr,"End of tag %s: expecting %s\n", name, ctxt->name);
+	xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", name, ctxt->name);
 #endif
         if ((ctxt->name != NULL) && 
 	    (!xmlStrEqual(ctxt->name, name))) {
@@ -3112,12 +3119,12 @@
 	oldname = htmlnamePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG
-	    fprintf(stderr,"End of tag %s: popping out %s\n", name, oldname);
+	    xmlGenericError(xmlGenericErrorContext,"End of tag %s: popping out %s\n", name, oldname);
 #endif
 	    xmlFree(oldname);
 #ifdef DEBUG
 	} else {
-	    fprintf(stderr,"End of tag %s: stack empty !!!\n", name);
+	    xmlGenericError(xmlGenericErrorContext,"End of tag %s: stack empty !!!\n", name);
 #endif
 	}
     }
@@ -3349,11 +3356,14 @@
     name = ctxt->name;
 #ifdef DEBUG
     if (oldname == NULL)
-	fprintf(stderr, "Start of element %s\n", name);
+	xmlGenericError(xmlGenericErrorContext,
+		"Start of element %s\n", name);
     else if (name == NULL)	
-	fprintf(stderr, "Start of element failed, was %s\n", oldname);
+	xmlGenericError(xmlGenericErrorContext,
+		"Start of element failed, was %s\n", oldname);
     else	
-	fprintf(stderr, "Start of element %s, was %s\n", name, oldname);
+	xmlGenericError(xmlGenericErrorContext,
+		"Start of element %s, was %s\n", name, oldname);
 #endif
     if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
         (name == NULL)) {
@@ -3392,7 +3402,7 @@
 	    ctxt->sax->endElement(ctxt->userData, name);
 	oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-        fprintf(stderr,"End of tag the XML way: popping out %s\n", oldname);
+        xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n", oldname);
 #endif
 	if (oldname != NULL)
 	    xmlFree(oldname);
@@ -3415,7 +3425,7 @@
 	    nodePop(ctxt);
 	    oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-	    fprintf(stderr,"End of start tag problem: popping out %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"End of start tag problem: popping out %s\n", oldname);
 #endif
 	    if (oldname != NULL)
 		xmlFree(oldname);
@@ -3442,7 +3452,7 @@
 	    ctxt->sax->endElement(ctxt->userData, name);
 	oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-	fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
+	xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
 #endif
 	if (oldname != NULL)
 	    xmlFree(oldname);
@@ -3473,7 +3483,7 @@
 	nodePop(ctxt);
 	oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-	fprintf(stderr,"Premature end of tag %s : popping out %s\n", name, oldname);
+	xmlGenericError(xmlGenericErrorContext,"Premature end of tag %s : popping out %s\n", name, oldname);
 #endif
 	if (oldname != NULL)
 	    xmlFree(oldname);
@@ -3621,7 +3631,8 @@
 
     sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
     if (sax == NULL) {
-        fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlInitParserCtxt: out of memory\n");
     }
     else
         memset(sax, 0, sizeof(htmlSAXHandler));
@@ -3630,7 +3641,8 @@
     ctxt->inputTab = (htmlParserInputPtr *) 
                       xmlMalloc(5 * sizeof(htmlParserInputPtr));
     if (ctxt->inputTab == NULL) {
-        fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlInitParserCtxt: out of memory\n");
 	ctxt->inputNr = 0;
 	ctxt->inputMax = 0;
 	ctxt->input = NULL;
@@ -3647,7 +3659,8 @@
     /* Allocate the Node stack */
     ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
     if (ctxt->nodeTab == NULL) {
-        fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlInitParserCtxt: out of memory\n");
 	ctxt->nodeNr = 0;
 	ctxt->nodeMax = 0;
 	ctxt->node = NULL;
@@ -3663,7 +3676,8 @@
     /* Allocate the Name stack */
     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
     if (ctxt->nameTab == NULL) {
-        fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlInitParserCtxt: out of memory\n");
 	ctxt->nameNr = 0;
 	ctxt->nameMax = 10;
 	ctxt->name = NULL;
@@ -3805,13 +3819,16 @@
 	    ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
 	    if (next == 0)
-		fprintf(stderr, "HPP: lookup '%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: lookup '%c' found at %d\n",
 			first, base);
 	    else if (third == 0)
-		fprintf(stderr, "HPP: lookup '%c%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: lookup '%c%c' found at %d\n",
 			first, next, base);
 	    else 
-		fprintf(stderr, "HPP: lookup '%c%c%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: lookup '%c%c%c' found at %d\n",
 			first, next, third, base);
 #endif
 	    return(base - (in->cur - in->base));
@@ -3820,11 +3837,14 @@
     ctxt->checkIndex = base;
 #ifdef DEBUG_PUSH
     if (next == 0)
-	fprintf(stderr, "HPP: lookup '%c' failed\n", first);
+	xmlGenericError(xmlGenericErrorContext,
+		"HPP: lookup '%c' failed\n", first);
     else if (third == 0)
-	fprintf(stderr, "HPP: lookup '%c%c' failed\n", first, next);
+	xmlGenericError(xmlGenericErrorContext,
+		"HPP: lookup '%c%c' failed\n", first, next);
     else	
-	fprintf(stderr, "HPP: lookup '%c%c%c' failed\n", first, next, third);
+	xmlGenericError(xmlGenericErrorContext,
+		"HPP: lookup '%c%c%c' failed\n", first, next, third);
 #endif
     return(-1);
 }
@@ -3848,37 +3868,53 @@
 #ifdef DEBUG_PUSH
     switch (ctxt->instate) {
 	case XML_PARSER_EOF:
-	    fprintf(stderr, "HPP: try EOF\n"); break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try EOF\n"); break;
 	case XML_PARSER_START:
-	    fprintf(stderr, "HPP: try START\n"); break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try START\n"); break;
 	case XML_PARSER_MISC:
-	    fprintf(stderr, "HPP: try MISC\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try MISC\n");break;
 	case XML_PARSER_COMMENT:
-	    fprintf(stderr, "HPP: try COMMENT\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try COMMENT\n");break;
 	case XML_PARSER_PROLOG:
-	    fprintf(stderr, "HPP: try PROLOG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try PROLOG\n");break;
 	case XML_PARSER_START_TAG:
-	    fprintf(stderr, "HPP: try START_TAG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try START_TAG\n");break;
 	case XML_PARSER_CONTENT:
-	    fprintf(stderr, "HPP: try CONTENT\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try CONTENT\n");break;
 	case XML_PARSER_CDATA_SECTION:
-	    fprintf(stderr, "HPP: try CDATA_SECTION\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try CDATA_SECTION\n");break;
 	case XML_PARSER_END_TAG:
-	    fprintf(stderr, "HPP: try END_TAG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try END_TAG\n");break;
 	case XML_PARSER_ENTITY_DECL:
-	    fprintf(stderr, "HPP: try ENTITY_DECL\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try ENTITY_DECL\n");break;
 	case XML_PARSER_ENTITY_VALUE:
-	    fprintf(stderr, "HPP: try ENTITY_VALUE\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try ENTITY_VALUE\n");break;
 	case XML_PARSER_ATTRIBUTE_VALUE:
-	    fprintf(stderr, "HPP: try ATTRIBUTE_VALUE\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try ATTRIBUTE_VALUE\n");break;
 	case XML_PARSER_DTD:
-	    fprintf(stderr, "HPP: try DTD\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try DTD\n");break;
 	case XML_PARSER_EPILOG:
-	    fprintf(stderr, "HPP: try EPILOG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try EPILOG\n");break;
 	case XML_PARSER_PI:
-	    fprintf(stderr, "HPP: try PI\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try PI\n");break;
 	case XML_PARSER_SYSTEM_LITERAL:
-	    fprintf(stderr, "HPP: try SYSTEM_LITERAL\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "HPP: try SYSTEM_LITERAL\n");break;
     }
 #endif
 
@@ -3939,18 +3975,21 @@
 		        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: Parsing internal subset\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: Parsing internal subset\n");
 #endif
 		    htmlParseDocTypeDecl(ctxt);
 		    ctxt->instate = XML_PARSER_PROLOG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering PROLOG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering PROLOG\n");
 #endif
                 } else {
 		    ctxt->instate = XML_PARSER_MISC;
 		}
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering MISC\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering MISC\n");
 #endif
 		break;
             case XML_PARSER_MISC:
@@ -3969,7 +4008,8 @@
 		        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: Parsing Comment\n");
 #endif
 		    htmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_MISC;
@@ -3982,12 +4022,14 @@
 		        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: Parsing internal subset\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: Parsing internal subset\n");
 #endif
 		    htmlParseDocTypeDecl(ctxt);
 		    ctxt->instate = XML_PARSER_PROLOG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering PROLOG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering PROLOG\n");
 #endif
 		} else if ((cur == '<') && (next == '!') &&
 		           (avail < 9)) {
@@ -3995,7 +4037,8 @@
 		} else {
 		    ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering START_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering START_TAG\n");
 #endif
 		}
 		break;
@@ -4015,7 +4058,8 @@
 		        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: Parsing Comment\n");
 #endif
 		    htmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_PROLOG;
@@ -4025,7 +4069,8 @@
 		} else {
 		    ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering START_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering START_TAG\n");
 #endif
 		}
 		break;
@@ -4050,7 +4095,8 @@
 		        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: Parsing Comment\n");
 #endif
 		    htmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_EPILOG;
@@ -4065,7 +4111,8 @@
 		    ctxt->wellFormed = 0;
 		    ctxt->instate = XML_PARSER_EOF;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering EOF\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering EOF\n");
 #endif
 		    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
 			ctxt->sax->endDocument(ctxt->userData);
@@ -4083,7 +4130,8 @@
 	        if (cur != '<') {
 		    ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering CONTENT\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering CONTENT\n");
 #endif
 		    break;
 		}
@@ -4096,12 +4144,15 @@
 		name = ctxt->name;
 #ifdef DEBUG
 		if (oldname == NULL)
-		    fprintf(stderr, "Start of element %s\n", name);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Start of element %s\n", name);
 		else if (name == NULL)	
-		    fprintf(stderr, "Start of element failed, was %s\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Start of element failed, was %s\n",
 		            oldname);
 		else	
-		    fprintf(stderr, "Start of element %s, was %s\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Start of element %s, was %s\n",
 		            name, oldname);
 #endif
 		if (((depth == ctxt->nameNr) &&
@@ -4143,14 +4194,15 @@
 			ctxt->sax->endElement(ctxt->userData, name);
 		    oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-		    fprintf(stderr,"End of tag the XML way: popping out %s\n",
+		    xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n",
 		            oldname);
 #endif
 		    if (oldname != NULL)
 			xmlFree(oldname);
 		    ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering CONTENT\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering CONTENT\n");
 #endif
 		    break;
 		}
@@ -4171,7 +4223,7 @@
 			nodePop(ctxt);
 			oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-			fprintf(stderr,
+			xmlGenericError(xmlGenericErrorContext,
 			 "End of start tag problem: popping out %s\n", oldname);
 #endif
 			if (oldname != NULL)
@@ -4180,7 +4232,8 @@
 
 		    ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "HPP: entering CONTENT\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "HPP: entering CONTENT\n");
 #endif
 		    break;
 		}
@@ -4193,14 +4246,15 @@
 			ctxt->sax->endElement(ctxt->userData, name);
 		    oldname = htmlnamePop(ctxt);
 #ifdef DEBUG
-		    fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
+		    xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
 #endif
 		    if (oldname != NULL)
 			xmlFree(oldname);
 		}
 		ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
                 break;
 	    }
@@ -4258,7 +4312,8 @@
 			ctxt->instate = XML_PARSER_END_TAG;
 			ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: entering END_TAG\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: entering END_TAG\n");
 #endif
 			break;
 		    }
@@ -4285,7 +4340,8 @@
 			    (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			    goto done;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: Parsing Comment\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: Parsing Comment\n");
 #endif
 			htmlParseComment(ctxt);
 			ctxt->instate = XML_PARSER_CONTENT;
@@ -4295,14 +4351,16 @@
 			ctxt->instate = XML_PARSER_END_TAG;
 			ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: entering END_TAG\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: entering END_TAG\n");
 #endif
 			break;
 		    } else if (cur == '<') {
 			ctxt->instate = XML_PARSER_START_TAG;
 			ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: entering START_TAG\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: entering START_TAG\n");
 #endif
 			break;
 		    } else if (cur == '&') {
@@ -4310,7 +4368,8 @@
 			    (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
 			    goto done;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: Parsing Reference\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: Parsing Reference\n");
 #endif
 			/* TODO: check generation of subtrees if noent !!! */
 			htmlParseReference(ctxt);
@@ -4329,7 +4388,8 @@
 			}
 			ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "HPP: Parsing char data\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"HPP: Parsing char data\n");
 #endif
 			htmlParseCharData(ctxt, 0);
 		    }
@@ -4361,71 +4421,88 @@
 		}
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 	        break;
             case XML_PARSER_CDATA_SECTION:
-		fprintf(stderr, "HPP: internal error, state == CDATA\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == CDATA\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_DTD:
-		fprintf(stderr, "HPP: internal error, state == DTD\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == DTD\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_COMMENT:
-		fprintf(stderr, "HPP: internal error, state == COMMENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == COMMENT\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_PI:
-		fprintf(stderr, "HPP: internal error, state == PI\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == PI\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_ENTITY_DECL:
-		fprintf(stderr, "HPP: internal error, state == ENTITY_DECL\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == ENTITY_DECL\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_ENTITY_VALUE:
-		fprintf(stderr, "HPP: internal error, state == ENTITY_VALUE\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == ENTITY_VALUE\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering DTD\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering DTD\n");
 #endif
 		break;
             case XML_PARSER_ATTRIBUTE_VALUE:
-		fprintf(stderr, "HPP: internal error, state == ATTRIBUTE_VALUE\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == ATTRIBUTE_VALUE\n");
 		ctxt->instate = XML_PARSER_START_TAG;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering START_TAG\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering START_TAG\n");
 #endif
 		break;
 	    case XML_PARSER_SYSTEM_LITERAL:
-		fprintf(stderr, "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "HPP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"HPP: entering CONTENT\n");
 #endif
 		break;
 	}
@@ -4454,7 +4531,7 @@
 		    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
     }
 #ifdef DEBUG_PUSH
-    fprintf(stderr, "HPP: done %d\n", ret);
+    xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
 #endif
     return(ret);
 }
@@ -4495,7 +4572,7 @@
 	ctxt->input->base = ctxt->input->buf->buffer->content + base;
 	ctxt->input->cur = ctxt->input->base + cur;
 #ifdef DEBUG_PUSH
-	fprintf(stderr, "HPP: pushed %d\n", size);
+	xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
 #endif
 
 	if ((terminate) || (ctxt->input->buf->buffer->use > 80))
@@ -4602,7 +4679,7 @@
         (ctxt->input->buf != NULL))  {	      
 	xmlParserInputBufferPush(ctxt->input->buf, size, chunk);	      
 #ifdef DEBUG_PUSH
-	fprintf(stderr, "HPP: pushed %d\n", size);
+	xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
 #endif
     }
 
diff --git a/HTMLtree.c b/HTMLtree.c
index 5465357..0a259c5 100644
--- a/HTMLtree.c
+++ b/HTMLtree.c
@@ -31,6 +31,7 @@
 #include <libxml/HTMLtree.h>
 #include <libxml/entities.h>
 #include <libxml/valid.h>
+#include <libxml/xmlerror.h>
 
 /************************************************************************
  *									*
@@ -335,7 +336,8 @@
     xmlDtdPtr cur = doc->intSubset;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlDtdDump : no internal subset\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlDtdDump : no internal subset\n");
 	return;
     }
     xmlBufferWriteChar(buf, "<!DOCTYPE ");
@@ -367,7 +369,8 @@
     xmlChar *value;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlAttrDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlAttrDump : property == NULL\n");
 	return;
     }
     xmlBufferWriteChar(buf, " ");
@@ -395,7 +398,8 @@
 static void
 htmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
     if (cur == NULL) {
-        fprintf(stderr, "htmlAttrListDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlAttrListDump : property == NULL\n");
 	return;
     }
     while (cur != NULL) {
@@ -418,7 +422,8 @@
 static void
 htmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
     if (cur == NULL) {
-        fprintf(stderr, "htmlNodeListDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlNodeListDump : node == NULL\n");
 	return;
     }
     while (cur != NULL) {
@@ -440,7 +445,8 @@
     htmlElemDescPtr info;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlNodeDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlNodeDump : node == NULL\n");
 	return;
     }
     /*
@@ -624,7 +630,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "htmlxmlDocDumpMemory : document == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlxmlDocDumpMemory : document == NULL\n");
 #endif
 	*mem = NULL;
 	*size = 0;
@@ -665,7 +672,8 @@
     xmlDtdPtr cur = doc->intSubset;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlDtdDump : no internal subset\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlDtdDump : no internal subset\n");
 	return;
     }
     xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
@@ -697,7 +705,8 @@
     xmlChar *value;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlAttrDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlAttrDump : property == NULL\n");
 	return;
     }
     xmlOutputBufferWriteString(buf, " ");
@@ -725,7 +734,8 @@
 static void
 htmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) {
     if (cur == NULL) {
-        fprintf(stderr, "htmlAttrListDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlAttrListDump : property == NULL\n");
 	return;
     }
     while (cur != NULL) {
@@ -749,7 +759,8 @@
 static void
 htmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, const char *encoding) {
     if (cur == NULL) {
-        fprintf(stderr, "htmlNodeListDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlNodeListDump : node == NULL\n");
 	return;
     }
     while (cur != NULL) {
@@ -771,7 +782,8 @@
     htmlElemDescPtr info;
 
     if (cur == NULL) {
-        fprintf(stderr, "htmlNodeDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlNodeDump : node == NULL\n");
 	return;
     }
     /*
@@ -971,7 +983,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "htmlDocDump : document == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"htmlDocDump : document == NULL\n");
 #endif
 	return(-1);
     }
diff --git a/SAX.c b/SAX.c
index 7406105..168eb46 100644
--- a/SAX.c
+++ b/SAX.c
@@ -161,7 +161,8 @@
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlDtdPtr dtd;
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.internalSubset(%s, %s, %s)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.internalSubset(%s, %s, %s)\n",
             name, ExternalID, SystemID);
 #endif
 
@@ -194,7 +195,8 @@
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.externalSubset(%s, %s, %s)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.externalSubset(%s, %s, %s)\n",
             name, ExternalID, SystemID);
 #endif
     if (((ExternalID != NULL) || (SystemID != NULL)) &&
@@ -323,7 +325,8 @@
     URI = xmlBuildURI(systemId, (const xmlChar *) base);
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.resolveEntity(%s, %s)\n", publicId, systemId);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.resolveEntity(%s, %s)\n", publicId, systemId);
 #endif
 
     ret = xmlLoadExternalEntity((const char *) URI,
@@ -349,7 +352,8 @@
     xmlEntityPtr ret;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.getEntity(%s)\n", name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.getEntity(%s)\n", name);
 #endif
 
     ret = xmlGetDocEntity(ctxt->myDoc, name);
@@ -385,7 +389,8 @@
     xmlEntityPtr ret;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.getParameterEntity(%s)\n", name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.getParameterEntity(%s)\n", name);
 #endif
 
     ret = xmlGetParameterEntity(ctxt->myDoc, name);
@@ -412,7 +417,8 @@
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
             name, type, publicId, systemId, content);
 #endif
     if (ctxt->inSubset == 1) {
@@ -482,7 +488,8 @@
     xmlChar *name = NULL, *prefix = NULL;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
             elem, fullname, type, def, defaultValue);
 #endif
     name = xmlSplitQName(ctxt, fullname, &prefix);
@@ -528,7 +535,8 @@
     xmlElementPtr elem = NULL;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.elementDecl(%s, %d, ...)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.elementDecl(%s, %d, ...)\n",
             fullname, type);
 #endif
     
@@ -567,7 +575,8 @@
     xmlNotationPtr nota = NULL;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
 #endif
 
     if (ctxt->inSubset == 1)
@@ -606,7 +615,8 @@
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
             name, publicId, systemId, notationName);
 #endif
     if (ctxt->validate && ctxt->wellFormed &&
@@ -631,7 +641,8 @@
 {
     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.setDocumentLocator()\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.setDocumentLocator()\n");
 #endif
 }
 
@@ -648,14 +659,16 @@
     xmlDocPtr doc;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.startDocument()\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.startDocument()\n");
 #endif
     if (ctxt->html) {
 	if (ctxt->myDoc == NULL)
 #ifdef LIBXML_HTML_ENABLED
 	    ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
 #else
-        fprintf(stderr, "libxml2 built without HTML support\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"libxml2 built without HTML support\n");
 #endif
     } else {
 	doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
@@ -684,7 +697,8 @@
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.endDocument()\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.endDocument()\n");
 #endif
     if (ctxt->validate && ctxt->wellFormed &&
         ctxt->myDoc && ctxt->myDoc->intSubset)
@@ -731,7 +745,8 @@
 
 /****************
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.attribute(%s, %s)\n", fullname, value);
+    xmlGenericError(xmlGenericErrorContext,
+    "SAX.attribute(%s, %s)\n", fullname, value);
 #endif
  ****************/
     /*
@@ -897,7 +912,8 @@
     int i;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.startElement(%s)\n", fullname);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.startElement(%s)\n", fullname);
 #endif
 
     /*
@@ -932,7 +948,7 @@
     if (ret == NULL) return;
     if (ctxt->myDoc->children == NULL) {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "Setting %s as root\n", name);
+	xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
 #endif
         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
     } else if (parent == NULL) {
@@ -944,7 +960,7 @@
      * We are parsing a new node.
      */
 #ifdef DEBUG_SAX_TREE
-    fprintf(stderr, "pushing(%s)\n", name);
+    xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
 #endif
     nodePush(ctxt, ret);
 
@@ -954,12 +970,14 @@
     if (parent != NULL) {
         if (parent->type == XML_ELEMENT_NODE) {
 #ifdef DEBUG_SAX_TREE
-	    fprintf(stderr, "adding child %s to %s\n", name, parent->name);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "adding child %s to %s\n", name, parent->name);
 #endif
 	    xmlAddChild(parent, ret);
 	} else {
 #ifdef DEBUG_SAX_TREE
-	    fprintf(stderr, "adding sibling %s to ", name);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "adding sibling %s to ", name);
 	    xmlDebugDumpOneNode(stderr, parent, 0);
 #endif
 	    xmlAddSibling(parent, ret);
@@ -1059,9 +1077,9 @@
 
 #ifdef DEBUG_SAX
     if (name == NULL)
-        fprintf(stderr, "SAX.endElement(NULL)\n");
+        xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
     else
-	fprintf(stderr, "SAX.endElement(%s)\n", name);
+	xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
 #endif
     
     /* Capture end position and add node */
@@ -1083,7 +1101,7 @@
      * end of parsing of this node.
      */
 #ifdef DEBUG_SAX_TREE
-    fprintf(stderr, "popping(%s)\n", cur->name);
+    xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
 #endif
     nodePop(ctxt);
 }
@@ -1102,14 +1120,16 @@
     xmlNodePtr ret;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.reference(%s)\n", name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.reference(%s)\n", name);
 #endif
     if (name[0] == '#')
 	ret = xmlNewCharRef(ctxt->myDoc, name);
     else
 	ret = xmlNewReference(ctxt->myDoc, name);
 #ifdef DEBUG_SAX_TREE
-    fprintf(stderr, "add reference %s to %s \n", name, ctxt->node->name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "add reference %s to %s \n", name, ctxt->node->name);
 #endif
     xmlAddChild(ctxt->node, ret);
 }
@@ -1130,7 +1150,8 @@
     xmlNodePtr lastChild;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.characters(%.30s, %d)\n", ch, len);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.characters(%.30s, %d)\n", ch, len);
 #endif
     /*
      * Handle the data if any. If there is no child
@@ -1140,13 +1161,15 @@
 
     if (ctxt->node == NULL) {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "add chars: ctxt->node == NULL !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"add chars: ctxt->node == NULL !\n");
 #endif
         return;
     }
     lastChild = xmlGetLastChild(ctxt->node);
 #ifdef DEBUG_SAX_TREE
-    fprintf(stderr, "add chars to %s \n", ctxt->node->name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "add chars to %s \n", ctxt->node->name);
 #endif
 
     /*
@@ -1222,7 +1245,8 @@
 {
     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
 #endif
 }
 
@@ -1243,7 +1267,8 @@
     xmlNodePtr parent = ctxt->node;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.processingInstruction(%s, %s)\n", target, data);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.processingInstruction(%s, %s)\n", target, data);
 #endif
 
     ret = xmlNewPI(target, data);
@@ -1259,19 +1284,22 @@
     }
     if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
 #ifdef DEBUG_SAX_TREE
-	    fprintf(stderr, "Setting PI %s as root\n", target);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Setting PI %s as root\n", target);
 #endif
         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
 	return;
     }
     if (parent->type == XML_ELEMENT_NODE) {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "adding PI %s child to %s\n", target, parent->name);
+	xmlGenericError(xmlGenericErrorContext,
+		"adding PI %s child to %s\n", target, parent->name);
 #endif
 	xmlAddChild(parent, ret);
     } else {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "adding PI %s sibling to ", target);
+	xmlGenericError(xmlGenericErrorContext,
+		"adding PI %s sibling to ", target);
 	xmlDebugDumpOneNode(stderr, parent, 0);
 #endif
 	xmlAddSibling(parent, ret);
@@ -1291,7 +1319,8 @@
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.globalNamespace(%s, %s)\n", href, prefix);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.globalNamespace(%s, %s)\n", href, prefix);
 #endif
     xmlNewGlobalNs(ctxt->myDoc, href, prefix);
 }
@@ -1312,7 +1341,7 @@
     xmlNodePtr parent;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.setNamespace(%s)\n", name);
+    xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
 #endif
     ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
     if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
@@ -1341,7 +1370,7 @@
     xmlNsPtr ret;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.getNamespace()\n");
+    xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
 #endif
     ret = ctxt->node->ns;
     return(ret);
@@ -1365,7 +1394,8 @@
     xmlNodePtr cur = ctxt->node;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.checkNamespace(%s)\n", namespace);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.checkNamespace(%s)\n", namespace);
 #endif
 
     /*
@@ -1412,9 +1442,11 @@
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     if (prefix == NULL)
-	fprintf(stderr, "SAX.namespaceDecl(%s, NULL)\n", href);
+	xmlGenericError(xmlGenericErrorContext,
+		"SAX.namespaceDecl(%s, NULL)\n", href);
     else
-	fprintf(stderr, "SAX.namespaceDecl(%s, %s)\n", href, prefix);
+	xmlGenericError(xmlGenericErrorContext,
+		"SAX.namespaceDecl(%s, %s)\n", href, prefix);
 #endif
     xmlNewNs(ctxt->node, href, prefix);
 }
@@ -1434,7 +1466,7 @@
     xmlNodePtr parent = ctxt->node;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.comment(%s)\n", value);
+    xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
 #endif
     ret = xmlNewDocComment(ctxt->myDoc, value);
     if (ret == NULL) return;
@@ -1448,19 +1480,22 @@
     }
     if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
 #ifdef DEBUG_SAX_TREE
-	    fprintf(stderr, "Setting comment as root\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Setting comment as root\n");
 #endif
         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
 	return;
     }
     if (parent->type == XML_ELEMENT_NODE) {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "adding comment child to %s\n", parent->name);
+	xmlGenericError(xmlGenericErrorContext,
+		"adding comment child to %s\n", parent->name);
 #endif
 	xmlAddChild(parent, ret);
     } else {
 #ifdef DEBUG_SAX_TREE
-	fprintf(stderr, "adding comment sibling to ");
+	xmlGenericError(xmlGenericErrorContext,
+		"adding comment sibling to ");
 	xmlDebugDumpOneNode(stderr, parent, 0);
 #endif
 	xmlAddSibling(parent, ret);
@@ -1482,11 +1517,13 @@
     xmlNodePtr ret, lastChild;
 
 #ifdef DEBUG_SAX
-    fprintf(stderr, "SAX.pcdata(%.10s, %d)\n", value, len);
+    xmlGenericError(xmlGenericErrorContext,
+	    "SAX.pcdata(%.10s, %d)\n", value, len);
 #endif
     lastChild = xmlGetLastChild(ctxt->node);
 #ifdef DEBUG_SAX_TREE
-    fprintf(stderr, "add chars to %s \n", ctxt->node->name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "add chars to %s \n", ctxt->node->name);
 #endif
     if ((lastChild != NULL) &&
         (lastChild->type == XML_CDATA_SECTION_NODE)) {
diff --git a/debugXML.c b/debugXML.c
index 22172ed..6873d71 100644
--- a/debugXML.c
+++ b/debugXML.c
@@ -31,6 +31,7 @@
 #include <libxml/debugXML.h>
 #include <libxml/HTMLtree.h>
 #include <libxml/HTMLparser.h>
+#include <libxml/xmlerror.h>
 
 #define IS_BLANK(c)							\
   (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' '))
@@ -1203,31 +1204,36 @@
     if (node == NULL)
         return(-1);
     if ((filename == NULL) || (filename[0] == 0)) {
-        fprintf(stderr, "Write command requires a filename argument\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"Write command requires a filename argument\n");
 	return(-1);
     }
 #ifdef W_OK
     if (access((char *) filename, W_OK)) {
-        fprintf(stderr, "Cannot write to %s\n", filename);
+        xmlGenericError(xmlGenericErrorContext,
+		"Cannot write to %s\n", filename);
 	return(-1);
     }
 #endif    
     switch(node->type) {
         case XML_DOCUMENT_NODE:
 	    if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
-		fprintf(stderr, "Failed to write to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to write to %s\n", filename);
 		return(-1);
 	    }
 	    break;
         case XML_HTML_DOCUMENT_NODE:
 #ifdef LIBXML_HTML_ENABLED
 	    if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
-		fprintf(stderr, "Failed to write to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to write to %s\n", filename);
 		return(-1);
 	    }
 #else
 	    if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
-		fprintf(stderr, "Failed to write to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to write to %s\n", filename);
 		return(-1);
 	    }
 #endif /* LIBXML_HTML_ENABLED */
@@ -1237,7 +1243,8 @@
 
 	    f = fopen((char *) filename, "w");
 	    if (f == NULL) {
-		fprintf(stderr, "Failed to write to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to write to %s\n", filename);
 		return(-1);
 	    }
 	    xmlElemDump(f, ctxt->doc, node);
@@ -1268,29 +1275,33 @@
         filename = ctxt->filename;
 #ifdef W_OK
     if (access((char *) filename, W_OK)) {
-        fprintf(stderr, "Cannot save to %s\n", filename);
+        xmlGenericError(xmlGenericErrorContext,
+		"Cannot save to %s\n", filename);
 	return(-1);
     }
 #endif
     switch(ctxt->doc->type) {
         case XML_DOCUMENT_NODE:
 	    if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
-		fprintf(stderr, "Failed to save to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to save to %s\n", filename);
 	    }
 	    break;
         case XML_HTML_DOCUMENT_NODE:
 #ifdef LIBXML_HTML_ENABLED
 	    if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
-		fprintf(stderr, "Failed to save to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to save to %s\n", filename);
 	    }
 #else
 	    if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
-		fprintf(stderr, "Failed to save to %s\n", filename);
+		xmlGenericError(xmlGenericErrorContext,
+			"Failed to save to %s\n", filename);
 	    }
 #endif /* LIBXML_HTML_ENABLED */
 	    break;
 	default:
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	      "To save to subparts of a document use the 'write' command\n");
 	    return(-1);
 	    
@@ -1653,7 +1664,8 @@
 		if (list != NULL) {
 		    switch (list->type) {
 			case XPATH_UNDEFINED:
-			    fprintf(stderr, "%s: no such node\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s: no such node\n", arg);
 			    break;
 			case XPATH_NODESET: {
 			    int i;
@@ -1669,30 +1681,38 @@
 			    break;
 			}
 			case XPATH_BOOLEAN:
-			    fprintf(stderr, "%s is a Boolean\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a Boolean\n", arg);
 			    break;
 			case XPATH_NUMBER:
-			    fprintf(stderr, "%s is a number\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a number\n", arg);
 			    break;
 			case XPATH_STRING:
-			    fprintf(stderr, "%s is a string\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a string\n", arg);
 			    break;
 			case XPATH_POINT:
-			    fprintf(stderr, "%s is a point\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a point\n", arg);
 			    break;
 			case XPATH_RANGE:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_LOCATIONSET:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_USERS:
-			    fprintf(stderr, "%s is user-defined\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is user-defined\n", arg);
 			    break;
 		    }
 		    xmlXPathFreeNodeSetList(list);
 		} else {
-		    fprintf(stderr, "%s: no such node\n", arg);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "%s: no such node\n", arg);
 		}
 		ctxt->pctxt->node = NULL;
 	    }
@@ -1709,40 +1729,50 @@
 		if (list != NULL) {
 		    switch (list->type) {
 			case XPATH_UNDEFINED:
-			    fprintf(stderr, "%s: no such node\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s: no such node\n", arg);
 			    break;
 			case XPATH_NODESET:
 			    if (list->nodesetval->nodeNr == 1) {
 				ctxt->node = list->nodesetval->nodeTab[0];
 			    } else 
-				fprintf(stderr, "%s is a %d Node Set\n",
+				xmlGenericError(xmlGenericErrorContext,
+					"%s is a %d Node Set\n",
 				        arg, list->nodesetval->nodeNr);
 			    break;
 			case XPATH_BOOLEAN:
-			    fprintf(stderr, "%s is a Boolean\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a Boolean\n", arg);
 			    break;
 			case XPATH_NUMBER:
-			    fprintf(stderr, "%s is a number\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a number\n", arg);
 			    break;
 			case XPATH_STRING:
-			    fprintf(stderr, "%s is a string\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a string\n", arg);
 			    break;
 			case XPATH_POINT:
-			    fprintf(stderr, "%s is a point\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a point\n", arg);
 			    break;
 			case XPATH_RANGE:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_LOCATIONSET:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_USERS:
-			    fprintf(stderr, "%s is user-defined\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is user-defined\n", arg);
 			    break;
 		    }
 		    xmlXPathFreeNodeSetList(list);
 		} else {
-		    fprintf(stderr, "%s: no such node\n", arg);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "%s: no such node\n", arg);
 		}
 		ctxt->pctxt->node = NULL;
 	    }
@@ -1760,7 +1790,8 @@
 		if (list != NULL) {
 		    switch (list->type) {
 			case XPATH_UNDEFINED:
-			    fprintf(stderr, "%s: no such node\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s: no such node\n", arg);
 			    break;
 			case XPATH_NODESET: {
 			    int i;
@@ -1773,35 +1804,44 @@
 			    break;
 			}
 			case XPATH_BOOLEAN:
-			    fprintf(stderr, "%s is a Boolean\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a Boolean\n", arg);
 			    break;
 			case XPATH_NUMBER:
-			    fprintf(stderr, "%s is a number\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a number\n", arg);
 			    break;
 			case XPATH_STRING:
-			    fprintf(stderr, "%s is a string\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a string\n", arg);
 			    break;
 			case XPATH_POINT:
-			    fprintf(stderr, "%s is a point\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a point\n", arg);
 			    break;
 			case XPATH_RANGE:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_LOCATIONSET:
-			    fprintf(stderr, "%s is a range\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is a range\n", arg);
 			    break;
 			case XPATH_USERS:
-			    fprintf(stderr, "%s is user-defined\n", arg);
+			    xmlGenericError(xmlGenericErrorContext,
+				    "%s is user-defined\n", arg);
 			    break;
 		    }
 		    xmlXPathFreeNodeSetList(list);
 		} else {
-		    fprintf(stderr, "%s: no such node\n", arg);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "%s: no such node\n", arg);
 		}
 		ctxt->pctxt->node = NULL;
 	    }
 	} else {
-	    fprintf(stderr, "Unknown command %s\n", command);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Unknown command %s\n", command);
 	}
 	free(cmdline); /* not xmlFree here ! */
     }
diff --git a/encoding.c b/encoding.c
index 9981890..d60b845 100644
--- a/encoding.c
+++ b/encoding.c
@@ -46,6 +46,7 @@
 #ifdef LIBXML_HTML_ENABLED
 #include <libxml/HTMLparser.h>
 #endif
+#include <libxml/xmlerror.h>
 
 xmlCharEncodingHandlerPtr xmlUTF16LEHandler = NULL;
 xmlCharEncodingHandlerPtr xmlUTF16BEHandler = NULL;
@@ -585,7 +586,8 @@
 	    *outlen = 2;
 	    *inlen = 0;
 #ifdef DEBUG_ENCODING
-            fprintf(stderr, "Added FFFE Byte Order Mark\n");
+            xmlGenericError(xmlGenericErrorContext,
+		    "Added FFFE Byte Order Mark\n");
 #endif
 	    return(2);
 	}
@@ -795,7 +797,8 @@
 	    *outlen = 2;
 	    *inlen = 0;
 #ifdef DEBUG_ENCODING
-            fprintf(stderr, "Added FEFF Byte Order Mark\n");
+            xmlGenericError(xmlGenericErrorContext,
+		    "Added FEFF Byte Order Mark\n");
 #endif
 	    return(2);
 	}
@@ -1146,7 +1149,7 @@
     if (!strcmp(upper, "EUC-JP")) return(XML_CHAR_ENCODING_EUC_JP);
 
 #ifdef DEBUG_ENCODING
-    fprintf(stderr, "Unknown encoding %s\n", name);
+    xmlGenericError(xmlGenericErrorContext, "Unknown encoding %s\n", name);
 #endif
     return(XML_CHAR_ENCODING_ERROR);
 }
@@ -1265,7 +1268,8 @@
      * Keep only the uppercase version of the encoding.
      */
     if (name == NULL) {
-        fprintf(stderr, "xmlNewCharEncodingHandler : no name !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewCharEncodingHandler : no name !\n");
 	return(NULL);
     }
     for (i = 0;i < 499;i++) {
@@ -1275,7 +1279,8 @@
     upper[i] = 0;
     up = xmlMemStrdup(upper);
     if (up == NULL) {
-        fprintf(stderr, "xmlNewCharEncodingHandler : out of memory !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewCharEncodingHandler : out of memory !\n");
 	return(NULL);
     }
 
@@ -1285,7 +1290,8 @@
     handler = (xmlCharEncodingHandlerPtr)
               xmlMalloc(sizeof(xmlCharEncodingHandler));
     if (handler == NULL) {
-        fprintf(stderr, "xmlNewCharEncodingHandler : out of memory !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewCharEncodingHandler : out of memory !\n");
 	return(NULL);
     }
     handler->input = input;
@@ -1302,7 +1308,8 @@
      */
     xmlRegisterCharEncodingHandler(handler);
 #ifdef DEBUG_ENCODING
-    fprintf(stderr, "Registered encoding handler for %s\n", name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "Registered encoding handler for %s\n", name);
 #endif
     return(handler);
 }
@@ -1327,10 +1334,12 @@
 
     if (*ptr == 0x12) xmlLittleEndian = 0;
     else if (*ptr == 0x34) xmlLittleEndian = 1;
-    else fprintf(stderr, "Odd problem at endianness detection\n");
+    else xmlGenericError(xmlGenericErrorContext,
+	    "Odd problem at endianness detection\n");
 
     if (handlers == NULL) {
-        fprintf(stderr, "xmlInitCharEncodingHandlers : out of memory !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitCharEncodingHandlers : out of memory !\n");
 	return;
     }
     xmlNewCharEncodingHandler("UTF-8", NULL, NULL);
@@ -1381,14 +1390,16 @@
 xmlRegisterCharEncodingHandler(xmlCharEncodingHandlerPtr handler) {
     if (handlers == NULL) xmlInitCharEncodingHandlers();
     if (handler == NULL) {
-        fprintf(stderr, "xmlRegisterCharEncodingHandler: NULL handler !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlRegisterCharEncodingHandler: NULL handler !\n");
 	return;
     }
 
     if (nbCharEncodingHandler >= MAX_ENCODING_HANDLERS) {
-        fprintf(stderr, 
+        xmlGenericError(xmlGenericErrorContext, 
 	"xmlRegisterCharEncodingHandler: Too many handler registered\n");
-        fprintf(stderr, "\tincrease MAX_ENCODING_HANDLERS : %s\n", __FILE__);
+        xmlGenericError(xmlGenericErrorContext,
+		"\tincrease MAX_ENCODING_HANDLERS : %s\n", __FILE__);
 	return;
     }
     handlers[nbCharEncodingHandler++] = handler;
@@ -1518,7 +1529,8 @@
     }
     
 #ifdef DEBUG_ENCODING
-    fprintf(stderr, "No handler found for encoding %d\n", enc);
+    xmlGenericError(xmlGenericErrorContext,
+	    "No handler found for encoding %d\n", enc);
 #endif
     return(NULL);
 }
@@ -1567,7 +1579,8 @@
     for (i = 0;i < nbCharEncodingHandler; i++)
         if (!strcmp(upper, handlers[i]->name)) {
 #ifdef DEBUG_ENCODING
-            fprintf(stderr, "Found registered handler for encoding %s\n", name);
+            xmlGenericError(xmlGenericErrorContext,
+		    "Found registered handler for encoding %s\n", name);
 #endif
 	    return(handlers[i]);
 	}
@@ -1590,16 +1603,19 @@
 	    enc->iconv_in = icv_in;
 	    enc->iconv_out = icv_out;
 #ifdef DEBUG_ENCODING
-            fprintf(stderr, "Found iconv handler for encoding %s\n", name);
+            xmlGenericError(xmlGenericErrorContext,
+		    "Found iconv handler for encoding %s\n", name);
 #endif
 	    return enc;
     } else if ((icv_in != (iconv_t) -1) || icv_out != (iconv_t) -1) {
-	    fprintf(stderr, "iconv : problems with filters for '%s'\n", name);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "iconv : problems with filters for '%s'\n", name);
     }
 #endif /* LIBXML_ICONV_ENABLED */
 
 #ifdef DEBUG_ENCODING
-    fprintf(stderr, "No handler found for encoding %s\n", name);
+    xmlGenericError(xmlGenericErrorContext,
+	    "No handler found for encoding %s\n", name);
 #endif
 
     /*
@@ -1739,22 +1755,24 @@
 #ifdef DEBUG_ENCODING
     switch (ret) {
         case 0:
-	    fprintf(stderr, "converted %d bytes to %d bytes of input\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "converted %d bytes to %d bytes of input\n",
 	            toconv, written);
 	    break;
         case -1:
-	    fprintf(stderr,"converted %d bytes to %d bytes of input, %d left\n",
+	    xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n",
 	            toconv, written, in->use);
 	    break;
         case -2:
-	    fprintf(stderr, "input conversion failed due to input error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "input conversion failed due to input error\n");
 	    break;
         case -3:
-	    fprintf(stderr,"converted %d bytes to %d bytes of input, %d left\n",
+	    xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n",
 	            toconv, written, in->use);
 	    break;
 	default:
-	    fprintf(stderr,"Unknown input conversion failed %d\n", ret);
+	    xmlGenericError(xmlGenericErrorContext,"Unknown input conversion failed %d\n", ret);
     }
 #endif
     /*
@@ -1817,21 +1835,24 @@
     switch (ret) {
 #ifdef DEBUG_ENCODING
         case 0:
-	    fprintf(stderr, "converted %d bytes to %d bytes of input\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "converted %d bytes to %d bytes of input\n",
 	            toconv, written);
 	    break;
         case -1:
-	    fprintf(stderr,"converted %d bytes to %d bytes of input, %d left\n",
+	    xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n",
 	            toconv, written, in->use);
 	    break;
         case -3:
-	    fprintf(stderr,"converted %d bytes to %d bytes of input, %d left\n",
+	    xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n",
 	            toconv, written, in->use);
 	    break;
 #endif
         case -2:
-	    fprintf(stderr, "input conversion failed due to input error\n");
-	    fprintf(stderr, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "input conversion failed due to input error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
 		    in->content[0], in->content[1],
 		    in->content[2], in->content[3]);
     }
@@ -1895,7 +1916,8 @@
 	}
 #endif /* LIBXML_ICONV_ENABLED */
 #ifdef DEBUG_ENCODING
-	fprintf(stderr, "initialized encoder\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"initialized encoder\n");
 #endif
         return(0);
     }
@@ -1928,7 +1950,8 @@
     }
 #endif /* LIBXML_ICONV_ENABLED */
     else {
-	fprintf(stderr, "xmlCharEncOutFunc: no output function !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCharEncOutFunc: no output function !\n");
 	return(-1);
     }
 
@@ -1940,14 +1963,16 @@
     switch (ret) {
 #ifdef DEBUG_ENCODING
         case 0:
-	    fprintf(stderr, "converted %d bytes to %d bytes of output\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "converted %d bytes to %d bytes of output\n",
 	            toconv, written);
 	    break;
         case -1:
-	    fprintf(stderr, "output conversion failed by lack of space\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "output conversion failed by lack of space\n");
 	    break;
         case -3:
-	    fprintf(stderr,"converted %d bytes to %d bytes of output %d left\n",
+	    xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of output %d left\n",
 	            toconv, written, in->use);
 	    break;
 #endif
@@ -1961,8 +1986,10 @@
 		xmlChar charref[20];
 
 #ifdef DEBUG_ENCODING
-		fprintf(stderr, "handling output conversion error\n");
-		fprintf(stderr, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"handling output conversion error\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
 			in->content[0], in->content[1],
 			in->content[2], in->content[3]);
 #endif
@@ -1977,8 +2004,10 @@
 
 		goto retry;
 	    } else {
-		fprintf(stderr, "output conversion failed due to conv error\n");
-		fprintf(stderr, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"output conversion failed due to conv error\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
 			in->content[0], in->content[1],
 			in->content[2], in->content[3]);
 		in->content[0] = ' ';
@@ -2026,9 +2055,11 @@
 #endif /* LIBXML_ICONV_ENABLED */
 #ifdef DEBUG_ENCODING
     if (ret)
-        fprintf(stderr, "failed to close the encoding handler\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"failed to close the encoding handler\n");
     else
-        fprintf(stderr, "closed the encoding handler\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"closed the encoding handler\n");
 
 #endif
     return(ret);
diff --git a/entities.c b/entities.c
index f855f06..0fc4e8e 100644
--- a/entities.c
+++ b/entities.c
@@ -21,6 +21,7 @@
 #include <libxml/hash.h>
 #include <libxml/entities.h>
 #include <libxml/parser.h>
+#include <libxml/xmlerror.h>
 
 #define DEBUG_ENT_REF /* debugging of cross entities dependancies */
 #define ENTITY_HASH_SIZE 256 /* modify xmlEntityComputeHash accordingly */
@@ -122,7 +123,8 @@
 	return(NULL);
     ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddEntity: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddEntity: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlEntity));
@@ -236,12 +238,12 @@
     xmlDtdPtr dtd;
 
     if (doc == NULL) {
-        fprintf(stderr,
+        xmlGenericError(xmlGenericErrorContext,
 	        "xmlAddDtdEntity: doc == NULL !\n");
 	return(NULL);
     }
     if (doc->extSubset == NULL) {
-        fprintf(stderr,
+        xmlGenericError(xmlGenericErrorContext,
 	        "xmlAddDtdEntity: document without external subset !\n");
 	return(NULL);
     }
@@ -285,12 +287,12 @@
     xmlDtdPtr dtd;
 
     if (doc == NULL) {
-        fprintf(stderr,
+        xmlGenericError(xmlGenericErrorContext,
 	        "xmlAddDocEntity: document is NULL !\n");
 	return(NULL);
     }
     if (doc->intSubset == NULL) {
-        fprintf(stderr,
+        xmlGenericError(xmlGenericErrorContext,
 	        "xmlAddDtdEntity: document without internal subset !\n");
 	return(NULL);
     }
@@ -465,8 +467,10 @@
 
 
     if (warning) {
-    fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n");
-    fprintf(stderr, "   change code to use xmlEncodeEntitiesReentrant()\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "Deprecated API xmlEncodeEntities() used\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "   change code to use xmlEncodeEntitiesReentrant()\n");
     warning = 0;
     }
 
@@ -561,7 +565,8 @@
 	     * default case, this is not a valid char !
 	     * Skip it...
 	     */
-	    fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlEncodeEntities: invalid char %d\n", (int) *cur);
 	}
 #endif
 	cur++;
@@ -688,7 +693,7 @@
 		int val = 0, l = 1;
 
 		if (*cur < 0xC0) {
-		    fprintf(stderr,
+		    xmlGenericError(xmlGenericErrorContext,
 			    "xmlEncodeEntitiesReentrant : input not UTF-8\n");
 		    doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
 #ifdef HAVE_SNPRINTF
@@ -723,7 +728,7 @@
 		    l = 4;
 		}
 		if ((l == 1) || (!IS_CHAR(val))) {
-		    fprintf(stderr,
+		    xmlGenericError(xmlGenericErrorContext,
 			"xmlEncodeEntitiesReentrant : char out of range\n");
 		    doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
 #ifdef HAVE_SNPRINTF
@@ -769,7 +774,8 @@
 	     * default case, this is not a valid char !
 	     * Skip it...
 	     */
-	    fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlEncodeEntities: invalid char %d\n", (int) *cur);
 	}
 #endif
 	cur++;
@@ -895,7 +901,8 @@
 
     cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
     if (cur == NULL) {
-	fprintf(stderr, "xmlCopyEntity: out of memory !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCopyEntity: out of memory !\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlEntity));
@@ -1008,7 +1015,7 @@
 	    xmlBufferWriteChar(buf, ">\n");
 	    break;
 	default:
-	    fprintf(stderr,
+	    xmlGenericError(xmlGenericErrorContext,
 		"xmlDumpEntitiesTable: internal: unknown type %d\n",
 		    ent->etype);
     }
diff --git a/error.c b/error.c
index b51aebe..806cdad 100644
--- a/error.c
+++ b/error.c
@@ -15,6 +15,64 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <libxml/parser.h>
+#include <libxml/xmlerror.h>
+
+/************************************************************************
+ * 									*
+ * 			Handling of out of context errors		*
+ * 									*
+ ************************************************************************/
+
+/**
+ * xmlGenericErrorDefaultFunc:
+ * @ctx:  an error context
+ * @msg:  the message to display/transmit
+ * @...:  extra parameters for the message display
+ * 
+ * Default handler for out of context error messages.
+ */
+void
+xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...) {
+    va_list args;
+
+    if (xmlGenericErrorContext == NULL)
+	xmlGenericErrorContext = (void *) stderr;
+
+    va_start(args, msg);
+    vfprintf((FILE *)xmlGenericErrorContext, msg, args);
+    va_end(args);
+}
+
+xmlGenericErrorFunc xmlGenericError = xmlGenericErrorDefaultFunc;
+void *xmlGenericErrorContext = NULL;
+
+
+/**
+ * xmlSetGenericErrorFunc:
+ * @ctx:  the new error handling context
+ * @handler:  the new handler function
+ *
+ * Function to reset the handler and the error context for out of
+ * context error messages.
+ * This simply means that @handler will be called for subsequent
+ * error messages while not parsing nor validating. And @ctx will
+ * be passed as first argument to @handler
+ * One can simply force messages to be emitted to another FILE * than
+ * stderr by setting @ctx to this file handle and @handler to NULL.
+ */
+void
+xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
+    if (ctx != NULL)
+	xmlGenericErrorContext = ctx;
+    if (handler != NULL)
+	xmlGenericError = handler;
+}
+
+/************************************************************************
+ * 									*
+ * 			Handling of parsing errors			*
+ * 									*
+ ************************************************************************/
 
 /**
  * xmlParserPrintFileInfo:
@@ -27,10 +85,12 @@
 xmlParserPrintFileInfo(xmlParserInputPtr input) {
     if (input != NULL) {
 	if (input->filename)
-	    fprintf(stderr, "%s:%d: ", input->filename,
+	    xmlGenericError(xmlGenericErrorContext,
+		    "%s:%d: ", input->filename,
 		    input->line);
 	else
-	    fprintf(stderr, "Entity: line %d: ", input->line);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Entity: line %d: ", input->line);
     }
 }
 
@@ -59,19 +119,20 @@
     base = cur;
     n = 0;
     while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
-        fprintf(stderr, "%c", (unsigned char) *cur++);
+        xmlGenericError(xmlGenericErrorContext,
+		"%c", (unsigned char) *cur++);
 	n++;
     }
-    fprintf(stderr, "\n");
+    xmlGenericError(xmlGenericErrorContext, "\n");
     cur = input->cur;
     while ((*cur == '\n') || (*cur == '\r'))
 	cur--;
     n = 0;
     while ((cur != base) && (n++ < 80)) {
-        fprintf(stderr, " ");
+        xmlGenericError(xmlGenericErrorContext, " ");
         base++;
     }
-    fprintf(stderr,"^\n");
+    xmlGenericError(xmlGenericErrorContext,"^\n");
 }
 
 /**
@@ -101,16 +162,16 @@
 	xmlParserPrintFileInfo(input);
     }
 
-    fprintf(stderr, "error: ");
+    xmlGenericError(xmlGenericErrorContext, "error: ");
     va_start(args, msg);
-    vfprintf(stderr, msg, args);
+    vfprintf(xmlGenericErrorContext, msg, args);
     va_end(args);
 
     if (ctxt != NULL) {
 	xmlParserPrintFileContext(input);
 	if (cur != NULL) {
 	    xmlParserPrintFileInfo(cur);
-	    fprintf(stderr, "\n");
+	    xmlGenericError(xmlGenericErrorContext, "\n");
 	    xmlParserPrintFileContext(cur);
 	}
     }
@@ -143,21 +204,28 @@
 	xmlParserPrintFileInfo(input);
     }
         
-    fprintf(stderr, "warning: ");
+    xmlGenericError(xmlGenericErrorContext, "warning: ");
     va_start(args, msg);
-    vfprintf(stderr, msg, args);
+    vfprintf(xmlGenericErrorContext, msg, args);
     va_end(args);
 
+
     if (ctxt != NULL) {
 	xmlParserPrintFileContext(input);
 	if (cur != NULL) {
 	    xmlParserPrintFileInfo(cur);
-	    fprintf(stderr, "\n");
+	    xmlGenericError(xmlGenericErrorContext, "\n");
 	    xmlParserPrintFileContext(cur);
 	}
     }
 }
 
+/************************************************************************
+ * 									*
+ * 			Handling of validation errors			*
+ * 									*
+ ************************************************************************/
+
 /**
  * xmlParserValidityError:
  * @ctx:  an XML parser context
@@ -182,9 +250,9 @@
 	xmlParserPrintFileInfo(input);
     }
 
-    fprintf(stderr, "validity error: ");
+    xmlGenericError(xmlGenericErrorContext, "validity error: ");
     va_start(args, msg);
-    vfprintf(stderr, msg, args);
+    vfprintf(xmlGenericErrorContext, msg, args);
     va_end(args);
 
     if (ctxt != NULL) {
@@ -216,9 +284,9 @@
 	xmlParserPrintFileInfo(input);
     }
         
-    fprintf(stderr, "validity warning: ");
+    xmlGenericError(xmlGenericErrorContext, "validity warning: ");
     va_start(args, msg);
-    vfprintf(stderr, msg, args);
+    vfprintf(xmlGenericErrorContext, msg, args);
     va_end(args);
 
     if (ctxt != NULL) {
@@ -226,3 +294,4 @@
     }
 }
 
+
diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h
index 25d9db0..fa675ab 100644
--- a/include/libxml/xmlerror.h
+++ b/include/libxml/xmlerror.h
@@ -135,6 +135,30 @@
     XML_ERR_URI_FRAGMENT /* 92 */
 }xmlParserErrors;
 
+/*
+ * Signature of the function to use when there is an error and
+ * no parsing or validity context available 
+ */
+typedef void (*xmlGenericErrorFunc) (void *ctx, const char *msg, ...);
+
+/*
+ * Those are the default error function and associated context to use
+ * when when there is an error and no parsing or validity context available
+ */
+
+extern xmlGenericErrorFunc xmlGenericError;
+extern void *xmlGenericErrorContext;
+
+/*
+ * Use the following function to reset the two previous global variables.
+ */
+void	xmlSetGenericErrorFunc	(void *ctx,
+				 xmlGenericErrorFunc handler);
+
+/*
+ * Default message routines used by SAX and Valid context for error
+ * and warning reporting
+ */
 void	xmlParserError		(void *ctx,
 				 const char *msg,
 				 ...);
diff --git a/nanoftp.c b/nanoftp.c
index 351a3d8..c5e7acb 100644
--- a/nanoftp.c
+++ b/nanoftp.c
@@ -63,6 +63,7 @@
 
 #include <libxml/xmlmemory.h>
 #include <libxml/nanoftp.h>
+#include <libxml/xmlerror.h>
 
 /* #define DEBUG_FTP 1  */
 #ifdef STANDALONE
@@ -420,9 +421,9 @@
     }
 #ifdef DEBUG_FTP
     if (URL == NULL)
-	printf("Removing FTP proxy info\n");
+	xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n");
     else
-	printf("Using FTP proxy %s\n", URL);
+	xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL);
 #endif
     if (URL == NULL) return;
     buf[index] = 0;
@@ -567,7 +568,8 @@
 
     if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
 #ifdef DEBUG_FTP
-        printf("xmlNanoFTPGetMore : controlBufIndex = %d\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPGetMore : controlBufIndex = %d\n",
 		ctxt->controlBufIndex);
 #endif
 	return(-1);
@@ -575,14 +577,16 @@
 
     if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
 #ifdef DEBUG_FTP
-        printf("xmlNanoFTPGetMore : controlBufUsed = %d\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPGetMore : controlBufUsed = %d\n",
 		ctxt->controlBufUsed);
 #endif
 	return(-1);
     }
     if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
 #ifdef DEBUG_FTP
-        printf("xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
 	       ctxt->controlBufIndex, ctxt->controlBufUsed);
 #endif
 	return(-1);
@@ -600,7 +604,8 @@
     size = FTP_BUF_SIZE - ctxt->controlBufUsed;
     if (size == 0) {
 #ifdef DEBUG_FTP
-        printf("xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
 #endif
 	return(0);
     }
@@ -615,7 +620,8 @@
         return(-1);
     }
 #ifdef DEBUG_FTP
-    printf("xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
+    xmlGenericError(xmlGenericErrorContext,
+	    "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
 	   ctxt->controlBufUsed, ctxt->controlBufUsed + len);
 #endif
     ctxt->controlBufUsed += len;
@@ -651,7 +657,8 @@
     end = &ctxt->controlBuf[ctxt->controlBufUsed];
 
 #ifdef DEBUG_FTP
-    printf("\n<<<\n%s\n--\n", ptr);
+    xmlGenericError(xmlGenericErrorContext,
+	    "\n<<<\n%s\n--\n", ptr);
 #endif
     while (ptr < end) {
         cur = xmlNanoFTPParseResponse(ctxt, ptr, end - ptr);
@@ -681,11 +688,11 @@
     ctxt->controlBufIndex = ptr - ctxt->controlBuf;
 #ifdef DEBUG_FTP
     ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
-    printf("\n---\n%s\n--\n", ptr);
+    xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
 #endif
 
 #ifdef DEBUG_FTP
-    printf("Got %d\n", res);
+    xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
 #endif
     return(res / 100);
 }
@@ -761,7 +768,7 @@
     buf[sizeof(buf) - 1] = 0;
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) return(res);
@@ -794,7 +801,7 @@
     buf[sizeof(buf) - 1] = 0;
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) return(res);
@@ -821,7 +828,7 @@
     sprintf(buf, "QUIT\r\n");
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     return(0);
@@ -947,7 +954,7 @@
             buf[sizeof(buf) - 1] = 0;
             len = strlen(buf);
 #ifdef DEBUG_FTP
-	    printf(buf);
+	    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 	    res = send(ctxt->controlFd, buf, len, 0);
 	    if (res < 0) {
@@ -977,7 +984,7 @@
                     buf[sizeof(buf) - 1] = 0;
                     len = strlen(buf);
 #ifdef DEBUG_FTP
-		    printf(buf);
+		    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 		    res = send(ctxt->controlFd, buf, len, 0);
 		    if (res < 0) {
@@ -1021,7 +1028,7 @@
                 buf[sizeof(buf) - 1] = 0;
                 len = strlen(buf);
 #ifdef DEBUG_FTP
-		printf(buf);
+		xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 		res = send(ctxt->controlFd, buf, len, 0);
 		if (res < 0) {
@@ -1060,7 +1067,7 @@
                 buf[sizeof(buf) - 1] = 0;
                 len = strlen(buf);
 #ifdef DEBUG_FTP
-		printf(buf);
+		xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 		res = send(ctxt->controlFd, buf, len, 0);
 		if (res < 0) {
@@ -1089,7 +1096,7 @@
                 buf[sizeof(buf) - 1] = 0;
                 len = strlen(buf);
 #ifdef DEBUG_FTP
-		printf(buf);
+		xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 		res = send(ctxt->controlFd, buf, len, 0);
 		if (res < 0) {
@@ -1154,7 +1161,8 @@
 	case 2:
 	    break;
 	case 3:
-	    fprintf(stderr, "FTP server asking for ACCNT on anonymous\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "FTP server asking for ACCNT on anonymous\n");
 	case 1:
 	case 4:
 	case 5:
@@ -1230,7 +1238,7 @@
     buf[sizeof(buf) - 1] = 0;
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) return(res);
@@ -1268,7 +1276,8 @@
 
     ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (ctxt->dataFd < 0) {
-        fprintf(stderr, "xmlNanoFTPGetConnection: failed to create socket\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPGetConnection: failed to create socket\n");
 	return(-1);
     }
     dataAddrLen = sizeof(dataAddr);
@@ -1279,7 +1288,7 @@
 	sprintf(buf, "PASV\r\n");
         len = strlen(buf);
 #ifdef DEBUG_FTP
-	printf(buf);
+	xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 	res = send(ctxt->controlFd, buf, len, 0);
 	if (res < 0) {
@@ -1303,7 +1312,8 @@
 	while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
 	if (sscanf(cur, "%d,%d,%d,%d,%d,%d", &temp[0], &temp[1], &temp[2],
 	            &temp[3], &temp[4], &temp[5]) != 6) {
-	    fprintf(stderr, "Invalid answer to PASV\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Invalid answer to PASV\n");
 	    if (ctxt->dataFd != -1) {
 		closesocket(ctxt->dataFd); ctxt->dataFd = -1;
 	    }
@@ -1313,7 +1323,8 @@
 	memcpy(&dataAddr.sin_addr, &ad[0], 4);
 	memcpy(&dataAddr.sin_port, &ad[4], 2);
 	if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
-	    fprintf(stderr, "Failed to create a data connection\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Failed to create a data connection\n");
 	    closesocket(ctxt->dataFd); ctxt->dataFd = -1;
 	    return (-1);
 	}
@@ -1321,14 +1332,16 @@
         getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
 	dataAddr.sin_port = 0;
 	if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
-	    fprintf(stderr, "Failed to bind a port\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Failed to bind a port\n");
 	    closesocket(ctxt->dataFd); ctxt->dataFd = -1;
 	    return (-1);
 	}
         getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
 
 	if (listen(ctxt->dataFd, 1) < 0) {
-	    fprintf(stderr, "Could not listen on port %d\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Could not listen on port %d\n",
 	            ntohs(dataAddr.sin_port));
 	    closesocket(ctxt->dataFd); ctxt->dataFd = -1;
 	    return (-1);
@@ -1347,7 +1360,7 @@
         buf[sizeof(buf) - 1] = 0;
         len = strlen(buf);
 #ifdef DEBUG_FTP
-	printf(buf);
+	xmlGenericError(xmlGenericErrorContext, buf);
 #endif
 
 	res = send(ctxt->controlFd, buf, len, 0);
@@ -1398,7 +1411,8 @@
     }
     if (res == 0) {
 #ifdef DEBUG_FTP
-	fprintf(stderr, "xmlNanoFTPCloseConnection: timeout\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlNanoFTPCloseConnection: timeout\n");
 #endif
 	closesocket(ctxt->controlFd); ctxt->controlFd = -1;
     } else {
@@ -1584,7 +1598,7 @@
     buf[sizeof(buf) - 1] = 0;
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) {
@@ -1678,7 +1692,7 @@
     sprintf(buf, "TYPE I\r\n");
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) {
@@ -1705,7 +1719,7 @@
     buf[sizeof(buf) - 1] = 0;
     len = strlen(buf);
 #ifdef DEBUG_FTP
-    printf(buf);
+    xmlGenericError(xmlGenericErrorContext, buf);
 #endif
     res = send(ctxt->controlFd, buf, len, 0);
     if (res < 0) {
@@ -1810,7 +1824,7 @@
 
     len = recv(ctxt->dataFd, dest, len, 0);
 #ifdef DEBUG_FTP
-    printf("Recvd %d bytes\n", len);
+    xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
 #endif
     if (len <= 0) {
 	xmlNanoFTPCloseConnection(ctxt);
@@ -1888,7 +1902,8 @@
 void ftpList(void *userData, const char *filename, const char* attrib,
 	     const char *owner, const char *group, unsigned long size, int links,
 	     int year, const char *month, int day, int hour, int minute) {
-    printf("%s %s %s %ld %s\n", attrib, owner, group, size, filename);
+    xmlGenericError(xmlGenericErrorContext,
+	    "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
 }
 void ftpData(void *userData, const char *data, int len) {
     if (userData == NULL) return;
@@ -1908,7 +1923,8 @@
     if (argc > 1) {
 	ctxt = xmlNanoFTPNewCtxt(argv[1]);
 	if (xmlNanoFTPConnect(ctxt) < 0) {
-	    fprintf(stderr, "Couldn't connect to %s\n", argv[1]);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Couldn't connect to %s\n", argv[1]);
 	    exit(1);
 	}
 	if (argc > 2)
@@ -1916,14 +1932,16 @@
     } else
 	ctxt = xmlNanoFTPConnectTo("localhost", 0);
     if (ctxt == NULL) {
-        fprintf(stderr, "Couldn't connect to localhost\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"Couldn't connect to localhost\n");
         exit(1);
     }
     xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
     output = fopen("/tmp/tstdata", "w");
     if (output != NULL) {
 	if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
-	    fprintf(stderr, "Failed to get file\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Failed to get file\n");
 	
     }
     xmlNanoFTPClose(ctxt);
@@ -1935,7 +1953,8 @@
 #ifdef STANDALONE
 #include <stdio.h>
 int main(int argc, char **argv) {
-    printf("%s : FTP support not compiled in\n", argv[0]);
+    xmlGenericError(xmlGenericErrorContext,
+	    "%s : FTP support not compiled in\n", argv[0]);
     return(0);
 }
 #endif /* STANDALONE */
diff --git a/nanohttp.c b/nanohttp.c
index 0ac9d30..a92a9b3 100644
--- a/nanohttp.c
+++ b/nanohttp.c
@@ -302,9 +302,11 @@
     }
 #ifdef DEBUG_HTTP
     if (URL == NULL)
-	printf("Removing HTTP proxy info\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"Removing HTTP proxy info\n");
     else
-	printf("Using HTTP proxy %s\n", URL);
+	xmlGenericError(xmlGenericErrorContext,
+		"Using HTTP proxy %s\n", URL);
 #endif
     if (URL == NULL) return;
     buf[index] = 0;
@@ -749,7 +751,7 @@
     if (h==NULL)
     {
 #ifdef DEBUG_HTTP
-	fprintf(stderr,"unable to resolve '%s'.\n", host);
+	xmlGenericError(xmlGenericErrorContext,"unable to resolve '%s'.\n", host);
 #endif
 	return(-1);
     }
@@ -764,7 +766,8 @@
     }
 
 #ifdef DEBUG_HTTP
-    fprintf(stderr, "unable to connect to '%s'.\n", host);
+    xmlGenericError(xmlGenericErrorContext,
+	    "unable to connect to '%s'.\n", host);
 #endif
     return(-1);
 }
@@ -935,9 +938,11 @@
     else
 	strcpy(p, "\r\n");
 #ifdef DEBUG_HTTP
-    printf("-> %s%s", proxy? "(Proxy) " : "", bp);
+    xmlGenericError(xmlGenericErrorContext,
+	    "-> %s%s", proxy? "(Proxy) " : "", bp);
     if ((blen -= strlen(bp)+1) < 0)
-	printf("ERROR: overflowed buffer by %d bytes\n", -blen);
+	xmlGenericError(xmlGenericErrorContext,
+		"ERROR: overflowed buffer by %d bytes\n", -blen);
 #endif
     ctxt->outptr = ctxt->out = bp;
     ctxt->state = XML_NANO_HTTP_WRITE;
@@ -955,7 +960,7 @@
 	xmlNanoHTTPScanAnswer(ctxt, p);
 
 #ifdef DEBUG_HTTP
-	printf("<- %s\n", p);
+	xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
 #endif
         xmlFree(p);
     }
@@ -963,7 +968,8 @@
     if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
         (ctxt->returnValue < 400)) {
 #ifdef DEBUG_HTTP
-	printf("\nRedirect to: %s\n", ctxt->location);
+	xmlGenericError(xmlGenericErrorContext,
+		"\nRedirect to: %s\n", ctxt->location);
 #endif
 	while (xmlNanoHTTPRecv(ctxt)) ;
         if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
@@ -974,7 +980,8 @@
 	}
 	xmlNanoHTTPFreeCtxt(ctxt);
 #ifdef DEBUG_HTTP
-	printf("Too many redirects, aborting ...\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"Too many redirects, aborting ...\n");
 #endif
 	return(NULL);
 
@@ -989,10 +996,12 @@
 
 #ifdef DEBUG_HTTP
     if (ctxt->contentType != NULL)
-	printf("\nCode %d, content-type '%s'\n\n",
+	xmlGenericError(xmlGenericErrorContext,
+		"\nCode %d, content-type '%s'\n\n",
 	       ctxt->returnValue, ctxt->contentType);
     else
-	printf("\nCode %d, no content-type\n\n",
+	xmlGenericError(xmlGenericErrorContext,
+		"\nCode %d, no content-type\n\n",
 	       ctxt->returnValue);
 #endif
 
@@ -1107,8 +1116,10 @@
 	    xmlNanoHTTPFetch(argv[1], "-", &contentType);
 	if (contentType != NULL) xmlFree(contentType);
     } else {
-        printf("%s: minimal HTTP GET implementation\n", argv[0]);
-        printf("\tusage %s [ URL [ filename ] ]\n", argv[0]);
+        xmlGenericError(xmlGenericErrorContext,
+		"%s: minimal HTTP GET implementation\n", argv[0]);
+        xmlGenericError(xmlGenericErrorContext,
+		"\tusage %s [ URL [ filename ] ]\n", argv[0]);
     }
     xmlNanoHTTPCleanup();
     xmlMemoryDump();
@@ -1119,7 +1130,8 @@
 #ifdef STANDALONE
 #include <stdio.h>
 int main(int argc, char **argv) {
-    printf("%s : HTTP support not compiled in\n", argv[0]);
+    xmlGenericError(xmlGenericErrorContext,
+	    "%s : HTTP support not compiled in\n", argv[0]);
     return(0);
 }
 #endif /* STANDALONE */
diff --git a/parser.c b/parser.c
index 9d21fec..7a93285 100644
--- a/parser.c
+++ b/parser.c
@@ -120,7 +120,8 @@
         ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,		\
 	             ctxt->name##Max * sizeof(ctxt->name##Tab[0]));	\
         if (ctxt->name##Tab == NULL) {					\
-	    fprintf(stderr, "realloc failed !\n");			\
+	    xmlGenericError(xmlGenericErrorContext,			\
+		    "realloc failed !\n");				\
 	    return(0);							\
 	}								\
     }									\
@@ -154,7 +155,8 @@
         ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
 	             ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
         if (ctxt->spaceTab == NULL) {
-	    fprintf(stderr, "realloc failed !\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "realloc failed !\n");
 	    return(0);
 	}
     }
@@ -316,7 +318,8 @@
 xmlPopInput(xmlParserCtxtPtr ctxt) {
     if (ctxt->inputNr == 1) return(0); /* End of main Input */
     if (xmlParserDebugEntities)
-	fprintf(stderr, "Popping input %d\n", ctxt->inputNr);
+	xmlGenericError(xmlGenericErrorContext,
+		"Popping input %d\n", ctxt->inputNr);
     xmlFreeInputStream(inputPop(ctxt));
     if ((*ctxt->input->cur == 0) &&
         (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
@@ -338,9 +341,11 @@
 
     if (xmlParserDebugEntities) {
 	if ((ctxt->input != NULL) && (ctxt->input->filename))
-	    fprintf(stderr, "%s(%d): ", ctxt->input->filename,
+	    xmlGenericError(xmlGenericErrorContext,
+		    "%s(%d): ", ctxt->input->filename,
 		    ctxt->input->line);
-	fprintf(stderr, "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
+	xmlGenericError(xmlGenericErrorContext,
+		"Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
     }
     inputPush(ctxt, input);
     GROW;
@@ -661,7 +666,8 @@
     NEXT;
     name = xmlParseName(ctxt);
     if (xmlParserDebugEntities)
-	fprintf(stderr, "PE Reference: %s\n", name);
+	xmlGenericError(xmlGenericErrorContext,
+		"PE Reference: %s\n", name);
     if (name == NULL) {
         ctxt->errNo = XML_ERR_PEREF_NO_NAME;
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
@@ -829,7 +835,8 @@
 	    }
 	} else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
 	    if (xmlParserDebugEntities)
-		fprintf(stderr, "String decoding Entity Reference: %.30s\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"String decoding Entity Reference: %.30s\n",
 			str);
 	    ent = xmlParseStringEntityRef(ctxt, &str);
 	    if ((ent != NULL) &&
@@ -873,7 +880,8 @@
 	    }
 	} else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
 	    if (xmlParserDebugEntities)
-		fprintf(stderr, "String decoding PE Reference: %.30s\n", str);
+		xmlGenericError(xmlGenericErrorContext,
+			"String decoding PE Reference: %.30s\n", str);
 	    ent = xmlParseStringPEReference(ctxt, &str);
 	    if (ent != NULL) {
 		xmlChar *rep;
@@ -930,7 +938,8 @@
     if ((cur == NULL) || (len < 0)) return(NULL);
     ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
     if (ret == NULL) {
-        fprintf(stderr, "malloc of %ld byte failed\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"malloc of %ld byte failed\n",
 	        (len + 1) * (long)sizeof(xmlChar));
         return(NULL);
     }
@@ -976,7 +985,7 @@
     if ((cur == NULL) || (len < 0)) return(NULL);
     ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
     if (ret == NULL) {
-        fprintf(stderr, "malloc of %ld byte failed\n",
+        xmlGenericError(xmlGenericErrorContext, "malloc of %ld byte failed\n",
 	        (len + 1) * (long)sizeof(xmlChar));
         return(NULL);
     }
@@ -1309,7 +1318,8 @@
     size = xmlStrlen(cur);
     ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar));
     if (ret == NULL) {
-        fprintf(stderr, "xmlStrncat: realloc of %ld byte failed\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlStrncat: realloc of %ld byte failed\n",
 	        (size + len + 1) * (long)sizeof(xmlChar));
         return(cur);
     }
@@ -1858,7 +1868,8 @@
     }
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return(NULL);
     }
 
@@ -1886,7 +1897,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		return(NULL);
 	    }
 	}
@@ -2213,7 +2225,8 @@
     
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return(NULL);
     }
     ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
@@ -2223,7 +2236,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		ctxt->instate = (xmlParserInputState) state;
 		return(NULL);
 	    }
@@ -2294,7 +2308,8 @@
     }
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return(NULL);
     }
     cur = CUR;
@@ -2303,7 +2318,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		return(NULL);
 	    }
 	}
@@ -2563,7 +2579,8 @@
     SKIP(4);
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	ctxt->instate = state;
 	return;
     }
@@ -2588,7 +2605,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		ctxt->instate = state;
 		return;
 	    }
@@ -2752,7 +2770,8 @@
 	    }
 	    buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "malloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"malloc of %d byte failed\n", size);
 		ctxt->instate = state;
 		return;
 	    }
@@ -2773,7 +2792,8 @@
 		    size *= 2;
 		    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 		    if (buf == NULL) {
-			fprintf(stderr, "realloc of %d byte failed\n", size);
+			xmlGenericError(xmlGenericErrorContext,
+				"realloc of %d byte failed\n", size);
 			ctxt->instate = state;
 			return;
 		    }
@@ -4430,9 +4450,11 @@
 	}
 	if (xmlParserDebugEntities) {
 	    if ((ctxt->input != NULL) && (ctxt->input->filename))
-		fprintf(stderr, "%s(%d): ", ctxt->input->filename,
+		xmlGenericError(xmlGenericErrorContext,
+			"%s(%d): ", ctxt->input->filename,
 			ctxt->input->line);
-	    fprintf(stderr, "Entering INCLUDE Conditional Section\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Entering INCLUDE Conditional Section\n");
 	}
 
 	while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
@@ -4469,9 +4491,11 @@
 	}
 	if (xmlParserDebugEntities) {
 	    if ((ctxt->input != NULL) && (ctxt->input->filename))
-		fprintf(stderr, "%s(%d): ", ctxt->input->filename,
+		xmlGenericError(xmlGenericErrorContext,
+			"%s(%d): ", ctxt->input->filename,
 			ctxt->input->line);
-	    fprintf(stderr, "Leaving INCLUDE Conditional Section\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Leaving INCLUDE Conditional Section\n");
 	}
 
     } else if ((RAW == 'I') && (NXT(1) == 'G') && (NXT(2) == 'N') &&
@@ -4492,9 +4516,11 @@
 	}
 	if (xmlParserDebugEntities) {
 	    if ((ctxt->input != NULL) && (ctxt->input->filename))
-		fprintf(stderr, "%s(%d): ", ctxt->input->filename,
+		xmlGenericError(xmlGenericErrorContext,
+			"%s(%d): ", ctxt->input->filename,
 			ctxt->input->line);
-	    fprintf(stderr, "Entering IGNORE Conditional Section\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Entering IGNORE Conditional Section\n");
 	}
 
 	/*
@@ -4538,9 +4564,11 @@
 	ctxt->disableSAX = state;
 	if (xmlParserDebugEntities) {
 	    if ((ctxt->input != NULL) && (ctxt->input->filename))
-		fprintf(stderr, "%s(%d): ", ctxt->input->filename,
+		xmlGenericError(xmlGenericErrorContext,
+			"%s(%d): ", ctxt->input->filename,
 			ctxt->input->line);
-	    fprintf(stderr, "Leaving IGNORE Conditional Section\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Leaving IGNORE Conditional Section\n");
 	}
 
     } else {
@@ -5866,7 +5894,8 @@
 	        maxatts = 10;
 	        atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
 		if (atts == NULL) {
-		    fprintf(stderr, "malloc of %ld byte failed\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "malloc of %ld byte failed\n",
 			    maxatts * (long)sizeof(xmlChar *));
 		    return(NULL);
 		}
@@ -5875,7 +5904,8 @@
 	        atts = (const xmlChar **) xmlRealloc((void *) atts,
 						     maxatts * sizeof(xmlChar *));
 		if (atts == NULL) {
-		    fprintf(stderr, "realloc of %ld byte failed\n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "realloc of %ld byte failed\n",
 			    maxatts * (long)sizeof(xmlChar *));
 		    return(NULL);
 		}
@@ -6015,7 +6045,7 @@
     spacePop(ctxt);
     if (oldname != NULL) {
 #ifdef DEBUG_STACK
-	fprintf(stderr,"Close: popped %s\n", oldname);
+	xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 	xmlFree(oldname);
     }
@@ -6083,7 +6113,8 @@
     cur = CUR_CHAR(l);
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return;
     }
     while (IS_CHAR(cur) &&
@@ -6092,7 +6123,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		return;
 	    }
 	}
@@ -6304,7 +6336,7 @@
 	spacePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG_STACK
-	    fprintf(stderr,"Close: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
 	}
@@ -6336,7 +6368,7 @@
 	spacePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG_STACK
-	    fprintf(stderr,"Close: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
 	}
@@ -6374,7 +6406,7 @@
 	spacePop(ctxt);
 	if (oldname != NULL) {
 #ifdef DEBUG_STACK
-	    fprintf(stderr,"Close: popped %s\n", oldname);
+	    xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 	    xmlFree(oldname);
 	}
@@ -6417,7 +6449,8 @@
 
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return(NULL);
     }
     cur = CUR;
@@ -6430,7 +6463,8 @@
 	    size *= 2;
 	    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 	    if (buf == NULL) {
-		fprintf(stderr, "realloc of %d byte failed\n", size);
+		xmlGenericError(xmlGenericErrorContext,
+			"realloc of %d byte failed\n", size);
 		return(NULL);
 	    }
 	}
@@ -6537,7 +6571,8 @@
         ((cur >= 'A') && (cur <= 'Z'))) {
 	buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
 	if (buf == NULL) {
-	    fprintf(stderr, "malloc of %d byte failed\n", size);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "malloc of %d byte failed\n", size);
 	    return(NULL);
 	}
 	
@@ -6553,7 +6588,8 @@
 		size *= 2;
 		buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 		if (buf == NULL) {
-		    fprintf(stderr, "realloc of %d byte failed\n", size);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "realloc of %d byte failed\n", size);
 		    return(NULL);
 		}
 	    }
@@ -7251,13 +7287,16 @@
 	    ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
 	    if (next == 0)
-		fprintf(stderr, "PP: lookup '%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: lookup '%c' found at %d\n",
 			first, base);
 	    else if (third == 0)
-		fprintf(stderr, "PP: lookup '%c%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: lookup '%c%c' found at %d\n",
 			first, next, base);
 	    else 
-		fprintf(stderr, "PP: lookup '%c%c%c' found at %d\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: lookup '%c%c%c' found at %d\n",
 			first, next, third, base);
 #endif
 	    return(base - (in->cur - in->base));
@@ -7266,11 +7305,14 @@
     ctxt->checkIndex = base;
 #ifdef DEBUG_PUSH
     if (next == 0)
-	fprintf(stderr, "PP: lookup '%c' failed\n", first);
+	xmlGenericError(xmlGenericErrorContext,
+		"PP: lookup '%c' failed\n", first);
     else if (third == 0)
-	fprintf(stderr, "PP: lookup '%c%c' failed\n", first, next);
+	xmlGenericError(xmlGenericErrorContext,
+		"PP: lookup '%c%c' failed\n", first, next);
     else	
-	fprintf(stderr, "PP: lookup '%c%c%c' failed\n", first, next, third);
+	xmlGenericError(xmlGenericErrorContext,
+		"PP: lookup '%c%c%c' failed\n", first, next, third);
 #endif
     return(-1);
 }
@@ -7293,35 +7335,50 @@
 #ifdef DEBUG_PUSH
     switch (ctxt->instate) {
 	case XML_PARSER_EOF:
-	    fprintf(stderr, "PP: try EOF\n"); break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try EOF\n"); break;
 	case XML_PARSER_START:
-	    fprintf(stderr, "PP: try START\n"); break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try START\n"); break;
 	case XML_PARSER_MISC:
-	    fprintf(stderr, "PP: try MISC\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try MISC\n");break;
 	case XML_PARSER_COMMENT:
-	    fprintf(stderr, "PP: try COMMENT\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try COMMENT\n");break;
 	case XML_PARSER_PROLOG:
-	    fprintf(stderr, "PP: try PROLOG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try PROLOG\n");break;
 	case XML_PARSER_START_TAG:
-	    fprintf(stderr, "PP: try START_TAG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try START_TAG\n");break;
 	case XML_PARSER_CONTENT:
-	    fprintf(stderr, "PP: try CONTENT\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try CONTENT\n");break;
 	case XML_PARSER_CDATA_SECTION:
-	    fprintf(stderr, "PP: try CDATA_SECTION\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try CDATA_SECTION\n");break;
 	case XML_PARSER_END_TAG:
-	    fprintf(stderr, "PP: try END_TAG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try END_TAG\n");break;
 	case XML_PARSER_ENTITY_DECL:
-	    fprintf(stderr, "PP: try ENTITY_DECL\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try ENTITY_DECL\n");break;
 	case XML_PARSER_ENTITY_VALUE:
-	    fprintf(stderr, "PP: try ENTITY_VALUE\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try ENTITY_VALUE\n");break;
 	case XML_PARSER_ATTRIBUTE_VALUE:
-	    fprintf(stderr, "PP: try ATTRIBUTE_VALUE\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try ATTRIBUTE_VALUE\n");break;
 	case XML_PARSER_DTD:
-	    fprintf(stderr, "PP: try DTD\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try DTD\n");break;
 	case XML_PARSER_EPILOG:
-	    fprintf(stderr, "PP: try EPILOG\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try EPILOG\n");break;
 	case XML_PARSER_PI:
-	    fprintf(stderr, "PP: try PI\n");break;
+	    xmlGenericError(xmlGenericErrorContext,
+		    "PP: try PI\n");break;
     }
 #endif
 
@@ -7383,7 +7440,8 @@
 		    ctxt->disableSAX = 1;
 		    ctxt->instate = XML_PARSER_EOF;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering EOF\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering EOF\n");
 #endif
 		    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
 			ctxt->sax->endDocument(ctxt->userData);
@@ -7404,7 +7462,8 @@
 			(IS_BLANK(ctxt->input->cur[5]))) {
 			ret += 5;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: Parsing XML Decl\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: Parsing XML Decl\n");
 #endif
 			xmlParseXMLDecl(ctxt);
 			if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
@@ -7424,7 +7483,8 @@
 			    ctxt->sax->startDocument(ctxt->userData);
 			ctxt->instate = XML_PARSER_MISC;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering MISC\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering MISC\n");
 #endif
 		    } else {
 			ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
@@ -7433,7 +7493,8 @@
 			    ctxt->sax->startDocument(ctxt->userData);
 			ctxt->instate = XML_PARSER_MISC;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering MISC\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering MISC\n");
 #endif
 		    }
 		} else {
@@ -7446,7 +7507,8 @@
 			ctxt->sax->startDocument(ctxt->userData);
 		    ctxt->instate = XML_PARSER_MISC;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering MISC\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering MISC\n");
 #endif
 		}
 		break;
@@ -7465,7 +7527,8 @@
 		        (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing PI\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing PI\n");
 #endif
 		    xmlParsePI(ctxt);
 		} else if ((cur == '<') && (next == '!') &&
@@ -7474,7 +7537,8 @@
 		        (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing Comment\n");
 #endif
 		    xmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_MISC;
@@ -7487,14 +7551,16 @@
 		        (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing internal subset\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing internal subset\n");
 #endif
 		    ctxt->inSubset = 1;
 		    xmlParseDocTypeDecl(ctxt);
 		    if (RAW == '[') {
 			ctxt->instate = XML_PARSER_DTD;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering DTD\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering DTD\n");
 #endif
 		    } else {
 			/*
@@ -7509,7 +7575,8 @@
 			ctxt->inSubset = 0;
 			ctxt->instate = XML_PARSER_PROLOG;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering PROLOG\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering PROLOG\n");
 #endif
 		    }
 		} else if ((cur == '<') && (next == '!') &&
@@ -7518,7 +7585,8 @@
 		} else {
 		    ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering START_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering START_TAG\n");
 #endif
 		}
 		break;
@@ -7537,7 +7605,8 @@
 		        (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing PI\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing PI\n");
 #endif
 		    xmlParsePI(ctxt);
 		} else if ((cur == '<') && (next == '!') &&
@@ -7546,7 +7615,8 @@
 		        (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing Comment\n");
 #endif
 		    xmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_PROLOG;
@@ -7556,7 +7626,8 @@
 		} else {
 		    ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering START_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering START_TAG\n");
 #endif
 		}
 		break;
@@ -7575,7 +7646,8 @@
 		        (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing PI\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing PI\n");
 #endif
 		    xmlParsePI(ctxt);
 		    ctxt->instate = XML_PARSER_EPILOG;
@@ -7585,7 +7657,8 @@
 		        (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing Comment\n");
 #endif
 		    xmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_EPILOG;
@@ -7601,7 +7674,8 @@
 		    ctxt->disableSAX = 1;
 		    ctxt->instate = XML_PARSER_EOF;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering EOF\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering EOF\n");
 #endif
 		    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL) &&
 			(!ctxt->disableSAX))
@@ -7624,7 +7698,8 @@
 		    ctxt->disableSAX = 1;
 		    ctxt->instate = XML_PARSER_EOF;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering EOF\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering EOF\n");
 #endif
 		    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL) &&
 			(!ctxt->disableSAX))
@@ -7643,7 +7718,8 @@
 		    spacePop(ctxt);
 		    ctxt->instate = XML_PARSER_EOF;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering EOF\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering EOF\n");
 #endif
 		    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL) &&
 			(!ctxt->disableSAX))
@@ -7674,19 +7750,21 @@
 		    spacePop(ctxt);
 		    if (oldname != NULL) {
 #ifdef DEBUG_STACK
-			fprintf(stderr,"Close: popped %s\n", oldname);
+			xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 			xmlFree(oldname);
 		    }
 		    if (ctxt->name == NULL) {
 			ctxt->instate = XML_PARSER_EPILOG;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering EPILOG\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering EPILOG\n");
 #endif
 		    } else {
 			ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-			fprintf(stderr, "PP: entering CONTENT\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"PP: entering CONTENT\n");
 #endif
 		    }
 		    break;
@@ -7710,7 +7788,7 @@
 		    spacePop(ctxt);
 		    if (oldname != NULL) {
 #ifdef DEBUG_STACK
-			fprintf(stderr,"Close: popped %s\n", oldname);
+			xmlGenericError(xmlGenericErrorContext,"Close: popped %s\n", oldname);
 #endif
 			xmlFree(oldname);
 		    }
@@ -7718,7 +7796,8 @@
 		xmlFree(name);
 		ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering CONTENT\n");
 #endif
                 break;
 	    }
@@ -7752,7 +7831,8 @@
 		        (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing PI\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing PI\n");
 #endif
 		    xmlParsePI(ctxt);
 		} else if ((cur == '<') && (next == '!') &&
@@ -7761,7 +7841,8 @@
 		        (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing Comment\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing Comment\n");
 #endif
 		    xmlParseComment(ctxt);
 		    ctxt->instate = XML_PARSER_CONTENT;
@@ -7773,7 +7854,8 @@
 		    SKIP(9);
 		    ctxt->instate = XML_PARSER_CDATA_SECTION;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering CDATA_SECTION\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering CDATA_SECTION\n");
 #endif
 		    break;
 		} else if ((cur == '<') && (next == '!') &&
@@ -7782,13 +7864,15 @@
 		} else if ((cur == '<') && (next == '/')) {
 		    ctxt->instate = XML_PARSER_END_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering END_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering END_TAG\n");
 #endif
 		    break;
 		} else if (cur == '<') {
 		    ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering START_TAG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering START_TAG\n");
 #endif
 		    break;
 		} else if (cur == '&') {
@@ -7796,7 +7880,8 @@
 		        (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
 			goto done;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing Reference\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing Reference\n");
 #endif
 		    xmlParseReference(ctxt);
 		} else {
@@ -7820,7 +7905,8 @@
                     }
 		    ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: Parsing char data\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: Parsing char data\n");
 #endif
 		    xmlParseCharData(ctxt, 0);
 		}
@@ -7872,7 +7958,8 @@
 		    ctxt->checkIndex = 0;
 		    ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering CONTENT\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering CONTENT\n");
 #endif
 		}
 		break;
@@ -7887,12 +7974,14 @@
 		if (ctxt->name == NULL) {
 		    ctxt->instate = XML_PARSER_EPILOG;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering EPILOG\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering EPILOG\n");
 #endif
 		} else {
 		    ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		    fprintf(stderr, "PP: entering CONTENT\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: entering CONTENT\n");
 #endif
 		}
 		break;
@@ -7956,7 +8045,8 @@
 		    ctxt->checkIndex = base;
 #ifdef DEBUG_PUSH
 		if (next == 0)
-		    fprintf(stderr, "PP: lookup of int subset end filed\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PP: lookup of int subset end filed\n");
 #endif
 	        goto done;
 
@@ -7971,57 +8061,70 @@
 		ctxt->instate = XML_PARSER_PROLOG;
 		ctxt->checkIndex = 0;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering PROLOG\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering PROLOG\n");
 #endif
                 break;
 	    }
             case XML_PARSER_COMMENT:
-		fprintf(stderr, "PP: internal error, state == COMMENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == COMMENT\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_PI:
-		fprintf(stderr, "PP: internal error, state == PI\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == PI\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering CONTENT\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering CONTENT\n");
 #endif
 		break;
             case XML_PARSER_ENTITY_DECL:
-		fprintf(stderr, "PP: internal error, state == ENTITY_DECL\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == ENTITY_DECL\n");
 		ctxt->instate = XML_PARSER_DTD;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering DTD\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering DTD\n");
 #endif
 		break;
             case XML_PARSER_ENTITY_VALUE:
-		fprintf(stderr, "PP: internal error, state == ENTITY_VALUE\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == ENTITY_VALUE\n");
 		ctxt->instate = XML_PARSER_CONTENT;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering DTD\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering DTD\n");
 #endif
 		break;
             case XML_PARSER_ATTRIBUTE_VALUE:
-		fprintf(stderr, "PP: internal error, state == ATTRIBUTE_VALUE\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == ATTRIBUTE_VALUE\n");
 		ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering START_TAG\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering START_TAG\n");
 #endif
 		break;
             case XML_PARSER_SYSTEM_LITERAL:
-		fprintf(stderr, "PP: internal error, state == SYSTEM_LITERAL\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: internal error, state == SYSTEM_LITERAL\n");
 		ctxt->instate = XML_PARSER_START_TAG;
 #ifdef DEBUG_PUSH
-		fprintf(stderr, "PP: entering START_TAG\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PP: entering START_TAG\n");
 #endif
 		break;
 	}
     }
 done:    
 #ifdef DEBUG_PUSH
-    fprintf(stderr, "PP: done %d\n", ret);
+    xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
 #endif
     return(ret);
 }
@@ -8062,7 +8165,7 @@
 	ctxt->input->base = ctxt->input->buf->buffer->content + base;
 	ctxt->input->cur = ctxt->input->base + cur;
 #ifdef DEBUG_PUSH
-	fprintf(stderr, "PP: pushed %d\n", size);
+	xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
 #endif
 
 	if ((terminate) || (ctxt->input->buf->buffer->use > 80))
@@ -8190,7 +8293,7 @@
         (ctxt->input->buf != NULL))  {	      
 	xmlParserInputBufferPush(ctxt->input->buf, size, chunk);	      
 #ifdef DEBUG_PUSH
-	fprintf(stderr, "PP: pushed %d\n", size);
+	xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
 #endif
     }
 
@@ -8532,7 +8635,8 @@
 	/* Allocate the Node stack */
 	ctxt->vctxt.nodeTab = (xmlNodePtr *) xmlMalloc(4 * sizeof(xmlNodePtr));
 	if (ctxt->vctxt.nodeTab == NULL) {
-	    fprintf(stderr, "xmlParseCtxtExternalEntity: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlParseCtxtExternalEntity: out of memory\n");
 	    ctxt->validate = 0;
 	    ctxt->vctxt.error = NULL;
 	    ctxt->vctxt.warning = NULL;
diff --git a/parserInternals.c b/parserInternals.c
index cdf7156..b7b54bb 100644
--- a/parserInternals.c
+++ b/parserInternals.c
@@ -68,13 +68,13 @@
     int myversion = (int) LIBXML_VERSION;
 
     if ((myversion / 10000) != (version / 10000)) {
-	fprintf(stderr, 
+	xmlGenericError(xmlGenericErrorContext, 
 		"Fatal: program compiled against libxml %d using libxml %d\n",
 		(version / 10000), (myversion / 10000));
 	exit(1);
     }
     if ((myversion / 100) < (version / 100)) {
-	fprintf(stderr, 
+	xmlGenericError(xmlGenericErrorContext, 
 		"Warning: program compiled against libxml %d using older %d\n",
 		(version / 100), (myversion / 100));
     }
@@ -877,15 +877,18 @@
 
 void check_buffer(xmlParserInputPtr in) {
     if (in->base != in->buf->buffer->content) {
-        fprintf(stderr, "xmlParserInput: base mismatch problem\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInput: base mismatch problem\n");
     }
     if (in->cur < in->base) {
-        fprintf(stderr, "xmlParserInput: cur < base problem\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInput: cur < base problem\n");
     }
     if (in->cur > in->base + in->buf->buffer->use) {
-        fprintf(stderr, "xmlParserInput: cur > base + use problem\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInput: cur > base + use problem\n");
     }
-    fprintf(stderr,"buffer %x : content %x, cur %d, use %d, size %d\n",
+    xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
             (int) in, (int) in->buf->buffer->content, in->cur - in->base,
 	    in->buf->buffer->use, in->buf->buffer->size);
 }
@@ -913,7 +916,7 @@
     int index;
 
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "Read\n");
+    xmlGenericError(xmlGenericErrorContext, "Read\n");
 #endif
     if (in->buf == NULL) return(-1);
     if (in->base == NULL) return(-1);
@@ -961,7 +964,7 @@
     int index;
 
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "Grow\n");
+    xmlGenericError(xmlGenericErrorContext, "Grow\n");
 #endif
     if (in->buf == NULL) return(-1);
     if (in->base == NULL) return(-1);
@@ -1015,7 +1018,7 @@
     int index;
 
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "Shrink\n");
+    xmlGenericError(xmlGenericErrorContext, "Shrink\n");
 #endif
     if (in->buf == NULL) return;
     if (in->base == NULL) return;
@@ -1479,7 +1482,8 @@
 	else if (val < 0x10000) len = 3;
 	else if (val < 0x110000) len = 4;
 	if (len == 0) {
-	    fprintf(stderr, "Internal error, xmlCopyChar 0x%X out of bound\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Internal error, xmlCopyChar 0x%X out of bound\n",
 		    val);
 	    return(0);
 	}
@@ -1736,7 +1740,8 @@
 						      ctxt->input->buf->raw);
 		    }
 		    if (nbchars < 0) {
-			fprintf(stderr, "xmlSwitchToEncoding: encoder error\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSwitchToEncoding: encoder error\n");
 			return(-1);
 		    }
 		    ctxt->input->base =
@@ -1776,7 +1781,8 @@
 		                               ctxt->input->buf->buffer,
 					       ctxt->input->buf->raw);
 		    if (nbchars < 0) {
-			fprintf(stderr, "xmlSwitchToEncoding: encoder error\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSwitchToEncoding: encoder error\n");
 			return(-1);
 		    }
 
@@ -1880,7 +1886,7 @@
     xmlParserInputPtr inputStream;
 
     if (xmlParserDebugEntities)
-	fprintf(stderr, "new input from I/O\n");
+	xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
     inputStream = xmlNewInputStream(ctxt);
     if (inputStream == NULL) {
 	return(NULL);
@@ -1918,7 +1924,8 @@
 	return(NULL);
     }
     if (xmlParserDebugEntities)
-	fprintf(stderr, "new input from entity: %s\n", entity->name);
+	xmlGenericError(xmlGenericErrorContext,
+		"new input from entity: %s\n", entity->name);
     if (entity->content == NULL) {
 	switch (entity->etype) {
             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
@@ -1982,7 +1989,8 @@
 	return(NULL);
     }
     if (xmlParserDebugEntities)
-	fprintf(stderr, "new fixed input: %.30s\n", buffer);
+	xmlGenericError(xmlGenericErrorContext,
+		"new fixed input: %.30s\n", buffer);
     input = xmlNewInputStream(ctxt);
     if (input == NULL) {
 	return(NULL);
@@ -2010,7 +2018,8 @@
     xmlChar *URI = NULL;
 
     if (xmlParserDebugEntities)
-	fprintf(stderr, "new input from file: %s\n", filename);
+	xmlGenericError(xmlGenericErrorContext,
+		"new input from file: %s\n", filename);
     if (ctxt == NULL) return(NULL);
     buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
     if (buf == NULL)
@@ -2059,15 +2068,18 @@
 
     sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
     if (sax == NULL) {
-        fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitParserCtxt: out of memory\n");
     }
     else
         memset(sax, 0, sizeof(xmlSAXHandler));
 
     /* Allocate the Input stack */
-    ctxt->inputTab = (xmlParserInputPtr *) xmlMalloc(5 * sizeof(xmlParserInputPtr));
+    ctxt->inputTab = (xmlParserInputPtr *)
+	        xmlMalloc(5 * sizeof(xmlParserInputPtr));
     if (ctxt->inputTab == NULL) {
-        fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitParserCtxt: out of memory\n");
 	ctxt->inputNr = 0;
 	ctxt->inputMax = 0;
 	ctxt->input = NULL;
@@ -2091,7 +2103,8 @@
     /* Allocate the Node stack */
     ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
     if (ctxt->nodeTab == NULL) {
-        fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitParserCtxt: out of memory\n");
 	ctxt->nodeNr = 0;
 	ctxt->nodeMax = 0;
 	ctxt->node = NULL;
@@ -2107,7 +2120,8 @@
     /* Allocate the Name stack */
     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
     if (ctxt->nameTab == NULL) {
-        fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitParserCtxt: out of memory\n");
 	ctxt->nodeNr = 0;
 	ctxt->nodeMax = 0;
 	ctxt->node = NULL;
@@ -2126,7 +2140,8 @@
     /* Allocate the space stack */
     ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
     if (ctxt->spaceTab == NULL) {
-        fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlInitParserCtxt: out of memory\n");
 	ctxt->nodeNr = 0;
 	ctxt->nodeMax = 0;
 	ctxt->node = NULL;
@@ -2169,7 +2184,8 @@
 	/* Allocate the Node stack */
 	ctxt->vctxt.nodeTab = (xmlNodePtr *) xmlMalloc(4 * sizeof(xmlNodePtr));
 	if (ctxt->vctxt.nodeTab == NULL) {
-	    fprintf(stderr, "xmlInitParserCtxt: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlInitParserCtxt: out of memory\n");
 	    ctxt->vctxt.nodeMax = 0;
 	    ctxt->validate = 0;
 	    ctxt->vctxt.error = NULL;
@@ -2247,7 +2263,8 @@
 
     ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
     if (ctxt == NULL) {
-        fprintf(stderr, "xmlNewParserCtxt : cannot allocate context\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewParserCtxt : cannot allocate context\n");
         perror("malloc");
 	return(NULL);
     }
@@ -2540,7 +2557,8 @@
 
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlDecodeEntities() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlDecodeEntities() deprecated function reached\n");
 	deprecated = 1;
     }
 
@@ -2581,7 +2599,8 @@
 	} else if ((c == '&') && (ctxt->token != '&') &&
 		   (what & XML_SUBSTITUTE_REF)) {
 	    if (xmlParserDebugEntities)
-		fprintf(stderr, "decoding Entity Reference\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"decoding Entity Reference\n");
 	    ent = xmlParseEntityRef(ctxt);
 	    if ((ent != NULL) && 
 		(ctxt->replaceEntities != 0)) {
@@ -2611,7 +2630,8 @@
 	     * parsed if any. We will be called back later.
 	     */
 	    if (xmlParserDebugEntities)
-		fprintf(stderr, "decoding PE Reference\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"decoding PE Reference\n");
 	    if (nbchars != 0) break;
 
 	    xmlParsePEReference(ctxt);
@@ -2665,7 +2685,8 @@
 
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlNamespaceParseNCName() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlNamespaceParseNCName() deprecated function reached\n");
 	deprecated = 1;
     }
 
@@ -2674,7 +2695,8 @@
     GROW;
     if (!IS_LETTER(cur) && (cur != '_')) return(NULL);
 
-fprintf(stderr, "xmlNamespaceParseNCName: reached loop 3\n");
+xmlGenericError(xmlGenericErrorContext,
+	"xmlNamespaceParseNCName: reached loop 3\n");
     while ((IS_LETTER(cur)) || (IS_DIGIT(cur)) || /* NOT REACHED */
            (cur == '.') || (cur == '-') ||
 	   (cur == '_') ||
@@ -2684,7 +2706,7 @@
 	NEXTL(l);
 	cur = CUR_CHAR(l);
 	if (len >= XML_MAX_NAMELEN) {
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	       "xmlNamespaceParseNCName: reached XML_MAX_NAMELEN limit\n");
 	    while ((IS_LETTER(cur)) || (IS_DIGIT(cur)) ||/* NOT REACHED */
 		   (cur == '.') || (cur == '-') ||
@@ -2727,7 +2749,8 @@
 
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlNamespaceParseQName() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlNamespaceParseQName() deprecated function reached\n");
 	deprecated = 1;
     }
 
@@ -2767,7 +2790,8 @@
 xmlNamespaceParseNSDef(xmlParserCtxtPtr ctxt) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlNamespaceParseNSDef() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlNamespaceParseNSDef() deprecated function reached\n");
 	deprecated = 1;
     }
     return(NULL);
@@ -2801,7 +2825,8 @@
 xmlParseQuotedString(xmlParserCtxtPtr ctxt) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlParseQuotedString() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlParseQuotedString() deprecated function reached\n");
 	deprecated = 1;
     }
     return(NULL);
@@ -2814,10 +2839,12 @@
 
     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
     if (buf == NULL) {
-	fprintf(stderr, "malloc of %d byte failed\n", size);
+	xmlGenericError(xmlGenericErrorContext,
+		"malloc of %d byte failed\n", size);
 	return(NULL);
     }
-fprintf(stderr, "xmlParseQuotedString: reached loop 4\n");
+xmlGenericError(xmlGenericErrorContext,
+	"xmlParseQuotedString: reached loop 4\n");
     if (RAW == '"') {
         NEXT;
 	c = CUR_CHAR(l);
@@ -2826,7 +2853,8 @@
 		size *= 2;
 		buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 		if (buf == NULL) {
-		    fprintf(stderr, "realloc of %d byte failed\n", size);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "realloc of %d byte failed\n", size);
 		    return(NULL);
 		}
 	    }
@@ -2852,7 +2880,8 @@
 		size *= 2;
 		buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
 		if (buf == NULL) {
-		    fprintf(stderr, "realloc of %d byte failed\n", size);
+		    xmlGenericError(xmlGenericErrorContext,
+			    "realloc of %d byte failed\n", size);
 		    return(NULL);
 		}
 	    }
@@ -2894,7 +2923,8 @@
 xmlParseNamespace(xmlParserCtxtPtr ctxt) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlParseNamespace() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlParseNamespace() deprecated function reached\n");
 	deprecated = 1;
     }
 
@@ -2908,7 +2938,8 @@
      */
     SKIP_BLANKS;
 
-fprintf(stderr, "xmlParseNamespace: reached loop 5\n");
+xmlGenericError(xmlGenericErrorContext,
+	"xmlParseNamespace: reached loop 5\n");
     while (IS_CHAR(RAW) && (RAW != '>')) { /* NOT REACHED */
 	/*
 	 * We can have "ns" or "prefix" attributes
@@ -3019,7 +3050,8 @@
 xmlScanName(xmlParserCtxtPtr ctxt) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlScanName() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlScanName() deprecated function reached\n");
 	deprecated = 1;
     }
     return(NULL);
@@ -3044,7 +3076,7 @@
 	buf[len] = NXT(len);
 	len++;
 	if (len >= XML_MAX_NAMELEN) {
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	       "xmlScanName: reached XML_MAX_NAMELEN limit\n");
 	    while ((IS_LETTER(NXT(len))) || /* NOT REACHED */
 		   (IS_DIGIT(NXT(len))) ||
@@ -3091,7 +3123,8 @@
 xmlParserHandleReference(xmlParserCtxtPtr ctxt) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlParserHandleReference() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlParserHandleReference() deprecated function reached\n");
 	deprecated = 1;
     }
 
@@ -3238,9 +3271,12 @@
     }
 
 /* TODO: this seems not reached anymore .... Verify ... */
-fprintf(stderr, "Reached deprecated section in xmlParserHandleReference()\n");
-fprintf(stderr, "Please forward the document to Daniel.Veillard@w3.org\n");
-fprintf(stderr, "indicating the version: %s, thanks !\n", xmlParserVersion);
+xmlGenericError(xmlGenericErrorContext,
+	"Reached deprecated section in xmlParserHandleReference()\n");
+xmlGenericError(xmlGenericErrorContext,
+	"Please forward the document to Daniel.Veillard@w3.org\n");
+xmlGenericError(xmlGenericErrorContext,
+	"indicating the version: %s, thanks !\n", xmlParserVersion);
     NEXT;
     name = xmlScanName(ctxt);
     if (name == NULL) {
@@ -3329,7 +3365,8 @@
 xmlHandleEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
     static int deprecated = 0;
     if (!deprecated) {
-	fprintf(stderr, "xmlHandleEntity() deprecated function reached\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlHandleEntity() deprecated function reached\n");
 	deprecated = 1;
     }
 
diff --git a/testHTML.c b/testHTML.c
index 541620e..6a2a896 100644
--- a/testHTML.c
+++ b/testHTML.c
@@ -40,6 +40,7 @@
 #include <libxml/HTMLparser.h>
 #include <libxml/HTMLtree.h>
 #include <libxml/debugXML.h>
+#include <libxml/xmlerror.h>
 
 #ifdef LIBXML_DEBUG_ENABLED
 static int debug = 0;
@@ -719,7 +720,8 @@
 	doc = htmlParseFile(filename, NULL);
     }
     if (doc == NULL) {
-        fprintf(stderr, "Could not parse %s\n", filename);
+        xmlGenericError(xmlGenericErrorContext,
+		"Could not parse %s\n", filename);
     }
 
     /*
diff --git a/testSAX.c b/testSAX.c
index 444ae7a..b74061d 100644
--- a/testSAX.c
+++ b/testSAX.c
@@ -617,7 +617,8 @@
 	    }
 	    fclose(f);
 	} else {
-	    fprintf(stderr, "Cannot read file %s\n", filename);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Cannot read file %s\n", filename);
 	}
 	/*
 	 * Debug callback
diff --git a/testXPath.c b/testXPath.c
index c226cf0..487987f 100644
--- a/testXPath.c
+++ b/testXPath.c
@@ -41,6 +41,7 @@
 #include <libxml/debugXML.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/parserInternals.h>
+#include <libxml/xmlerror.h>
 #if defined(LIBXML_XPTR_ENABLED)
 #include <libxml/xpointer.h>
 static int xptr = 0;
@@ -115,7 +116,8 @@
 
     input = fopen(filename, "r");
     if (input == NULL) {
-        fprintf(stderr, "Cannot open %s for reading\n", filename);
+        xmlGenericError(xmlGenericErrorContext,
+		"Cannot open %s for reading\n", filename);
 	return;
     }
     while (fgets(expr, 4500, input) != NULL) {
diff --git a/tree.c b/tree.c
index 3212eb1..5aaece9 100644
--- a/tree.c
+++ b/tree.c
@@ -30,6 +30,7 @@
 #include <libxml/parser.h>
 #include <libxml/entities.h>
 #include <libxml/valid.h>
+#include <libxml/xmlerror.h>
 
 static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
 static xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
@@ -104,7 +105,8 @@
     if ((doc == NULL) || (doc->oldNs == NULL)) return;
     if (doc->children == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlUpgradeOldNs: failed no root !\n");
 #endif
 	return;
     }
@@ -142,7 +144,8 @@
      */
     cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewNs : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewNs : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNs));
@@ -209,7 +212,8 @@
      */
     cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewGlobalNs : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewGlobalNs : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNs));
@@ -248,7 +252,8 @@
 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
     if (node == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlSetNs: node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlSetNs: node == NULL\n");
 #endif
 	return;
     }
@@ -265,7 +270,8 @@
 xmlFreeNs(xmlNsPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeNs : ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeNs : ns == NULL\n");
 #endif
 	return;
     }
@@ -286,7 +292,8 @@
     xmlNsPtr next;
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeNsList : ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeNsList : ns == NULL\n");
 #endif
 	return;
     }
@@ -316,7 +323,8 @@
 
     if ((doc != NULL) && (doc->extSubset != NULL)) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDtd(%s): document %s already have a DTD %s\n",
 	    /* !!! */ (char *) name, doc->name,
 	    /* !!! */ (char *)doc->extSubset->name);
 #endif
@@ -328,7 +336,8 @@
      */
     cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewDtd : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDtd : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0 , sizeof(xmlDtd));
@@ -387,7 +396,8 @@
 
     if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, 
+        xmlGenericError(xmlGenericErrorContext,
+
      "xmlCreateIntSubset(): document %s already have an internal subset\n",
 	    doc->name);
 #endif
@@ -399,7 +409,8 @@
      */
     cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewDtd : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDtd : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlDtd));
@@ -447,7 +458,8 @@
 xmlFreeDtd(xmlDtdPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeDtd : DTD == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeDtd : DTD == NULL\n");
 #endif
 	return;
     }
@@ -505,7 +517,8 @@
      */
     cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewDoc : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDoc : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlDoc));
@@ -529,7 +542,8 @@
 xmlFreeDoc(xmlDocPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeDoc : document == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeDoc : document == NULL\n");
 #endif
 	return;
     }
@@ -597,7 +611,7 @@
 	    while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
 	    if ((*cur == 0) || (cur - value >= len)) {
 #ifdef DEBUG_TREE
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		    "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
 #endif
 	        return(ret);
@@ -710,7 +724,7 @@
 	    while ((*cur != 0) && (*cur != ';')) cur++;
 	    if (*cur == 0) {
 #ifdef DEBUG_TREE
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		        "xmlStringGetNodeList: unterminated entity %30s\n", q);
 #endif
 	        return(ret);
@@ -838,7 +852,8 @@
 	}
 #if 0
 	else {
-	    fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlGetNodeListString : invalide node type %d\n",
 	            node->type);
 	}
 #endif
@@ -912,7 +927,8 @@
 	}
 #if 0
 	else {
-	    fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlGetNodeListString : invalide node type %d\n",
 	            node->type);
 	}
 #endif
@@ -936,7 +952,8 @@
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewProp : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -946,7 +963,8 @@
      */
     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewProp : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlAttr));
@@ -1004,7 +1022,8 @@
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewProp : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1014,7 +1033,8 @@
      */
     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewProp : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlAttr));
@@ -1073,7 +1093,8 @@
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewProp : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1083,7 +1104,8 @@
      */
     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewProp : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewProp : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlAttr));
@@ -1107,7 +1129,8 @@
     xmlAttrPtr next;
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreePropList : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreePropList : property == NULL\n");
 #endif
 	return;
     }
@@ -1128,7 +1151,8 @@
 xmlFreeProp(xmlAttrPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeProp : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeProp : property == NULL\n");
 #endif
 	return;
     }
@@ -1156,13 +1180,15 @@
     xmlAttrPtr tmp;
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlRemoveProp : cur == NULL\n");
 #endif
 	return(-1);
     }
     if (cur->parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlRemoveProp : cur->parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlRemoveProp : cur->parent == NULL\n");
 #endif
 	return(-1);
     }
@@ -1183,7 +1209,8 @@
         tmp = tmp->next;
     }
 #ifdef DEBUG_TREE
-    fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
+    xmlGenericError(xmlGenericErrorContext,
+	    "xmlRemoveProp : attribute not owned by its node\n");
 #endif
     return(-1);
 }
@@ -1202,7 +1229,8 @@
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewPI : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewPI : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1212,7 +1240,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewPI : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewPI : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1247,7 +1276,8 @@
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewNode : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewNode : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1257,7 +1287,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewNode : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewNode : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1345,7 +1376,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewDocFragment : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewDocFragment : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1371,7 +1403,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewText : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewText : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1411,14 +1444,16 @@
 
     if (parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewTextChild : parent == NULL\n");
 #endif
 	return(NULL);
     }
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewTextChild : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewTextChild : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1468,7 +1503,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewText : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewText : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1506,7 +1542,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewText : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewText : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1579,7 +1616,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewText : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewText : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1634,7 +1672,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewComment : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewComment : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1672,7 +1711,8 @@
      */
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
-        fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewCDataBlock : malloc failed\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -1733,14 +1773,16 @@
 
     if (parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewChild : parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewChild : parent == NULL\n");
 #endif
 	return(NULL);
     }
 
     if (name == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewChild : name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewChild : name == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1789,13 +1831,15 @@
 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNextSibling : cur == NULL\n");
 #endif
 	return(NULL);
     }
     if (elem == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNextSibling : elem == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1857,13 +1901,15 @@
 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddPrevSibling : cur == NULL\n");
 #endif
 	return(NULL);
     }
     if (elem == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddPrevSibling : elem == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1927,14 +1973,16 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddSibling : cur == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddSibling : cur == NULL\n");
 #endif
 	return(NULL);
     }
 
     if (elem == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddSibling : elem == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddSibling : elem == NULL\n");
 #endif
 	return(NULL);
     }
@@ -1994,14 +2042,16 @@
 
     if (parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddChild : parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddChild : parent == NULL\n");
 #endif
 	return(NULL);
     }
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddChild : child == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddChild : child == NULL\n");
 #endif
 	return(NULL);
     }
@@ -2009,7 +2059,8 @@
     if ((cur->doc != NULL) && (parent->doc != NULL) &&
         (cur->doc != parent->doc)) {
 #ifdef DEBUG_TREE
-	fprintf(stderr, "Elements moved to a different document\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"Elements moved to a different document\n");
 #endif
     }
 
@@ -2071,14 +2122,16 @@
 
     if (parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddChild : parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddChild : parent == NULL\n");
 #endif
 	return(NULL);
     }
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAddChild : child == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddChild : child == NULL\n");
 #endif
 	return(NULL);
     }
@@ -2086,7 +2139,8 @@
     if ((cur->doc != NULL) && (parent->doc != NULL) &&
         (cur->doc != parent->doc)) {
 #ifdef DEBUG_TREE
-	fprintf(stderr, "Elements moved to a different document\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"Elements moved to a different document\n");
 #endif
     }
 
@@ -2175,7 +2229,8 @@
 xmlGetLastChild(xmlNodePtr parent) {
     if (parent == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlGetLastChild : parent == NULL\n");
 #endif
 	return(NULL);
     }
@@ -2194,7 +2249,8 @@
     xmlNodePtr next;
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeNodeList : node == NULL\n");
 #endif
 	return;
     }
@@ -2216,7 +2272,8 @@
 xmlFreeNode(xmlNodePtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlFreeNode : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlFreeNode : node == NULL\n");
 #endif
 	return;
     }
@@ -2252,7 +2309,8 @@
 xmlUnlinkNode(xmlNodePtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlUnlinkNode : node == NULL\n");
 #endif
 	return;
     }
@@ -2283,7 +2341,8 @@
 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
     if (old == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlReplaceNode : old == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlReplaceNode : old == NULL\n");
 #endif
 	return(NULL);
     }
@@ -2339,7 +2398,8 @@
 	    break;
 	default:
 #ifdef DEBUG_TREE
-	    fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlCopyNamespace: unknown type %d\n", cur->type);
 #endif
 	    return(NULL);
     }
@@ -2465,7 +2525,8 @@
      */
     ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (ret == NULL) {
-        fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlStaticCopyNode : malloc failed\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlNode));
@@ -3087,7 +3148,8 @@
 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeSetContent : node == NULL\n");
 #endif
 	return;
     }
@@ -3170,7 +3232,8 @@
 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeSetContentLen : node == NULL\n");
 #endif
 	return;
     }
@@ -3251,7 +3314,8 @@
 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeAddContentLen : node == NULL\n");
 #endif
 	return;
     }
@@ -3334,7 +3398,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeAddContent : node == NULL\n");
 #endif
 	return;
     }
@@ -3391,7 +3456,8 @@
 	    if (ret == NULL) {
 	        ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlGetNsList : out of memory!\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlGetNsList : out of memory!\n");
 		    return(NULL);
 		}
 		ret[nbns] = NULL;
@@ -3406,7 +3472,8 @@
 		    ret = (xmlNsPtr *) xmlRealloc(ret,
 		                         (maxns + 1) * sizeof(xmlNsPtr));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlGetNsList : realloc failed!\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlGetNsList : realloc failed!\n");
 			return(NULL);
 		    }
 		}
@@ -3534,13 +3601,15 @@
 
     if (tree == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewReconciliedNs : tree == NULL\n");
 #endif
 	return(NULL);
     }
     if (ns == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNewReconciliedNs : ns == NULL\n");
 #endif
 	return(NULL);
     }
@@ -3609,13 +3678,15 @@
 		oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
 					       sizeof(xmlNsPtr));
 		if (oldNs == NULL) {
-		    fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlReconciliateNs : memory pbm\n");
 		    return(-1);
 		}
 		newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
 					       sizeof(xmlNsPtr));
 		if (newNs == NULL) {
-		    fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlReconciliateNs : memory pbm\n");
 		    xmlFree(oldNs);
 		    return(-1);
 		}
@@ -3640,14 +3711,16 @@
 			oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
 			                               sizeof(xmlNsPtr));
 		        if (oldNs == NULL) {
-			    fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+			    xmlGenericError(xmlGenericErrorContext,
+				    "xmlReconciliateNs : memory pbm\n");
 			    xmlFree(newNs);
 			    return(-1);
 			}
 			newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
 			                               sizeof(xmlNsPtr));
 		        if (newNs == NULL) {
-			    fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+			    xmlGenericError(xmlGenericErrorContext,
+				    "xmlReconciliateNs : memory pbm\n");
 			    xmlFree(oldNs);
 			    return(-1);
 			}
@@ -3672,13 +3745,15 @@
 		    oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
 						   sizeof(xmlNsPtr));
 		    if (oldNs == NULL) {
-			fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlReconciliateNs : memory pbm\n");
 			return(-1);
 		    }
 		    newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
 						   sizeof(xmlNsPtr));
 		    if (newNs == NULL) {
-			fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlReconciliateNs : memory pbm\n");
 			xmlFree(oldNs);
 			return(-1);
 		    }
@@ -3703,7 +3778,7 @@
 			    oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
 							   sizeof(xmlNsPtr));
 			    if (oldNs == NULL) {
-				fprintf(stderr,
+				xmlGenericError(xmlGenericErrorContext,
 				        "xmlReconciliateNs : memory pbm\n");
 				xmlFree(newNs);
 				return(-1);
@@ -3711,7 +3786,7 @@
 			    newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
 							   sizeof(xmlNsPtr));
 			    if (newNs == NULL) {
-				fprintf(stderr,
+				xmlGenericError(xmlGenericErrorContext,
 				        "xmlReconciliateNs : memory pbm\n");
 				xmlFree(oldNs);
 				return(-1);
@@ -4025,7 +4100,8 @@
     if ((node->type != XML_TEXT_NODE) &&
         (node->type != XML_CDATA_SECTION_NODE)) {
 #ifdef DEBUG_TREE
-	fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlTextConcat: node is not text nor cdata\n");
 #endif
         return;
     }
@@ -4056,7 +4132,8 @@
 
     ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
     if (ret == NULL) {
-	fprintf(stderr, "xmlBufferCreate : out of memory!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferCreate : out of memory!\n");
         return(NULL);
     }
     ret->use = 0;
@@ -4064,7 +4141,8 @@
     ret->alloc = xmlBufferAllocScheme;
     ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
     if (ret->content == NULL) {
-	fprintf(stderr, "xmlBufferCreate : out of memory!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferCreate : out of memory!\n");
 	xmlFree(ret);
         return(NULL);
     }
@@ -4085,7 +4163,8 @@
 
     ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
     if (ret == NULL) {
-        fprintf(stderr, "xmlBufferCreate : out of memory!\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferCreate : out of memory!\n");
         return(NULL);
     }
     ret->use = 0;
@@ -4094,7 +4173,8 @@
     if (ret->size){
         ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
         if (ret->content == NULL) {
-            fprintf(stderr, "xmlBufferCreate : out of memory!\n");
+            xmlGenericError(xmlGenericErrorContext,
+		    "xmlBufferCreate : out of memory!\n");
             xmlFree(ret);
             return(NULL);
         }
@@ -4116,7 +4196,8 @@
                              xmlBufferAllocationScheme scheme) {
     if (buf == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferSetAllocationScheme: buf == NULL\n");
 #endif
         return;
     }
@@ -4134,7 +4215,8 @@
 xmlBufferFree(xmlBufferPtr buf) {
     if (buf == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferFree: buf == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferFree: buf == NULL\n");
 #endif
 	return;
     }
@@ -4223,13 +4305,15 @@
 
     if (buf == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferDump: buf == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferDump: buf == NULL\n");
 #endif
 	return(0);
     }
     if (buf->content == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferDump: buf->content == NULL\n");
 #endif
 	return(0);
     }
@@ -4311,7 +4395,8 @@
 	rebuf = (xmlChar *) xmlRealloc(buf->content, 
 				       newSize * sizeof(xmlChar));
     if (rebuf == NULL) {
-        fprintf(stderr, "xmlBufferAdd : out of memory!\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd : out of memory!\n");
         return 0;
     }
     buf->content = rebuf;
@@ -4335,13 +4420,15 @@
 
     if (str == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferAdd: str == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd: str == NULL\n");
 #endif
 	return;
     }
     if (len < -1) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferAdd: len < 0\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd: len < 0\n");
 #endif
 	return;
     }
@@ -4355,7 +4442,8 @@
     needSize = buf->use + len + 2;
     if (needSize > buf->size){
         if (!xmlBufferResize(buf, needSize)){
-            fprintf(stderr, "xmlBufferAdd : out of memory!\n");
+            xmlGenericError(xmlGenericErrorContext,
+		    "xmlBufferAdd : out of memory!\n");
             return;
         }
     }
@@ -4380,13 +4468,15 @@
 
     if (str == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferAdd: str == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd: str == NULL\n");
 #endif
 	return;
     }
     if (len < -1) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferAdd: len < 0\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd: len < 0\n");
 #endif
 	return;
     }
@@ -4400,7 +4490,8 @@
     needSize = buf->use + len + 2;
     if (needSize > buf->size){
         if (!xmlBufferResize(buf, needSize)){
-            fprintf(stderr, "xmlBufferAddHead : out of memory!\n");
+            xmlGenericError(xmlGenericErrorContext,
+		    "xmlBufferAddHead : out of memory!\n");
             return;
         }
     }
@@ -4437,14 +4528,16 @@
 
     if (str == NULL) {
 #ifdef DEBUG_BUFFER
-        fprintf(stderr, "xmlBufferAdd: str == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlBufferAdd: str == NULL\n");
 #endif
 	return;
     }
     for (cur = str;*cur != 0;cur++) {
         if (buf->use  + 10 >= buf->size) {
             if (!xmlBufferResize(buf, buf->use+10)){
-                fprintf(stderr, "xmlBufferCCat : out of memory!\n");
+                xmlGenericError(xmlGenericErrorContext,
+			"xmlBufferCCat : out of memory!\n");
                 return;
             }
         }
@@ -4493,7 +4586,7 @@
     if (xmlStrchr(string, '"')) {
         if (xmlStrchr(string, '\'')) {
 #ifdef DEBUG_BUFFER
-	    fprintf(stderr,
+	    xmlGenericError(xmlGenericErrorContext,
  "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
 #endif
 	}
@@ -4534,7 +4627,8 @@
 xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlGlobalNsDump : Ns == NULL\n");
 #endif
 	return;
     }
@@ -4579,7 +4673,8 @@
 xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNsDump : Ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNsDump : Ns == NULL\n");
 #endif
 	return;
     }
@@ -4622,7 +4717,8 @@
 xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
     if (dtd == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlDtdDump : no internal subset\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlDtdDump : no internal subset\n");
 #endif
 	return;
     }
@@ -4671,7 +4767,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAttrDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAttrDump : property == NULL\n");
 #endif
 	return;
     }
@@ -4703,7 +4800,8 @@
 xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAttrListDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAttrListDump : property == NULL\n");
 #endif
 	return;
     }
@@ -4732,7 +4830,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeListDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeListDump : node == NULL\n");
 #endif
 	return;
     }
@@ -4767,7 +4866,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeDump : node == NULL\n");
 #endif
 	return;
     }
@@ -4929,13 +5029,15 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlElemDump : cur == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlElemDump : cur == NULL\n");
 #endif
 	return;
     }
     if (doc == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlElemDump : doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlElemDump : doc == NULL\n");
 #endif
     }
     buf = xmlBufferCreate();
@@ -4945,7 +5047,8 @@
 #ifdef LIBXML_HTML_ENABLED
         htmlNodeDump(buf, doc, cur);
 #else	
-	printf("HTML support not compiled in\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"HTML support not compiled in\n");
 #endif /* LIBXML_HTML_ENABLED */
     } else
         xmlNodeDump(buf, doc, cur, 0, 1);
@@ -5020,7 +5123,8 @@
 xmlGlobalNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlGlobalNsDump : Ns == NULL\n");
 #endif
 	return;
     }
@@ -5065,7 +5169,8 @@
 xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNsDump : Ns == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNsDump : Ns == NULL\n");
 #endif
 	return;
     }
@@ -5109,7 +5214,8 @@
 xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
     if (dtd == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlDtdDump : no internal subset\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlDtdDump : no internal subset\n");
 #endif
 	return;
     }
@@ -5160,7 +5266,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAttrDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAttrDump : property == NULL\n");
 #endif
 	return;
     }
@@ -5194,7 +5301,8 @@
 	              xmlAttrPtr cur, const char *encoding) {
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlAttrListDump : property == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAttrListDump : property == NULL\n");
 #endif
 	return;
     }
@@ -5224,7 +5332,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeListDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeListDump : node == NULL\n");
 #endif
 	return;
     }
@@ -5260,7 +5369,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlNodeDump : node == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlNodeDump : node == NULL\n");
 #endif
 	return;
     }
@@ -5495,7 +5605,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlDocDumpMemory : document == NULL\n");
 #endif
 	*mem = NULL;
 	*size = 0;
@@ -5585,7 +5696,8 @@
 
     if (cur == NULL) {
 #ifdef DEBUG_TREE
-        fprintf(stderr, "xmlDocDump : document == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlDocDump : document == NULL\n");
 #endif
 	return(-1);
     }
@@ -5597,7 +5709,8 @@
 	enc = xmlParseCharEncoding(encoding);
 
 	if (cur->charset != XML_CHAR_ENCODING_UTF8) {
-	    fprintf(stderr, "xmlDocDump: document not in UTF8\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlDocDump: document not in UTF8\n");
 	    return(-1);
 	}
 	if (enc != XML_CHAR_ENCODING_UTF8) {
@@ -5657,7 +5770,8 @@
 
 	enc = xmlParseCharEncoding(encoding);
 	if (cur->charset != XML_CHAR_ENCODING_UTF8) {
-	    fprintf(stderr, "xmlSaveFileEnc: document not in UTF8\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlSaveFileEnc: document not in UTF8\n");
 	    return(-1);
 	}
 	if (enc != XML_CHAR_ENCODING_UTF8) {
@@ -5713,7 +5827,8 @@
 	enc = xmlParseCharEncoding(encoding);
 
 	if (cur->charset != XML_CHAR_ENCODING_UTF8) {
-	    fprintf(stderr, "xmlSaveFile: document not in UTF8\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlSaveFile: document not in UTF8\n");
 	    return(-1);
 	}
 	if (enc != XML_CHAR_ENCODING_UTF8) {
diff --git a/uri.c b/uri.c
index cb1c215..79c78e2 100644
--- a/uri.c
+++ b/uri.c
@@ -20,6 +20,7 @@
 
 #include <libxml/xmlmemory.h>
 #include <libxml/uri.h>
+#include <libxml/xmlerror.h>
 
 /*
  * alpha    = lowalpha | upalpha
@@ -181,7 +182,8 @@
 
     ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
     if (ret == NULL) {
-	fprintf(stderr, "xmlCreateURI: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCreateURI: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlURI));
@@ -209,7 +211,8 @@
     max = 80;
     ret = (xmlChar *) xmlMalloc((max + 1) * sizeof(xmlChar));
     if (ret == NULL) {
-	fprintf(stderr, "xmlSaveUri: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlSaveUri: out of memory\n");
 	return(NULL);
     }
     len = 0;
@@ -221,7 +224,8 @@
 		max *= 2;
 		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -231,7 +235,8 @@
 	    max *= 2;
 	    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 	    if (ret == NULL) {
-		fprintf(stderr, "xmlSaveUri: out of memory\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"xmlSaveUri: out of memory\n");
 		return(NULL);
 	    }
 	}
@@ -244,7 +249,8 @@
 		max *= 2;
 		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -265,7 +271,8 @@
 	    max *= 2;
 	    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 	    if (ret == NULL) {
-		fprintf(stderr, "xmlSaveUri: out of memory\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"xmlSaveUri: out of memory\n");
 		return(NULL);
 	    }
 	}
@@ -276,7 +283,8 @@
 		max *= 2;
 		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -287,15 +295,18 @@
 		while (*p != 0) {
 		    if (len + 3 >= max) {
 			max *= 2;
-			ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+			ret = (xmlChar *) xmlRealloc(ret,
+				(max + 1) * sizeof(xmlChar));
 			if (ret == NULL) {
-			    fprintf(stderr, "xmlSaveUri: out of memory\n");
+			    xmlGenericError(xmlGenericErrorContext,
+				    "xmlSaveUri: out of memory\n");
 			    return(NULL);
 			}
 		    }
 		    if ((IS_UNRESERVED(*(p))) ||
-			((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) ||
-			((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
+			((*(p) == ';')) || ((*(p) == ':')) ||
+			((*(p) == '&')) || ((*(p) == '=')) ||
+			((*(p) == '+')) || ((*(p) == '$')) ||
 			((*(p) == ',')))
 			ret[len++] = *p++;
 		    else {
@@ -308,9 +319,11 @@
 		}
 		if (len + 3 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -320,9 +333,11 @@
 	    while (*p != 0) {
 		if (len >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -331,9 +346,11 @@
 	    if (uri->port > 0) {
 		if (len + 10 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -342,9 +359,11 @@
 	} else if (uri->authority != NULL) {
 	    if (len + 3 >= max) {
 		max *= 2;
-		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		ret = (xmlChar *) xmlRealloc(ret,
+			(max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -354,9 +373,11 @@
 	    while (*p != 0) {
 		if (len + 3 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -376,9 +397,11 @@
 	} else if (uri->scheme != NULL) {
 	    if (len + 3 >= max) {
 		max *= 2;
-		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		ret = (xmlChar *) xmlRealloc(ret,
+			(max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -390,9 +413,11 @@
 	    while (*p != 0) {
 		if (len + 3 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -413,9 +438,11 @@
 	if (uri->query != NULL) {
 	    if (len + 3 >= max) {
 		max *= 2;
-		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		ret = (xmlChar *) xmlRealloc(ret,
+			(max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -424,9 +451,11 @@
 	    while (*p != 0) {
 		if (len + 3 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -444,9 +473,11 @@
 	if (uri->fragment != NULL) {
 	    if (len + 3 >= max) {
 		max *= 2;
-		ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		ret = (xmlChar *) xmlRealloc(ret,
+			(max + 1) * sizeof(xmlChar));
 		if (ret == NULL) {
-		    fprintf(stderr, "xmlSaveUri: out of memory\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "xmlSaveUri: out of memory\n");
 		    return(NULL);
 		}
 	    }
@@ -455,9 +486,11 @@
 	    while (*p != 0) {
 		if (len + 3 >= max) {
 		    max *= 2;
-		    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
+		    ret = (xmlChar *) xmlRealloc(ret,
+			    (max + 1) * sizeof(xmlChar));
 		    if (ret == NULL) {
-			fprintf(stderr, "xmlSaveUri: out of memory\n");
+			xmlGenericError(xmlGenericErrorContext,
+				"xmlSaveUri: out of memory\n");
 			return(NULL);
 		    }
 		}
@@ -476,7 +509,8 @@
 	    max *= 2;
 	    ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
 	    if (ret == NULL) {
-		fprintf(stderr, "xmlSaveUri: out of memory\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"xmlSaveUri: out of memory\n");
 		return(NULL);
 	    }
 	}
@@ -577,7 +611,8 @@
     if (target == NULL) {
 	ret = (char *) xmlMalloc(len + 1);
 	if (ret == NULL) {
-	    fprintf(stderr, "xmlURIUnescapeString: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlURIUnescapeString: out of memory\n");
 	    return(NULL);
 	}
     } else
@@ -691,7 +726,8 @@
     while (IS_SCHEME(*cur)) cur++;
     if (uri != NULL) {
 	if (uri->scheme != NULL) xmlFree(uri->scheme);
-	uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL); /* !!! strndup */
+	/* !!! strndup */
+	uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL);
     }
     *str = cur;
     return(0);
@@ -954,7 +990,8 @@
 	}
         path = (char *) xmlMalloc(len + 1);
 	if (path == NULL) {
-	    fprintf(stderr, "xmlParseURIPathSegments: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlParseURIPathSegments: out of memory\n");
 	    *str = cur;
 	    return(-1);
 	}
@@ -1557,7 +1594,8 @@
 	len += strlen(bas->path);
     res->path = (char *) xmlMalloc(len);
     if (res->path == NULL) {
-	fprintf(stderr, "xmlBuildURI: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlBuildURI: out of memory\n");
 	goto done;
     }
     res->path[0] = 0;
diff --git a/valid.c b/valid.c
index fe64bc0..8cbfd53 100644
--- a/valid.c
+++ b/valid.c
@@ -25,6 +25,7 @@
 #include <libxml/valid.h>
 #include <libxml/parser.h>
 #include <libxml/parserInternals.h>
+#include <libxml/xmlerror.h>
 
 /*
  * Generic function for accessing stacks in the Validity Context
@@ -37,7 +38,8 @@
         ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,		\
 	             ctxt->name##Max * sizeof(ctxt->name##Tab[0]));	\
         if (ctxt->name##Tab == NULL) {					\
-	    fprintf(stderr, "realloc failed !\n");			\
+	    xmlGenericError(xmlGenericErrorContext,			\
+		    "realloc failed !\n");				\
 	    return(0);							\
 	}								\
     }									\
@@ -65,59 +67,59 @@
 #ifdef DEBUG_VALID_ALGO
 void xmlValidPrintNodeList(xmlNodePtr cur) {
     if (cur == NULL)
-	fprintf(stderr, "null ");
+	xmlGenericError(xmlGenericErrorContext, "null ");
     while (cur != NULL) {
 	switch (cur->type) {
 	    case XML_ELEMENT_NODE:
-		fprintf(stderr, "%s ", cur->name);
+		xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
 		break;
 	    case XML_TEXT_NODE:
-		fprintf(stderr, "text ");
+		xmlGenericError(xmlGenericErrorContext, "text ");
 		break;
 	    case XML_CDATA_SECTION_NODE:
-		fprintf(stderr, "cdata ");
+		xmlGenericError(xmlGenericErrorContext, "cdata ");
 		break;
 	    case XML_ENTITY_REF_NODE:
-		fprintf(stderr, "&%s; ", cur->name);
+		xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
 		break;
 	    case XML_PI_NODE:
-		fprintf(stderr, "pi(%s) ", cur->name);
+		xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
 		break;
 	    case XML_COMMENT_NODE:
-		fprintf(stderr, "comment ");
+		xmlGenericError(xmlGenericErrorContext, "comment ");
 		break;
 	    case XML_ATTRIBUTE_NODE:
-		fprintf(stderr, "?attr? ");
+		xmlGenericError(xmlGenericErrorContext, "?attr? ");
 		break;
 	    case XML_ENTITY_NODE:
-		fprintf(stderr, "?ent? ");
+		xmlGenericError(xmlGenericErrorContext, "?ent? ");
 		break;
 	    case XML_DOCUMENT_NODE:
-		fprintf(stderr, "?doc? ");
+		xmlGenericError(xmlGenericErrorContext, "?doc? ");
 		break;
 	    case XML_DOCUMENT_TYPE_NODE:
-		fprintf(stderr, "?doctype? ");
+		xmlGenericError(xmlGenericErrorContext, "?doctype? ");
 		break;
 	    case XML_DOCUMENT_FRAG_NODE:
-		fprintf(stderr, "?frag? ");
+		xmlGenericError(xmlGenericErrorContext, "?frag? ");
 		break;
 	    case XML_NOTATION_NODE:
-		fprintf(stderr, "?nota? ");
+		xmlGenericError(xmlGenericErrorContext, "?nota? ");
 		break;
 	    case XML_HTML_DOCUMENT_NODE:
-		fprintf(stderr, "?html? ");
+		xmlGenericError(xmlGenericErrorContext, "?html? ");
 		break;
 	    case XML_DTD_NODE:
-		fprintf(stderr, "?dtd? ");
+		xmlGenericError(xmlGenericErrorContext, "?dtd? ");
 		break;
 	    case XML_ELEMENT_DECL:
-		fprintf(stderr, "?edecl? ");
+		xmlGenericError(xmlGenericErrorContext, "?edecl? ");
 		break;
 	    case XML_ATTRIBUTE_DECL:
-		fprintf(stderr, "?adecl? ");
+		xmlGenericError(xmlGenericErrorContext, "?adecl? ");
 		break;
 	    case XML_ENTITY_DECL:
-		fprintf(stderr, "?entdecl? ");
+		xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
 		break;
 	}
 	cur = cur->next;
@@ -128,11 +130,11 @@
     char expr[1000];
 
     expr[0] = 0;
-    fprintf(stderr, "valid: ");
+    xmlGenericError(xmlGenericErrorContext, "valid: ");
     xmlValidPrintNodeList(cur);
-    fprintf(stderr, "against ");
+    xmlGenericError(xmlGenericErrorContext, "against ");
     xmlSprintfElementContent(expr, cont, 0);
-    fprintf(stderr, "%s\n", expr);
+    xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
 }
 
 #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
@@ -233,23 +235,27 @@
     switch(type) {
 	case XML_ELEMENT_CONTENT_ELEMENT:
 	    if (name == NULL) {
-	        fprintf(stderr, "xmlNewElementContent : name == NULL !\n");
+	        xmlGenericError(xmlGenericErrorContext,
+			"xmlNewElementContent : name == NULL !\n");
 	    }
 	    break;
         case XML_ELEMENT_CONTENT_PCDATA:
 	case XML_ELEMENT_CONTENT_SEQ:
 	case XML_ELEMENT_CONTENT_OR:
 	    if (name != NULL) {
-	        fprintf(stderr, "xmlNewElementContent : name != NULL !\n");
+	        xmlGenericError(xmlGenericErrorContext,
+			"xmlNewElementContent : name != NULL !\n");
 	    }
 	    break;
 	default:
-	    fprintf(stderr, "xmlNewElementContent: unknown type %d\n", type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlNewElementContent: unknown type %d\n", type);
 	    return(NULL);
     }
     ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
     if (ret == NULL) {
-	fprintf(stderr, "xmlNewElementContent : out of memory!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlNewElementContent : out of memory!\n");
 	return(NULL);
     }
     ret->type = type;
@@ -277,7 +283,8 @@
     if (cur == NULL) return(NULL);
     ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
     if (ret == NULL) {
-        fprintf(stderr, "xmlCopyElementContent : out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlCopyElementContent : out of memory\n");
 	return(NULL);
     }
     ret->ocur = cur->ocur;
@@ -302,7 +309,8 @@
 	case XML_ELEMENT_CONTENT_OR:
 	    break;
 	default:
-	    fprintf(stderr, "xmlFreeElementContent : type %d\n", cur->type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlFreeElementContent : type %d\n", cur->type);
 	    return;
     }
     if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
@@ -357,7 +365,8 @@
 		xmlDumpElementContent(buf, content->c2, 0);
 	    break;
 	default:
-	    fprintf(stderr, "xmlDumpElementContent: unknown type %d\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlDumpElementContent: unknown type %d\n",
 	            content->type);
     }
     if (glob)
@@ -498,44 +507,47 @@
     xmlChar *ns, *uqname;
 
     if (dtd == NULL) {
-        fprintf(stderr, "xmlAddElementDecl: dtd == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddElementDecl: dtd == NULL\n");
 	return(NULL);
     }
     if (name == NULL) {
-        fprintf(stderr, "xmlAddElementDecl: name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddElementDecl: name == NULL\n");
 	return(NULL);
     }
     switch (type) {
         case XML_ELEMENT_TYPE_EMPTY:
 	    if (content != NULL) {
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		        "xmlAddElementDecl: content != NULL for EMPTY\n");
 		return(NULL);
 	    }
 	    break;
 	case XML_ELEMENT_TYPE_ANY:
 	    if (content != NULL) {
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		        "xmlAddElementDecl: content != NULL for ANY\n");
 		return(NULL);
 	    }
 	    break;
 	case XML_ELEMENT_TYPE_MIXED:
 	    if (content == NULL) {
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		        "xmlAddElementDecl: content == NULL for MIXED\n");
 		return(NULL);
 	    }
 	    break;
 	case XML_ELEMENT_TYPE_ELEMENT:
 	    if (content == NULL) {
-	        fprintf(stderr,
+	        xmlGenericError(xmlGenericErrorContext,
 		        "xmlAddElementDecl: content == NULL for ELEMENT\n");
 		return(NULL);
 	    }
 	    break;
 	default:
-	    fprintf(stderr, "xmlAddElementDecl: unknown type %d\n", type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlAddElementDecl: unknown type %d\n", type);
 	    return(NULL);
     }
 
@@ -555,13 +567,15 @@
 	dtd->elements = (void *) table;
     }
     if (table == NULL) {
-	fprintf(stderr, "xmlAddElementDecl: Table creation failed!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddElementDecl: Table creation failed!\n");
         return(NULL);
     }
 
     ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddElementDecl: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddElementDecl: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlElement));
@@ -633,7 +647,8 @@
 
     cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
     if (cur == NULL) {
-	fprintf(stderr, "xmlCopyElement: out of memory !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCopyElement: out of memory !\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlElement));
@@ -703,7 +718,7 @@
 	    xmlBufferWriteChar(buf, ">\n");
 	    break;
 	default:
-	    fprintf(stderr,
+	    xmlGenericError(xmlGenericErrorContext,
 		"xmlDumpElementDecl: internal: unknown type %d\n",
 		    elem->etype);
     }
@@ -736,7 +751,8 @@
 
     ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
     if (ret == NULL) {
-        fprintf(stderr, "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
 	        (long)sizeof(xmlEnumeration));
         return(NULL);
     }
@@ -851,11 +867,13 @@
     xmlAttributeTablePtr table;
 
     if (dtd == NULL) {
-        fprintf(stderr, "xmlScanAttributeDecl: dtd == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlScanAttributeDecl: dtd == NULL\n");
 	return(NULL);
     }
     if (elem == NULL) {
-        fprintf(stderr, "xmlScanAttributeDecl: elem == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlScanAttributeDecl: elem == NULL\n");
 	return(NULL);
     }
     table = (xmlAttributeTablePtr) dtd->attributes;
@@ -950,17 +968,20 @@
     xmlElementPtr elemDef;
 
     if (dtd == NULL) {
-        fprintf(stderr, "xmlAddAttributeDecl: dtd == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddAttributeDecl: dtd == NULL\n");
 	xmlFreeEnumeration(tree);
 	return(NULL);
     }
     if (name == NULL) {
-        fprintf(stderr, "xmlAddAttributeDecl: name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddAttributeDecl: name == NULL\n");
 	xmlFreeEnumeration(tree);
 	return(NULL);
     }
     if (elem == NULL) {
-        fprintf(stderr, "xmlAddAttributeDecl: elem == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddAttributeDecl: elem == NULL\n");
 	xmlFreeEnumeration(tree);
 	return(NULL);
     }
@@ -989,7 +1010,8 @@
         case XML_ATTRIBUTE_NOTATION:
 	    break;
 	default:
-	    fprintf(stderr, "xmlAddAttributeDecl: unknown type %d\n", type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlAddAttributeDecl: unknown type %d\n", type);
 	    xmlFreeEnumeration(tree);
 	    return(NULL);
     }
@@ -1009,14 +1031,16 @@
 	dtd->attributes = (void *) table;
     }
     if (table == NULL) {
-	fprintf(stderr, "xmlAddAttributeDecl: Table creation failed!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddAttributeDecl: Table creation failed!\n");
         return(NULL);
     }
 
 
     ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddAttributeDecl: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddAttributeDecl: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlAttribute));
@@ -1104,7 +1128,8 @@
 
     cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
     if (cur == NULL) {
-	fprintf(stderr, "xmlCopyAttribute: out of memory !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCopyAttribute: out of memory !\n");
 	return(NULL);
     }
     memset(cur, 0, sizeof(xmlAttribute));
@@ -1186,7 +1211,7 @@
 	    xmlDumpEnumeration(buf, attr->tree);
 	    break;
 	default:
-	    fprintf(stderr,
+	    xmlGenericError(xmlGenericErrorContext,
 		"xmlDumpAttributeTable: internal: unknown type %d\n",
 		    attr->atype);
     }
@@ -1203,7 +1228,7 @@
 	    xmlBufferWriteChar(buf, " #FIXED");
 	    break;
 	default:
-	    fprintf(stderr,
+	    xmlGenericError(xmlGenericErrorContext,
 		"xmlDumpAttributeTable: internal: unknown default %d\n",
 		    attr->def);
     }
@@ -1283,15 +1308,18 @@
     xmlNotationTablePtr table;
 
     if (dtd == NULL) {
-        fprintf(stderr, "xmlAddNotationDecl: dtd == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNotationDecl: dtd == NULL\n");
 	return(NULL);
     }
     if (name == NULL) {
-        fprintf(stderr, "xmlAddNotationDecl: name == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNotationDecl: name == NULL\n");
 	return(NULL);
     }
     if ((PublicID == NULL) && (SystemID == NULL)) {
-        fprintf(stderr, "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
     }
 
     /*
@@ -1301,13 +1329,15 @@
     if (table == NULL) 
         dtd->notations = table = xmlCreateNotationTable();
     if (table == NULL) {
-	fprintf(stderr, "xmlAddNotationDecl: Table creation failed!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNotationDecl: Table creation failed!\n");
         return(NULL);
     }
 
     ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddNotationDecl: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddNotationDecl: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0, sizeof(xmlNotation));
@@ -1326,7 +1356,7 @@
      * Check the DTD for previous declarations of the ATTLIST
      */
     if (xmlHashAddEntry(table, name, ret)) {
-	fprintf(stderr,
+	xmlGenericError(xmlGenericErrorContext,
 		"xmlAddNotationDecl: %s already defined\n", name);
 	xmlFreeNotation(ret);
 	return(NULL);
@@ -1359,7 +1389,8 @@
 
     cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
     if (cur == NULL) {
-	fprintf(stderr, "xmlCopyNotation: out of memory !\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlCopyNotation: out of memory !\n");
 	return(NULL);
     }
     if (nota->name != NULL)
@@ -1479,15 +1510,18 @@
     xmlIDTablePtr table;
 
     if (doc == NULL) {
-        fprintf(stderr, "xmlAddIDDecl: doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddIDDecl: doc == NULL\n");
 	return(NULL);
     }
     if (value == NULL) {
-        fprintf(stderr, "xmlAddIDDecl: value == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddIDDecl: value == NULL\n");
 	return(NULL);
     }
     if (attr == NULL) {
-        fprintf(stderr, "xmlAddIDDecl: attr == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddIDDecl: attr == NULL\n");
 	return(NULL);
     }
 
@@ -1498,13 +1532,15 @@
     if (table == NULL) 
         doc->ids = table = xmlCreateIDTable();
     if (table == NULL) {
-	fprintf(stderr, "xmlAddID: Table creation failed!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddID: Table creation failed!\n");
         return(NULL);
     }
 
     ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddID: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddID: out of memory\n");
 	return(NULL);
     }
 
@@ -1625,12 +1661,12 @@
     xmlIDPtr id;
 
     if (doc == NULL) {
-        fprintf(stderr, "xmlGetID: doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
 	return(NULL);
     }
 
     if (ID == NULL) {
-        fprintf(stderr, "xmlGetID: ID == NULL\n");
+        xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
 	return(NULL);
     }
 
@@ -1695,15 +1731,18 @@
     xmlRefTablePtr table;
 
     if (doc == NULL) {
-        fprintf(stderr, "xmlAddRefDecl: doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddRefDecl: doc == NULL\n");
 	return(NULL);
     }
     if (value == NULL) {
-        fprintf(stderr, "xmlAddRefDecl: value == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddRefDecl: value == NULL\n");
 	return(NULL);
     }
     if (attr == NULL) {
-        fprintf(stderr, "xmlAddRefDecl: attr == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAddRefDecl: attr == NULL\n");
 	return(NULL);
     }
 
@@ -1714,13 +1753,15 @@
     if (table == NULL) 
         doc->refs = table = xmlCreateRefTable();
     if (table == NULL) {
-	fprintf(stderr, "xmlAddRef: Table creation failed!\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddRef: Table creation failed!\n");
         return(NULL);
     }
 
     ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
     if (ret == NULL) {
-	fprintf(stderr, "xmlAddRef: out of memory\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlAddRef: out of memory\n");
 	return(NULL);
     }
 
@@ -1834,12 +1875,12 @@
     xmlRefTablePtr table;
 
     if (doc == NULL) {
-        fprintf(stderr, "xmlGetRef: doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext, "xmlGetRef: doc == NULL\n");
 	return(NULL);
     }
 
     if (Ref == NULL) {
-        fprintf(stderr, "xmlGetRef: Ref == NULL\n");
+        xmlGenericError(xmlGenericErrorContext, "xmlGetRef: Ref == NULL\n");
 	return(NULL);
     }
 
@@ -3350,7 +3391,8 @@
 				(cont->c1 == NULL) ||
 				(cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
 				/* Internal error !!! */
-				fprintf(stderr, "Internal: MIXED struct bad\n");
+				xmlGenericError(xmlGenericErrorContext,
+					"Internal: MIXED struct bad\n");
 				break;
 			    }
 			    cont = cont->c2;
@@ -3370,7 +3412,8 @@
 			    (cont->c1 == NULL) ||
 			    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
 			    /* Internal error !!! */
-			    fprintf(stderr, "Internal: MIXED struct bad\n");
+			    xmlGenericError(xmlGenericErrorContext,
+				    "Internal: MIXED struct bad\n");
 			    break;
 			}
 			cont = cont->c2;
@@ -3648,7 +3691,8 @@
     xmlRefTablePtr table;
 
     if (doc == NULL) {
-        fprintf(stderr, "xmlValidateDocumentFinal: doc == NULL\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlValidateDocumentFinal: doc == NULL\n");
 	return(0);
     }
 
diff --git a/xmlIO.c b/xmlIO.c
index 667b3dd..31cd913 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -40,6 +40,7 @@
 #include <libxml/xmlIO.h>
 #include <libxml/nanohttp.h>
 #include <libxml/nanoftp.h>
+#include <libxml/xmlerror.h>
 
 /* #define VERBOSE_FAILURE */
 /* #define DEBUG_EXTERNAL_ENTITIES */
@@ -728,7 +729,8 @@
 
     ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
     if (ret == NULL) {
-        fprintf(stderr, "xmlAllocParserInputBuffer : out of memory!\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAllocParserInputBuffer : out of memory!\n");
 	return(NULL);
     }
     memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
@@ -764,7 +766,8 @@
 
     ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
     if (ret == NULL) {
-        fprintf(stderr, "xmlAllocOutputBuffer : out of memory!\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlAllocOutputBuffer : out of memory!\n");
 	return(NULL);
     }
     memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
@@ -1190,7 +1193,8 @@
  *         in case of error.
  */
 int
-xmlParserInputBufferPush(xmlParserInputBufferPtr in, int len, const char *buf) {
+xmlParserInputBufferPush(xmlParserInputBufferPtr in,
+	                 int len, const char *buf) {
     int nbchars = 0;
 
     if (len < 0) return(0);
@@ -1208,7 +1212,8 @@
 	 */
 	nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
 	if (nbchars < 0) {
-	    fprintf(stderr, "xmlParserInputBufferPush: encoder error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlParserInputBufferPush: encoder error\n");
 	    return(-1);
 	}
     } else {
@@ -1216,7 +1221,8 @@
         xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars);
     }
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "I/O: pushed %d chars, buffer %d/%d\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "I/O: pushed %d chars, buffer %d/%d\n",
             nbchars, in->buffer->use, in->buffer->size);
 #endif
     return(nbchars);
@@ -1248,7 +1254,8 @@
         len = MINLEN;
     buffree = in->buffer->size - in->buffer->use;
     if (buffree <= 0) {
-        fprintf(stderr, "xmlParserInputBufferGrow : buffer full !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInputBufferGrow : buffer full !\n");
 	return(0);
     }
     if (len > buffree) 
@@ -1256,7 +1263,8 @@
 
     buffer = (char *) xmlMalloc((len + 1) * sizeof(char));
     if (buffer == NULL) {
-        fprintf(stderr, "xmlParserInputBufferGrow : out of memory !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInputBufferGrow : out of memory !\n");
 	return(-1);
     }
 
@@ -1266,7 +1274,8 @@
     if (in->readcallback != NULL) {
 	res = in->readcallback(in->context, &buffer[0], len);
     } else {
-        fprintf(stderr, "xmlParserInputBufferGrow : no input !\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlParserInputBufferGrow : no input !\n");
 	xmlFree(buffer);
 	return(-1);
     }
@@ -1290,7 +1299,8 @@
 	 */
 	nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
 	if (nbchars < 0) {
-	    fprintf(stderr, "xmlParserInputBufferGrow: encoder error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlParserInputBufferGrow: encoder error\n");
 	    return(-1);
 	}
     } else {
@@ -1299,7 +1309,8 @@
         xmlBufferAdd(in->buffer, (xmlChar *) buffer, nbchars);
     }
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "I/O: read %d chars, buffer %d/%d\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    "I/O: read %d chars, buffer %d/%d\n",
             nbchars, in->buffer->use, in->buffer->size);
 #endif
     xmlFree(buffer);
@@ -1367,7 +1378,8 @@
 	 */
 	nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
 	if (nbchars < 0) {
-	    fprintf(stderr, "xmlOutputBufferWrite: encoder error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlOutputBufferWrite: encoder error\n");
 	    return(-1);
 	}
 	nbchars = out->conv->use;
@@ -1393,13 +1405,15 @@
 	    xmlBufferShrink(out->buffer, nbchars);
     }
     if (ret < 0) {
-        fprintf(stderr, "I/O: error %d writing %d bytes\n", ret, nbchars);
+        xmlGenericError(xmlGenericErrorContext,
+		"I/O: error %d writing %d bytes\n", ret, nbchars);
 	return(ret);
     }
     out->written += ret;
 
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "I/O: wrote %d chars\n", ret);
+    xmlGenericError(xmlGenericErrorContext,
+	    "I/O: wrote %d chars\n", ret);
 #endif
     return(nbchars);
 }
@@ -1451,7 +1465,8 @@
 	 */
 	nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
 	if (nbchars < 0) {
-	    fprintf(stderr, "xmlOutputBufferWrite: encoder error\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlOutputBufferWrite: encoder error\n");
 	    return(-1);
 	}
     }
@@ -1471,13 +1486,15 @@
 	    xmlBufferShrink(out->buffer, ret);
     }
     if (ret < 0) {
-        fprintf(stderr, "I/O: error %d flushing %d bytes\n", ret, nbchars);
+        xmlGenericError(xmlGenericErrorContext,
+		"I/O: error %d flushing %d bytes\n", ret, nbchars);
 	return(ret);
     }
     out->written += ret;
 
 #ifdef DEBUG_INPUT
-    fprintf(stderr, "I/O: flushed %d chars\n", ret);
+    xmlGenericError(xmlGenericErrorContext,
+	    "I/O: flushed %d chars\n", ret);
 #endif
     return(ret);
 }
@@ -1548,19 +1565,20 @@
     xmlParserInputPtr ret = NULL;
 
 #ifdef DEBUG_EXTERNAL_ENTITIES
-    fprintf(stderr, "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
+    xmlGenericError(xmlGenericErrorContext,
+	    "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
 #endif
     if (URL == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
-	    ctxt->sax->warning(ctxt, "failed to load external entity \"%s\"\n",
-	                       ID);
+	    ctxt->sax->warning(ctxt,
+		    "failed to load external entity \"%s\"\n", ID);
         return(NULL);
     }
     ret = xmlNewInputFromFile(ctxt, URL);
     if (ret == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
-	    ctxt->sax->warning(ctxt, "failed to load external entity \"%s\"\n",
-	                       URL);
+	    ctxt->sax->warning(ctxt,
+		    "failed to load external entity \"%s\"\n", URL);
     }
     return(ret);
 }
diff --git a/xmlerror.h b/xmlerror.h
index 25d9db0..fa675ab 100644
--- a/xmlerror.h
+++ b/xmlerror.h
@@ -135,6 +135,30 @@
     XML_ERR_URI_FRAGMENT /* 92 */
 }xmlParserErrors;
 
+/*
+ * Signature of the function to use when there is an error and
+ * no parsing or validity context available 
+ */
+typedef void (*xmlGenericErrorFunc) (void *ctx, const char *msg, ...);
+
+/*
+ * Those are the default error function and associated context to use
+ * when when there is an error and no parsing or validity context available
+ */
+
+extern xmlGenericErrorFunc xmlGenericError;
+extern void *xmlGenericErrorContext;
+
+/*
+ * Use the following function to reset the two previous global variables.
+ */
+void	xmlSetGenericErrorFunc	(void *ctx,
+				 xmlGenericErrorFunc handler);
+
+/*
+ * Default message routines used by SAX and Valid context for error
+ * and warning reporting
+ */
 void	xmlParserError		(void *ctx,
 				 const char *msg,
 				 ...);
diff --git a/xmllint.c b/xmllint.c
index 118a691..5ce88e1 100644
--- a/xmllint.c
+++ b/xmllint.c
@@ -54,6 +54,7 @@
 #include <libxml/tree.h>
 #include <libxml/xpath.h>
 #include <libxml/debugXML.h>
+#include <libxml/xmlerror.h>
 
 #ifdef LIBXML_DEBUG_ENABLED
 static int debug = 0;
@@ -97,7 +98,7 @@
 
     result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
     if (result) {
-	fprintf(stderr, "%s", result);
+	xmlGenericError(xmlGenericErrorContext, "%s", result);
 	xmlFree(result);
     }
     buffer[0] = 0;
@@ -112,7 +113,7 @@
 
 void
 xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
-    fprintf(stderr, "<p>");
+    xmlGenericError(xmlGenericErrorContext, "<p>");
     if (input != NULL) {
 	if (input->filename) {
 	    sprintf(&buffer[strlen(buffer)], "%s:%d: ", input->filename,
@@ -137,7 +138,7 @@
     int n;
 
     if (input == NULL) return;
-    fprintf(stderr, "<pre>\n");
+    xmlGenericError(xmlGenericErrorContext, "<pre>\n");
     cur = input->cur;
     base = input->base;
     while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
@@ -164,7 +165,7 @@
     }
     sprintf(&buffer[strlen(buffer)],"^\n");
     xmlHTMLEncodeSend();
-    fprintf(stderr, "</pre>");
+    xmlGenericError(xmlGenericErrorContext, "</pre>");
 }
 
 /**
@@ -193,12 +194,12 @@
         
     xmlHTMLPrintFileInfo(input);
 
-    fprintf(stderr, "<b>error</b>: ");
+    xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
     va_start(args, msg);
     vsprintf(&buffer[strlen(buffer)], msg, args);
     va_end(args);
     xmlHTMLEncodeSend();
-    fprintf(stderr, "</p>\n");
+    xmlGenericError(xmlGenericErrorContext, "</p>\n");
 
     xmlHTMLPrintFileContext(input);
     xmlHTMLEncodeSend();
@@ -231,12 +232,12 @@
 
     xmlHTMLPrintFileInfo(input);
         
-    fprintf(stderr, "<b>warning</b>: ");
+    xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
     va_start(args, msg);
     vsprintf(&buffer[strlen(buffer)], msg, args);
     va_end(args);
     xmlHTMLEncodeSend();
-    fprintf(stderr, "</p>\n");
+    xmlGenericError(xmlGenericErrorContext, "</p>\n");
 
     xmlHTMLPrintFileContext(input);
     xmlHTMLEncodeSend();
@@ -265,12 +266,12 @@
         
     xmlHTMLPrintFileInfo(input);
 
-    fprintf(stderr, "<b>validity error</b>: ");
+    xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
     va_start(args, msg);
     vsprintf(&buffer[strlen(buffer)], msg, args);
     va_end(args);
     xmlHTMLEncodeSend();
-    fprintf(stderr, "</p>\n");
+    xmlGenericError(xmlGenericErrorContext, "</p>\n");
 
     xmlHTMLPrintFileContext(input);
     xmlHTMLEncodeSend();
@@ -299,12 +300,12 @@
 
     xmlHTMLPrintFileInfo(input);
         
-    fprintf(stderr, "<b>validity warning</b>: ");
+    xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
     va_start(args, msg);
     vsprintf(&buffer[strlen(buffer)], msg, args);
     va_end(args);
     xmlHTMLEncodeSend();
-    fprintf(stderr, "</p>\n");
+    xmlGenericError(xmlGenericErrorContext, "</p>\n");
 
     xmlHTMLPrintFileContext(input);
     xmlHTMLEncodeSend();
@@ -568,12 +569,14 @@
 
 	dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid); 
 	if (dtd == NULL) {
-	    fprintf(stderr, "Could not parse DTD %s\n", dtdvalid);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Could not parse DTD %s\n", dtdvalid);
 	} else {
 	    xmlValidCtxt cvp;
 	    cvp.userData = (void *) stderr;                                                 cvp.error    = (xmlValidityErrorFunc) fprintf;                                  cvp.warning  = (xmlValidityWarningFunc) fprintf;
 	    if (!xmlValidateDtd(&cvp, doc, dtd)) {
-		fprintf(stderr, "Document %s does not validate against %s\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"Document %s does not validate against %s\n",
 			filename, dtdvalid);
 	    }
 	    xmlFreeDtd(dtd);
@@ -582,7 +585,8 @@
 	xmlValidCtxt cvp;
 	cvp.userData = (void *) stderr;                                                 cvp.error    = (xmlValidityErrorFunc) fprintf;                                  cvp.warning  = (xmlValidityWarningFunc) fprintf;
 	if (!xmlValidateDocument(&cvp, doc)) {
-	    fprintf(stderr, "Document %s does not validate\n", filename);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Document %s does not validate\n", filename);
 	}
     }
 
@@ -703,13 +707,14 @@
     if (noent != 0) xmlSubstituteEntitiesDefault(1);
     if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
     if ((htmlout) && (!nowrap)) {
-	fprintf(stderr,
+	xmlGenericError(xmlGenericErrorContext,
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
-	fprintf(stderr, "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
-	fprintf(stderr,
+	xmlGenericError(xmlGenericErrorContext,
+		"\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
+	xmlGenericError(xmlGenericErrorContext,
 	 "<html><head><title>%s output</title></head>\n",
 		argv[0]);
-	fprintf(stderr, 
+	xmlGenericError(xmlGenericErrorContext, 
 	 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
 		argv[0]);
     }
@@ -734,7 +739,7 @@
 	}
     }
     if ((htmlout) && (!nowrap)) {
-	fprintf(stderr, "</body></html>\n");
+	xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
     }
     if (files == 0) {
 	printf("Usage : %s [--debug] [--debugent] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n",
diff --git a/xmlmemory.c b/xmlmemory.c
index a9b2511..4cf1603 100644
--- a/xmlmemory.c
+++ b/xmlmemory.c
@@ -31,6 +31,7 @@
 
 
 #include <libxml/xmlmemory.h>
+#include <libxml/xmlerror.h>
 
 #ifdef xmlMalloc
 #undef xmlMalloc
@@ -111,7 +112,8 @@
 
 void
 xmlMallocBreakpoint(void) {
-    fprintf(stderr, "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock);
+    xmlGenericError(xmlGenericErrorContext,
+	    "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock);
 }
 
 /**
@@ -132,7 +134,8 @@
     
     if (!xmlMemInitialized) xmlInitMemory();
 #ifdef DEBUG_MEMORY
-    fprintf(stderr, "Malloc(%d)\n",size);
+    xmlGenericError(xmlGenericErrorContext,
+	    "Malloc(%d)\n",size);
 #endif
 
     TEST_POINT
@@ -140,7 +143,8 @@
     p = (MEMHDR *) malloc(RESERVE_SIZE+size);
 
     if (!p) {
-	fprintf(stderr, "xmlMalloc : Out of free space\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlMalloc : Out of free space\n");
 	xmlMemoryDump();
 	return(NULL);
     }   
@@ -157,7 +161,8 @@
 #endif
 
 #ifdef DEBUG_MEMORY
-    fprintf(stderr, "Malloc(%d) Ok\n",size);
+    xmlGenericError(xmlGenericErrorContext,
+	    "Malloc(%d) Ok\n",size);
 #endif
     
     if (xmlMemStopAtBlock == block) xmlMallocBreakpoint();
@@ -285,7 +290,8 @@
     return;
     
 error:    
-    fprintf(stderr, "xmlFree(%X) error\n", (unsigned int) ptr);
+    xmlGenericError(xmlGenericErrorContext,
+	    "xmlFree(%X) error\n", (unsigned int) ptr);
     return;
 }
 
@@ -557,7 +563,8 @@
  
 void debugmem_tag_error(void *p)
 {
-     fprintf(stderr, "Memory tag error occurs :%p \n\t bye\n", p);
+     xmlGenericError(xmlGenericErrorContext,
+	     "Memory tag error occurs :%p \n\t bye\n", p);
 #ifdef MEM_LIST
      if (stderr)
      xmlMemDisplay(stderr);
@@ -637,7 +644,8 @@
 #endif     
     
 #ifdef DEBUG_MEMORY
-     fprintf(stderr, "xmlInitMemory() Ok\n");
+     xmlGenericError(xmlGenericErrorContext,
+	     "xmlInitMemory() Ok\n");
 #endif     
      ret = 0;
      return(ret);
diff --git a/xpath.c b/xpath.c
index 5a84d92..a2b0028 100644
--- a/xpath.c
+++ b/xpath.c
@@ -57,6 +57,7 @@
 #ifdef LIBXML_DEBUG_ENABLED
 #include <libxml/debugXML.h>
 #endif
+#include <libxml/xmlerror.h>
 
 /* #define DEBUG */
 /* #define DEBUG_STEP */
@@ -173,21 +174,17 @@
  *									*
  ************************************************************************/
 
-FILE *xmlXPathDebug = NULL;
-
 #define TODO 								\
-    fprintf(xmlXPathDebug, "Unimplemented block at %s:%d\n",		\
+    xmlGenericError(xmlGenericErrorContext,				\
+	    "Unimplemented block at %s:%d\n",				\
             __FILE__, __LINE__);
 
 #define STRANGE 							\
-    fprintf(xmlXPathDebug, "Internal error at %s:%d\n",			\
+    xmlGenericError(xmlGenericErrorContext,				\
+	    "Internal error at %s:%d\n",				\
             __FILE__, __LINE__);
 
 #ifdef LIBXML_DEBUG_ENABLED
-double xmlXPathStringEvalNumber(const xmlChar *str);
-void xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs);
-void xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt);
-
 void xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
     int i;
     char shift[100];
@@ -360,7 +357,8 @@
         ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,		\
 	             ctxt->name##Max * sizeof(ctxt->name##Tab[0]));	\
         if (ctxt->name##Tab == NULL) {					\
-	    fprintf(xmlXPathDebug, "realloc failed !\n");		\
+	    xmlGenericError(xmlGenericErrorContext,			\
+		    "realloc failed !\n");				\
 	    return(0);							\
 	}								\
     }									\
@@ -464,7 +462,8 @@
     const xmlChar *cur;
     const xmlChar *base;
 
-    fprintf(xmlXPathDebug, "Error %s:%d: %s\n", file, line,
+    xmlGenericError(xmlGenericErrorContext,
+	    "Error %s:%d: %s\n", file, line,
             xmlXPathErrorMessages[no]);
 
     cur = ctxt->cur;
@@ -479,19 +478,19 @@
     base = cur;
     n = 0;
     while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
-        fprintf(xmlXPathDebug, "%c", (unsigned char) *cur++);
+        xmlGenericError(xmlGenericErrorContext, "%c", (unsigned char) *cur++);
 	n++;
     }
-    fprintf(xmlXPathDebug, "\n");
+    xmlGenericError(xmlGenericErrorContext, "\n");
     cur = ctxt->cur;
     while ((*cur == '\n') || (*cur == '\r'))
 	cur--;
     n = 0;
     while ((cur != base) && (n++ < 80)) {
-        fprintf(xmlXPathDebug, " ");
+        xmlGenericError(xmlGenericErrorContext, " ");
         base++;
     }
-    fprintf(xmlXPathDebug,"^\n");
+    xmlGenericError(xmlGenericErrorContext,"^\n");
 }
 
 
@@ -516,7 +515,8 @@
 
     ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewNodeSet: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewNodeSet: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
@@ -524,7 +524,8 @@
         ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
 					     sizeof(xmlNodePtr));
 	if (ret->nodeTab == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPathNewNodeSet: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPathNewNodeSet: out of memory\n");
 	    return(NULL);
 	}
 	memset(ret->nodeTab, 0 ,
@@ -561,7 +562,8 @@
         cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
 					     sizeof(xmlNodePtr));
 	if (cur->nodeTab == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPathNodeSetAdd: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPathNodeSetAdd: out of memory\n");
 	    return;
 	}
 	memset(cur->nodeTab, 0 ,
@@ -574,7 +576,8 @@
 	temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
 				      sizeof(xmlNodePtr));
 	if (temp == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPathNodeSetAdd: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPathNodeSetAdd: out of memory\n");
 	    return;
 	}
 	cur->nodeTab = temp;
@@ -634,7 +637,7 @@
 
     if (i >= cur->nodeNr) {
 #ifdef DEBUG
-        fprintf(xmlXPathDebug, 
+        xmlGenericError(xmlGenericErrorContext, 
 	        "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
 		val->name);
 #endif
@@ -686,17 +689,17 @@
 
 #if defined(DEBUG) || defined(DEBUG_STEP)
 /**
- * xmlXPathDebugNodeSet:
+ * xmlGenericErrorContextNodeSet:
  * @output:  a FILE * for the output
  * @obj:  the xmlNodeSetPtr to free
  *
  * Quick display of a NodeSet
  */
 void
-xmlXPathDebugNodeSet(FILE *output, xmlNodeSetPtr obj) {
+xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
     int i;
 
-    if (output == NULL) output = xmlXPathDebug;
+    if (output == NULL) output = xmlGenericErrorContext;
     if (obj == NULL)  {
         fprintf(output, "NodeSet == NULL !\n");
 	return;
@@ -740,7 +743,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewNodeSet: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewNodeSet: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -791,7 +795,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathWrapNodeSet: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathWrapNodeSet: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -976,7 +981,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewFloat: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewFloat: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -999,7 +1005,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewBoolean: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewBoolean: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -1022,7 +1029,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewString: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewString: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -1045,7 +1053,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewCString: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewCString: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -1071,7 +1080,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathObjectCopy: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathObjectCopy: out of memory\n");
 	return(NULL);
     }
     memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
@@ -1095,7 +1105,8 @@
 #endif
 	case XPATH_UNDEFINED:
 	case XPATH_USERS:
-	    fprintf(xmlXPathDebug, "xmlXPathObjectCopy: unsupported type %d\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPathObjectCopy: unsupported type %d\n",
 		    val->type);
     }
     return(ret);
@@ -1149,7 +1160,8 @@
 
     ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewContext: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewContext: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
@@ -1207,22 +1219,25 @@
 
 #define CHECK_CTXT(ctxt)						\
     if (ctxt == NULL) { 						\
-        fprintf(xmlXPathDebug, "%s:%d Internal error: ctxt == NULL\n",	\
+        xmlGenericError(xmlGenericErrorContext,				\
+		"%s:%d Internal error: ctxt == NULL\n",			\
 	        __FILE__, __LINE__);					\
     }									\
 
 
 #define CHECK_CONTEXT(ctxt)						\
     if (ctxt == NULL) { 						\
-        fprintf(xmlXPathDebug, "%s:%d Internal error: no context\n",	\
+        xmlGenericError(xmlGenericErrorContext,				\
+		"%s:%d Internal error: no context\n",			\
 	        __FILE__, __LINE__);					\
     }									\
     else if (ctxt->doc == NULL) { 					\
-        fprintf(xmlXPathDebug, "%s:%d Internal error: no document\n",	\
+        xmlGenericError(xmlGenericErrorContext,				\
+		"%s:%d Internal error: no document\n",			\
 	        __FILE__, __LINE__);					\
     }									\
     else if (ctxt->doc->children == NULL) { 				\
-        fprintf(xmlXPathDebug,						\
+        xmlGenericError(xmlGenericErrorContext,				\
 	        "%s:%d Internal error: document without root\n",	\
 	        __FILE__, __LINE__);					\
     }									\
@@ -1243,7 +1258,8 @@
 
     ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPathNewParserContext: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathNewParserContext: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
@@ -1432,7 +1448,8 @@
   
     if (arg1 == arg2) {
 #ifdef DEBUG_EXPR
-        fprintf(xmlXPathDebug, "Equal: by pointer\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"Equal: by pointer\n");
 #endif
         return(1);
     }
@@ -1440,14 +1457,16 @@
     switch (arg1->type) {
         case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-	    fprintf(xmlXPathDebug, "Equal: undefined\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "Equal: undefined\n");
 #endif
 	    break;
         case XPATH_NODESET:
 	    switch (arg2->type) {
 	        case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-		    fprintf(xmlXPathDebug, "Equal: undefined\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Equal: undefined\n");
 #endif
 		    break;
 		case XPATH_NODESET:
@@ -1478,7 +1497,8 @@
 	    switch (arg2->type) {
 	        case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-		    fprintf(xmlXPathDebug, "Equal: undefined\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Equal: undefined\n");
 #endif
 		    break;
 		case XPATH_NODESET:
@@ -1489,7 +1509,8 @@
 		    break;
 		case XPATH_BOOLEAN:
 #ifdef DEBUG_EXPR
-		    fprintf(xmlXPathDebug, "Equal: %d boolean %d \n",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Equal: %d boolean %d \n",
 			    arg1->boolval, arg2->boolval);
 #endif
 		    ret = (arg1->boolval == arg2->boolval);
@@ -1518,7 +1539,8 @@
 	    switch (arg2->type) {
 	        case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-		    fprintf(xmlXPathDebug, "Equal: undefined\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Equal: undefined\n");
 #endif
 		    break;
 		case XPATH_NODESET:
@@ -1549,7 +1571,8 @@
 	    switch (arg2->type) {
 	        case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-		    fprintf(xmlXPathDebug, "Equal: undefined\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "Equal: undefined\n");
 #endif
 		    break;
 		case XPATH_NODESET:
@@ -2346,74 +2369,88 @@
     obj = valuePop(ctxt);
 
 #ifdef DEBUG_STEP
-    fprintf(xmlXPathDebug, "new step : ");
+    xmlGenericError(xmlGenericErrorContext,
+	    "new step : ");
 #endif
     switch (axis) {
         case AXIS_ANCESTOR:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'ancestors' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'ancestors' ");
 #endif
 	    next = xmlXPathNextAncestor; break;
         case AXIS_ANCESTOR_OR_SELF:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'ancestors-or-self' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'ancestors-or-self' ");
 #endif
 	    next = xmlXPathNextAncestorOrSelf; break;
         case AXIS_ATTRIBUTE:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'attributes' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'attributes' ");
 #endif
 	    next = (xmlXPathTraversalFunction) xmlXPathNextAttribute; break;
 	    break;
         case AXIS_CHILD:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'child' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'child' ");
 #endif
 	    next = xmlXPathNextChild; break;
         case AXIS_DESCENDANT:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'descendant' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'descendant' ");
 #endif
 	    next = xmlXPathNextDescendant; break;
         case AXIS_DESCENDANT_OR_SELF:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'descendant-or-self' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'descendant-or-self' ");
 #endif
 	    next = xmlXPathNextDescendantOrSelf; break;
         case AXIS_FOLLOWING:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'following' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'following' ");
 #endif
 	    next = xmlXPathNextFollowing; break;
         case AXIS_FOLLOWING_SIBLING:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'following-siblings' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'following-siblings' ");
 #endif
 	    next = xmlXPathNextFollowingSibling; break;
         case AXIS_NAMESPACE:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'namespace' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'namespace' ");
 #endif
 	    next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; break;
 	    break;
         case AXIS_PARENT:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'parent' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'parent' ");
 #endif
 	    next = xmlXPathNextParent; break;
         case AXIS_PRECEDING:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'preceding' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'preceding' ");
 #endif
 	    next = xmlXPathNextPreceding; break;
         case AXIS_PRECEDING_SIBLING:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'preceding-sibling' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'preceding-sibling' ");
 #endif
 	    next = xmlXPathNextPrecedingSibling; break;
         case AXIS_SELF:
 #ifdef DEBUG_STEP
-	    fprintf(xmlXPathDebug, "axis 'self' ");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "axis 'self' ");
 #endif
 	    next = xmlXPathNextSelf; break;
     }
@@ -2423,33 +2460,41 @@
     nodelist = obj->nodesetval;
     ret = xmlXPathNodeSetCreate(NULL);
 #ifdef DEBUG_STEP
-    fprintf(xmlXPathDebug, " context contains %d nodes\n",
+    xmlGenericError(xmlGenericErrorContext,
+	    " context contains %d nodes\n",
             nodelist->nodeNr);
     switch (test) {
 	case NODE_TEST_NONE:
-	    fprintf(xmlXPathDebug, "           searching for none !!!\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for none !!!\n");
 	    break;
 	case NODE_TEST_TYPE:
-	    fprintf(xmlXPathDebug, "           searching for type %d\n", type);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for type %d\n", type);
 	    break;
 	case NODE_TEST_PI:
-	    fprintf(xmlXPathDebug, "           searching for PI !!!\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for PI !!!\n");
 	    break;
 	case NODE_TEST_ALL:
-	    fprintf(xmlXPathDebug, "           searching for *\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for *\n");
 	    break;
 	case NODE_TEST_NS:
-	    fprintf(xmlXPathDebug, "           searching for namespace %s\n",
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for namespace %s\n",
 	            prefix);
 	    break;
 	case NODE_TEST_NAME:
-	    fprintf(xmlXPathDebug, "           searching for name %s\n", name);
+	    xmlGenericError(xmlGenericErrorContext,
+		    "           searching for name %s\n", name);
 	    if (prefix != NULL)
-		fprintf(xmlXPathDebug, "           with namespace %s\n",
+		xmlGenericError(xmlGenericErrorContext,
+			"           with namespace %s\n",
 		        prefix);
 	    break;
     }
-    fprintf(xmlXPathDebug, "Testing : ");
+    xmlGenericError(xmlGenericErrorContext, "Testing : ");
 #endif
     /*
      * 2.3 Node Tests
@@ -2470,7 +2515,7 @@
 	    if (cur == NULL) break;
 #ifdef DEBUG_STEP
             t++;
-            fprintf(xmlXPathDebug, " %s", cur->name);
+            xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
 #endif
 	    switch (test) {
                 case NODE_TEST_NONE:
@@ -2543,7 +2588,7 @@
 	} while (cur != NULL);
     }
 #ifdef DEBUG_STEP
-    fprintf(xmlXPathDebug,
+    xmlGenericError(xmlGenericErrorContext,
             "\nExamined %d nodes, found %d nodes at that step\n", t, n);
 #endif
     xmlXPathFreeObject(obj);
@@ -2597,7 +2642,8 @@
     if (ctxt->context->contextSize > 0) {
 	valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize));
 #ifdef DEBUG_EXPR
-	fprintf(xmlXPathDebug, "last() : %d\n", ctxt->context->contextSize);
+	xmlGenericError(xmlGenericErrorContext,
+		"last() : %d\n", ctxt->context->contextSize);
 #endif
     } else {
 	XP_ERROR(XPATH_INVALID_CTXT_SIZE);
@@ -2620,7 +2666,7 @@
 	valuePush(ctxt,
 	      xmlXPathNewFloat((double) ctxt->context->proximityPosition));
 #ifdef DEBUG_EXPR
-	fprintf(xmlXPathDebug, "position() : %d\n",
+	xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
 		ctxt->context->proximityPosition);
 #endif
     } else {
@@ -2885,7 +2931,7 @@
     switch (cur->type) {
 	case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-	    fprintf(xmlXPathDebug, "String: undefined\n");
+	    xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
 #endif
 	    valuePush(ctxt, xmlXPathNewCString(""));
 	    break;
@@ -3495,7 +3541,7 @@
     switch (cur->type) {
 	case XPATH_UNDEFINED:
 #ifdef DEBUG_EXPR
-	    fprintf(xmlXPathDebug, "NUMBER: undefined\n");
+	    xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
 #endif
 	    valuePush(ctxt, xmlXPathNewFloat(0.0));
 	    break;
@@ -3967,7 +4013,7 @@
 	XP_ERROR(XPATH_UNKNOWN_FUNC_ERROR);
     }
 #ifdef DEBUG_EXPR
-    fprintf(xmlXPathDebug, "Calling function %s\n", name);
+    xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", name);
 #endif
 
     if (CUR != '(') {
@@ -4098,7 +4144,7 @@
 	buf[len] = NXT(len);
 	len++;
 	if (len >= XML_MAX_NAMELEN) {
-	    fprintf(stderr, 
+	    xmlGenericError(xmlGenericErrorContext, 
 	       "xmlScanName: reached XML_MAX_NAMELEN limit\n");
 	    while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
 		   (NXT(len) == '.') || (NXT(len) == '-') ||
@@ -4170,7 +4216,8 @@
 		if (NXT(len) == '/') {
 		    /* element name */
 #ifdef DEBUG_STEP
-		    fprintf(xmlXPathDebug, "PathExpr: AbbrRelLocation\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PathExpr: AbbrRelLocation\n");
 #endif
 		    lc = 1;
 		    break;
@@ -4179,7 +4226,8 @@
 		    blank = 1;
 		} else if (NXT(len) == ':') {
 #ifdef DEBUG_STEP
-		    fprintf(xmlXPathDebug, "PathExpr: AbbrRelLocation\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PathExpr: AbbrRelLocation\n");
 #endif
 		    lc = 1;
 		    break;
@@ -4187,12 +4235,14 @@
 		    /* Note Type or Function */
 		    if (xmlXPathIsNodeType(name)) {
 #ifdef DEBUG_STEP
-		        fprintf(xmlXPathDebug, "PathExpr: Type search\n");
+		        xmlGenericError(xmlGenericErrorContext,
+				"PathExpr: Type search\n");
 #endif
 			lc = 1;
 		    } else {
 #ifdef DEBUG_STEP
-		        fprintf(xmlXPathDebug, "PathExpr: function call\n");
+		        xmlGenericError(xmlGenericErrorContext,
+				"PathExpr: function call\n");
 #endif
 			lc = 0;
 		    }
@@ -4200,7 +4250,8 @@
 		} else if ((NXT(len) == '[')) {
 		    /* element name */
 #ifdef DEBUG_STEP
-		    fprintf(xmlXPathDebug, "PathExpr: AbbrRelLocation\n");
+		    xmlGenericError(xmlGenericErrorContext,
+			    "PathExpr: AbbrRelLocation\n");
 #endif
 		    lc = 1;
 		    break;
@@ -4211,7 +4262,8 @@
 	    }
 	    if (NXT(len) == 0) {
 #ifdef DEBUG_STEP
-		fprintf(xmlXPathDebug, "PathExpr: AbbrRelLocation\n");
+		xmlGenericError(xmlGenericErrorContext,
+			"PathExpr: AbbrRelLocation\n");
 #endif
 		/* element name */
 		lc = 1;
@@ -4685,8 +4737,9 @@
     NEXT;
     SKIP_BLANKS;
 #ifdef DEBUG_STEP
-    fprintf(xmlXPathDebug, "After predicate : ");
-    xmlXPathDebugNodeSet(xmlXPathDebug, ctxt->value->nodesetval);
+    xmlGenericError(xmlGenericErrorContext, "After predicate : ");
+    xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
+	    ctxt->value->nodesetval);
 #endif
 }
 
@@ -5017,12 +5070,13 @@
 	    return;
 
 #ifdef DEBUG_STEP
-	fprintf(xmlXPathDebug, "Basis : computing new set\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"Basis : computing new set\n");
 #endif
 	xmlXPathNodeCollectAndTest(ctxt, axis, test, type, prefix, name);
 #ifdef DEBUG_STEP
-	fprintf(xmlXPathDebug, "Basis : ");
-	xmlXPathDebugNodeSet(stdout, ctxt->value->nodesetval);
+	xmlGenericError(xmlGenericErrorContext, "Basis : ");
+	xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
 #endif
 	if (name != NULL)
 	    xmlFree(name);
@@ -5036,8 +5090,9 @@
 	}
     }
 #ifdef DEBUG_STEP
-    fprintf(xmlXPathDebug, "Step : ");
-    xmlXPathDebugNodeSet(xmlXPathDebug, ctxt->value->nodesetval);
+    xmlGenericError(xmlGenericErrorContext, "Step : ");
+    xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
+	    ctxt->value->nodesetval);
 #endif
 }
 
@@ -5144,9 +5199,6 @@
 
     xmlXPathInit();
 
-    if (xmlXPathDebug == NULL)
-        xmlXPathDebug = stderr;
-
     CHECK_CONTEXT(ctx)
 
     ctxt = xmlXPathNewParserContext(str, ctx);
@@ -5159,7 +5211,7 @@
     xmlXPathEvalExpr(ctxt);
 
     if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NODESET)) {
-	fprintf(xmlXPathDebug,
+	xmlGenericError(xmlGenericErrorContext,
 		"xmlXPathEval: evaluation failed to return a node set\n");
     } else {
 	res = valuePop(ctxt);
@@ -5174,7 +5226,8 @@
         }
     } while (tmp != NULL);
     if (stack != 0) {
-	fprintf(xmlXPathDebug, "xmlXPathEval: %d object left on the stack\n",
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathEval: %d object left on the stack\n",
 	        stack);
     }
     if (ctxt->error != XPATH_EXPRESSION_OK) {
@@ -5206,8 +5259,6 @@
 
     CHECK_CONTEXT(ctxt)
 
-    if (xmlXPathDebug == NULL)
-        xmlXPathDebug = stderr;
     pctxt = xmlXPathNewParserContext(str, ctxt);
     xmlXPathEvalExpr(pctxt);
 
@@ -5220,7 +5271,8 @@
 	}
     } while (tmp != NULL);
     if (stack != 0) {
-	fprintf(xmlXPathDebug, "xmlXPathEvalExpression: %d object left on the stack\n",
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlXPathEvalExpression: %d object left on the stack\n",
 	        stack);
     }
     xmlXPathFreeParserContext(pctxt);
diff --git a/xpointer.c b/xpointer.c
index 06949ea..77cf10e 100644
--- a/xpointer.c
+++ b/xpointer.c
@@ -33,20 +33,20 @@
 #ifdef LIBXML_DEBUG_ENABLED
 #include <libxml/debugXML.h>
 #endif
+#include <libxml/xmlerror.h>
 
 #ifdef LIBXML_XPTR_ENABLED
 
 /* #define DEBUG_RANGES */
 
-
-extern FILE *xmlXPathDebug;
-
 #define TODO 								\
-    fprintf(xmlXPathDebug, "Unimplemented block at %s:%d\n",		\
+    xmlGenericError(xmlGenericErrorContext,				\
+	    "Unimplemented block at %s:%d\n",				\
             __FILE__, __LINE__);
 
 #define STRANGE 							\
-    fprintf(xmlXPathDebug, "Internal error at %s:%d\n",			\
+    xmlGenericError(xmlGenericErrorContext,				\
+	    "Internal error at %s:%d\n",				\
             __FILE__, __LINE__);
 
 /************************************************************************
@@ -235,7 +235,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewPoint: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewPoint: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -300,7 +301,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangePoints: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangePoints: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -337,7 +339,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangePoints: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangePoints: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -372,7 +375,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangePointNode: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangePointNode: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -409,7 +413,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodePoint: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangeNodePoint: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -442,7 +447,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodes: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangeNodes: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -472,7 +478,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodes: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangeNodes: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -518,7 +525,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewRangeNodeObject: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewRangeNodeObject: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -558,7 +566,8 @@
 
     ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrLocationSetCreate: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrLocationSetCreate: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
@@ -566,7 +575,8 @@
         ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
 					     sizeof(xmlXPathObjectPtr));
 	if (ret->locTab == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPtrLocationSetCreate: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPtrLocationSetCreate: out of memory\n");
 	    return(NULL);
 	}
 	memset(ret->locTab, 0 ,
@@ -603,7 +613,8 @@
         cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
 					     sizeof(xmlXPathObjectPtr));
 	if (cur->locTab == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPtrLocationSetAdd: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPtrLocationSetAdd: out of memory\n");
 	    return;
 	}
 	memset(cur->locTab, 0 ,
@@ -616,7 +627,8 @@
 	temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
 				      sizeof(xmlXPathObjectPtr));
 	if (temp == NULL) {
-	    fprintf(xmlXPathDebug, "xmlXPtrLocationSetAdd: out of memory\n");
+	    xmlGenericError(xmlGenericErrorContext,
+		    "xmlXPtrLocationSetAdd: out of memory\n");
 	    return;
 	}
 	cur->locTab = temp;
@@ -673,7 +685,7 @@
 
     if (i >= cur->locNr) {
 #ifdef DEBUG
-        fprintf(xmlXPathDebug, 
+        xmlGenericError(xmlGenericErrorContext, 
 	        "xmlXPtrLocationSetDel: Range %s wasn't found in RangeList\n",
 		val->name);
 #endif
@@ -745,7 +757,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewLocationSetNodes: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewLocationSetNodes: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -772,7 +785,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrNewLocationSetNodes: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrNewLocationSetNodes: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -808,7 +822,8 @@
 
     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
     if (ret == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrWrapLocationSet: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrWrapLocationSet: out of memory\n");
 	return(NULL);
     }
     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
@@ -943,7 +958,8 @@
     len++;
     buffer = (xmlChar *) xmlMalloc(len * sizeof (xmlChar));
     if (buffer == NULL) {
-        fprintf(xmlXPathDebug, "xmlXPtrEvalXPtrPart: out of memory\n");
+        xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrEvalXPtrPart: out of memory\n");
 	return;
     }
 
@@ -987,7 +1003,8 @@
 	xmlXPathEvalExpr(ctxt);
 	CUR_PTR=left;
     } else {
-        fprintf(xmlXPathDebug, "unsupported scheme '%s'\n", name);
+        xmlGenericError(xmlGenericErrorContext,
+		"unsupported scheme '%s'\n", name);
     }
     xmlFree(buffer);
     xmlFree(name);
@@ -1095,7 +1112,8 @@
      * this might prove useful in some cases, warn about it.
      */
     if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
-	fprintf(xmlXPathDebug, "warning: ChildSeq not starting by /1\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"warning: ChildSeq not starting by /1\n");
     }
 
     if (name != NULL) {
@@ -1234,8 +1252,6 @@
     if ((ctx == NULL) || (str == NULL))
 	return(NULL);
 
-    if (xmlXPathDebug == NULL)
-        xmlXPathDebug = stderr;
     ctxt = xmlXPathNewParserContext(str, ctx);
     if (ctx->node != NULL) {
 	init = xmlXPathNewNodeSet(ctx->node);
@@ -1246,7 +1262,7 @@
     if ((ctxt->value != NULL) &&
 	(ctxt->value->type != XPATH_NODESET) &&
 	(ctxt->value->type != XPATH_LOCATIONSET)) {
-	fprintf(xmlXPathDebug,
+	xmlGenericError(xmlGenericErrorContext,
 		"xmlXPtrEval: evaluation failed to return a node set\n");
     } else {
 	res = valuePop(ctxt);
@@ -1272,7 +1288,8 @@
         }
     } while (tmp != NULL);
     if (stack != 0) {
-	fprintf(xmlXPathDebug, "xmlXPtrEval: %d object left on the stack\n",
+	xmlGenericError(xmlGenericErrorContext,
+		"xmlXPtrEval: %d object left on the stack\n",
 	        stack);
     }
     if (ctxt->error != XPATH_EXPRESSION_OK) {
@@ -2054,10 +2071,11 @@
 		match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
 		if (match) {
 #ifdef DEBUG_RANGES
-		    fprintf(stdout, "found range %d bytes at index %d of ->",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "found range %d bytes at index %d of ->",
 			    stringlen, pos + 1);
 		    xmlDebugDumpString(stdout, cur->content);
-		    fprintf(stdout, "\n");
+		    xmlGenericError(xmlGenericErrorContext, "\n");
 #endif
 		    *end = cur;
 		    *endindex = pos + stringlen;
@@ -2070,10 +2088,11 @@
 		match = (!xmlStrncmp(&cur->content[pos], string, sub));
 		if (match) {
 #ifdef DEBUG_RANGES
-		    fprintf(stdout, "found subrange %d bytes at index %d of ->",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "found subrange %d bytes at index %d of ->",
 			    sub, pos + 1);
 		    xmlDebugDumpString(stdout, cur->content);
-		    fprintf(stdout, "\n");
+		    xmlGenericError(xmlGenericErrorContext, "\n");
 #endif
                     string = &string[sub];
 		    stringlen -= sub;
@@ -2138,10 +2157,11 @@
 		    if (str != NULL) {
 			pos = (str - cur->content);
 #ifdef DEBUG_RANGES
-			fprintf(stdout, "found '%c' at index %d of ->",
+			xmlGenericError(xmlGenericErrorContext,
+				"found '%c' at index %d of ->",
 				first, pos + 1);
 			xmlDebugDumpString(stdout, cur->content);
-			fprintf(stdout, "\n");
+			xmlGenericError(xmlGenericErrorContext, "\n");
 #endif
 			if (xmlXPtrMatchString(string, cur, pos + 1,
 					       end, endindex)) {
@@ -2160,10 +2180,11 @@
 		     * character. 
 		     */
 #ifdef DEBUG_RANGES
-		    fprintf(stdout, "found '' at index %d of ->",
+		    xmlGenericError(xmlGenericErrorContext,
+			    "found '' at index %d of ->",
 			    pos + 1);
 		    xmlDebugDumpString(stdout, cur->content);
-		    fprintf(stdout, "\n");
+		    xmlGenericError(xmlGenericErrorContext, "\n");
 #endif
 		    *start = cur;
 		    *startindex = pos + 1;
@@ -2397,12 +2418,14 @@
 	xmlXPtrGetLastChar(&end, &endindex);
 
 #ifdef DEBUG_RANGES
-	fprintf(stdout, "from index %d of ->", startindex);
+	xmlGenericError(xmlGenericErrorContext,
+		"from index %d of ->", startindex);
 	xmlDebugDumpString(stdout, start->content);
-	fprintf(stdout, "\n");
-	fprintf(stdout, "to index %d of ->", endindex);
+	xmlGenericError(xmlGenericErrorContext, "\n");
+	xmlGenericError(xmlGenericErrorContext,
+		"to index %d of ->", endindex);
 	xmlDebugDumpString(stdout, end->content);
-	fprintf(stdout, "\n");
+	xmlGenericError(xmlGenericErrorContext, "\n");
 #endif
 	do {
             fend = end;