332c7af2dd850d0304379ff0e6de47283daf399e
[yaz-moved-to-github.git] / src / libxml2_error.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file libxml2_error.c
7  * \brief Libxml2 error handling
8  */
9
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <yaz/log.h>
13 #include <yaz/snprintf.h>
14 #include <yaz/libxml2_error.h>
15
16 #if YAZ_HAVE_XML2
17 #include <libxml/xmlerror.h>
18 #endif
19
20 #if YAZ_HAVE_XSLT
21 #include <libxslt/xsltutils.h>
22 #endif
23
24 static int libxml2_error_level = 0;
25
26 #if YAZ_HAVE_XML2
27 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
28 {
29     char buf[1024];
30
31     va_list ap;
32     va_start(ap, fmt);
33
34     yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
35     yaz_log(libxml2_error_level, "%s: %s", (char*) ctx, buf);
36
37     va_end (ap);
38 }
39 #endif
40
41 int libxml2_error_to_yazlog(int level, const char *lead_msg)
42 {
43     libxml2_error_level = level;
44 #if YAZ_HAVE_XML2
45     xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
46 #if YAZ_HAVE_XSLT
47     xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
48 #endif
49     return 0;
50 #else
51     return -1;
52 #endif
53 }
54
55 /*
56  * Local variables:
57  * c-basic-offset: 4
58  * c-file-style: "Stroustrup"
59  * indent-tabs-mode: nil
60  * End:
61  * vim: shiftwidth=4 tabstop=8 expandtab
62  */
63