70bd639836d373d98c18bf0599a2f215aeee1075
[yaz-moved-to-github.git] / src / xmlerror.c
1 /*
2  * Copyright (C) 1995-2008, Index Data ApS
3  * All rights reserved.
4  */
5
6 /** \file
7     \brief Log XML / XSLT Errors via yaz_log
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <yaz/log.h>
14 #include <yaz/snprintf.h>
15
16 #if YAZ_HAVE_XML2
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19 #endif
20 #if YAZ_HAVE_XSLT
21 #include <libxslt/xsltutils.h>
22 #endif
23
24 static int xml_error_log_level = YLOG_WARN;
25
26 #if YAZ_HAVE_XML2
27 static void xml_error_handler(void *ctx, const char *fmt, ...)
28 {
29     char buf[1024];
30     const char *prefix = (const char *) ctx;
31
32     va_list ap;
33     va_start(ap, fmt);
34
35     yaz_vsnprintf(buf, sizeof(buf), fmt, ap);
36     yaz_log(YLOG_WARN, "%s: %s", prefix, buf);
37
38     va_end (ap);
39 }
40 #endif
41
42 void yaz_log_xml_errors(const char *prefix, int log_level)
43 {
44     xml_error_log_level = log_level;
45     
46 #if YAZ_HAVE_XML2
47     xmlSetGenericErrorFunc((void *) (prefix ? prefix : "XML"),
48                            xml_error_handler);
49 #endif
50 #if YAZ_HAVE_XSLT 
51     xsltSetGenericErrorFunc((void *) (prefix ? prefix : "XSLT"),
52                             xml_error_handler);
53 #endif
54 }
55
56 /*
57  * Local variables:
58  * c-basic-offset: 4
59  * indent-tabs-mode: nil
60  * End:
61  * vim: shiftwidth=4 tabstop=8 expandtab
62  */