Question
I'm writing a small wrapper around libxml2 in C++, and I'm trying to work out how to handle errors. For now, let's say I just want to print them out. Here's what I've got at present:
My error handling function:
void foo(void *ctx, const char *msg, ...) {
cout << msg << endl;
return;
}
Initialised like this:
xmlGenericErrorFunc handler = (xmlGenericErrorFunc)foo;
initGenericErrorDefaultFunc(&handler);
However, if I parse a bad XPath, I get this output:
%s
Without the error handling code, I get this:
XPath error : Invalid expression //.@foobar ^
Obviously eventually my error handling will do more than just print out the error message (it'll log it to a database or something), but for now - how can I get that error string?
Solution
The three dots at the end of the argument list for you function foo() means it takes a variable amount of arguments. To be able to print those you could do something like this (not tested):
#include <stdarg.h>
#define TMP_BUF_SIZE 256
void foo(void *ctx, const char *msg, ...) {
char string[TMP_BUF_SIZE];
va_list arg_ptr;
va_start(arg_ptr, msg);
vsnprintf(string, TMP_BUF_SIZE, msg, arg_ptr);
va_end(arg_ptr);
cout << string << endl;
return;
}
OTHER TIPS
As already pointed out if this is your handling function:
#define TMP_BUF_SIZE 256
void err(void *ctx, const char *msg, ...) {
char string[TMP_BUF_SIZE];
va_list arg_ptr;
va_start(arg_ptr, msg);
vsnprintf(string, TMP_BUF_SIZE, msg, arg_ptr);
va_end(arg_ptr);
cout << string << endl;
return;
}
you can set it with this libxml2 function
xmlSetGenericErrorFunc(NULL,gemXmlGenericErrorFunc);
if you have a context to pass, i.e. some struct,data,class whatever pointer casted to void*, you can put it as first argument.
Note that foo will be called a lot, for instance if you have a parse error each time libxml adds a line to the error message.