d036e58aa91bc396e6d9f402827c5e7634721b62
[yaz-moved-to-github.git] / src / xmlerror.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
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 #if YAZ_HAVE_XSLT 
50     xsltSetGenericErrorFunc((void *) (prefix ? prefix : "XSLT"),
51                             xml_error_handler);
52 #endif
53 #endif
54 }
55
56 /*
57  * Local variables:
58  * c-basic-offset: 4
59  * c-file-style: "Stroustrup"
60  * indent-tabs-mode: nil
61  * End:
62  * vim: shiftwidth=4 tabstop=8 expandtab
63  */
64