* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
- * $Id: log.h,v 1.37 2006-05-07 18:26:25 adam Exp $
+ * $Id: log.h,v 1.38 2006-07-06 13:10:29 heikki Exp $
*/
/**
YAZ_EXPORT void yaz_log_reopen(void);
+/** \brief Truncate the log file */
+YAZ_EXPORT void yaz_log_trunc(void);
+
YAZ_EXPORT void log_event_start(void (*func)(int level, const char *msg,
void *info), void *info);
/* Copyright (C) 2006, Index Data ApS
* See the file LICENSE for details.
- * $Id: nfaxml.h,v 1.3 2006-07-06 07:45:07 adam Exp $
+ * $Id: nfaxml.h,v 1.4 2006-07-06 13:10:29 heikki Exp $
*/
/**
#ifndef YAZ_NFA_XML_H
#define YAZ_NFA_XML_H
+#if YAZ_HAVE_XML2
+
+#include <libxml/parser.h>
+
#include <yaz/yconfig.h>
#include <yaz/log.h>
#include <yaz/nfa.h>
*
* It is up to the caller to destroy the nfa when done.
*
+ * Does not expand XIncludes.
+ *
* In case of errors, returns a null pointer. You can then
- * call xmlGetLastError() to get the details of the error.
+ * call xmlGetLastError() to get the details of the error,
+ * if you have a recent enough libxml2. Those are already
+ * logged in yazlog.
*
*/
-yaz_nfa *yaz_nfa_parse_xml_doc(void *xmlDocPtr);
+yaz_nfa *yaz_nfa_parse_xml_doc(xmlDocPtr doc);
/** \brief Parse the NFA from a file
*
* It is up to the caller to destroy the nfa when done.
*
- * In case of errors, error_info will be filled with
- * suitable diagnostics. It may be null, if you don't
- * care.
+ * This routine also expands XIncludes.
+ *
+ * In case of errors, returns a null pointer. You can then
+ * call xmlGetLastError() to get the details of the error,
+ * if you have a recent enough libxml2. Those are already
+ * logged in yazlog.
*
*/
yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath);
-YAZ_END_CDECL
+/** \brief Parse the NFA from a memory buffer
+ *
+ * \param filepath path to the xml file to parse
+ * \param error_info will be filled in case of errors
+ *
+ * \returns either the NFA, or null in case of errors
+ *
+ * It is up to the caller to destroy the nfa when done.
+ *
+ * Does not expand XIncludes.
+ *
+ * In case of errors, returns a null pointer. You can then
+ * call xmlGetLastError() to get the details of the error,
+ * if you have a recent enough libxml2. Those are already
+ * logged in yazlog.
+ *
+ */
+yaz_nfa *yaz_nfa_parse_xml_memory(const char *xmlbuff);
+
+YAZ_END_CDECL
+
+#endif /* YAZ_HAVE_XML2 */
#endif /* YAZ_NFA_XML_H */
/*
* Copyright (C) 1995-2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: test.h,v 1.6 2006-05-10 12:52:17 heikki Exp $
+ * $Id: test.h,v 1.7 2006-07-06 13:10:29 heikki Exp $
*/
/** \file test.h
/** \brief Macro to terminate the system (end of main, normally) */
#define YAZ_CHECK_TERM yaz_check_term1(); return 0
+/** \brief Macro to enable and initialize the yaz_log(start of main) */
+#define YAZ_CHECK_LOG() yaz_check_init_log(argv[0])
YAZ_BEGIN_CDECL
+
/** \brief used by macro. Should not be called directly */
YAZ_EXPORT void yaz_check_init1(int *argc, char ***argv);
+
/** \brief used by macro. Should not be called directly */
YAZ_EXPORT void yaz_check_term1(void);
+
+/** \brief used by macro. Should not be called directly */
+YAZ_EXPORT void yaz_check_init_log(char *argv0);
+
/** \brief used by macro. Should not be called directly */
YAZ_EXPORT void yaz_check_print1(int type, const char *file, int line,
const char *expr);
* Copyright (C) 1995-2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: log.c,v 1.36 2006-06-30 11:09:44 adam Exp $
+ * $Id: log.c,v 1.37 2006-07-06 13:10:31 heikki Exp $
*/
/**
#endif
}
+
void yaz_log_init_level(int level)
{
init_mutex();
end_hook_info = info;
}
-static void yaz_log_open_check(struct tm *tm, int force)
+static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
{
char new_filename[512];
static char cur_filename[512] = "";
if (force && yaz_file_type == use_file && *cur_filename)
{
yaz_log_close();
- yaz_global_log_file = fopen(cur_filename, "a");
+ yaz_global_log_file = fopen(cur_filename, filemode);
if (l_level < 0) l_level = default_log_level();
if (l_level & YLOG_FLUSH)
setvbuf(yaz_global_log_file, 0, _IONBF, 0);
}
}
-void yaz_log_reopen()
+static void yaz_log_do_reopen(const char *filemode)
{
time_t cur_time = time(0);
#if HAVE_LOCALTIME_R
#else
tm = localtime(&cur_time);
#endif
- yaz_log_open_check(tm, 1);
+ yaz_log_open_check(tm, 1, filemode );
nmem_mutex_leave(log_mutex);
}
+
+void yaz_log_reopen()
+{
+ yaz_log_do_reopen("a");
+}
+
+void yaz_log_trunc()
+{
+ yaz_log_do_reopen("w");
+}
+
+
+
static void yaz_strftime(char *dst, size_t sz,
const char *fmt, const struct tm *tm)
{
tm = localtime(&ti);
#endif
- yaz_log_open_check(tm, 0);
+ yaz_log_open_check(tm, 0, "a");
file = yaz_log_file(); /* file may change in yaz_log_open_check */
if (file)
/* Copyright (C) 2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: nfaxml.c,v 1.6 2006-07-06 10:17:53 adam Exp $
+ * $Id: nfaxml.c,v 1.7 2006-07-06 13:10:31 heikki Exp $
*/
/**
#include <string.h>
-#include <libxml/parser.h>
+/* #include <libxml/parser.h> */
#include <libxml/tree.h>
#include <libxml/xinclude.h>
/** \brief Parse the NFA from a XML document
*/
-yaz_nfa *yaz_nfa_parse_xml_doc(void *p)
+yaz_nfa *yaz_nfa_parse_xml_doc(xmlDocPtr doc)
{
- xmlDocPtr doc = (xmlDocPtr) p;
+ libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_file");
+
if (!doc)
return 0;
return 0;
}
-/** \brief Log XML errors in yaz_log
- *
- * Disabled because xmlErrorPtr does not exist for older Libxml2's
- */
-#if 0
-static void log_xml_error(int errlevel, char *msg) {
- xmlErrorPtr e=xmlGetLastError();
- if (!e) /* no error happened */
- return;
- if (!errlevel)
- errlevel=YLOG_FATAL;
- yaz_log(errlevel,"%s %d/%d: %s:%d: '%s' ",
- msg, e->domain, e->code, e->file, e->line, e->message);
- if (e->str1 || e->str2 || e->str3 )
- yaz_log(errlevel,"extra info: '%s' '%s' '%s' %d %d",
- e->str1, e->str2, e->str3, e->int1, e->int2 );
-}
-#endif
/** \brief Parse the NFA from a file
*/
return yaz_nfa_parse_xml_doc(doc);
}
+/** \brief Parse the NFA from a memory buffer
+ */
+yaz_nfa *yaz_nfa_parse_xml_memory(const char *xmlbuff) {
+ int nSubst;
+
+ libxml2_error_to_yazlog(YLOG_FATAL, "yaz_nfa_parse_xml_memory");
+ xmlDocPtr doc = xmlParseMemory(xmlbuff, strlen(xmlbuff));
+ return yaz_nfa_parse_xml_doc(doc);
+}
+
#endif /* YAZ_HAVE_XML2 */
* Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: test.c,v 1.7 2006-05-10 12:52:28 heikki Exp $
+ * $Id: test.c,v 1.8 2006-07-06 13:10:31 heikki Exp $
*/
/** \file test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
#include <yaz/test.h>
+#include <yaz/log.h>
static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
static int test_total = 0;
static int test_failed = 0;
static int test_verbose = 1;
static char *test_prog = 0;
+static int log_tests = 0;
static FILE *get_file()
{
*argv_p += i;
}
+/** \brief Initialize the log system */
+void yaz_check_init_log(char *argv0)
+{
+ char logfilename[2048];
+ log_tests = 1;
+ sprintf(logfilename,"%s.log", progname(argv0) );
+ unlink(logfilename);
+ yaz_log_init_file(logfilename);
+ yaz_log_trunc();
+
+}
+
void yaz_check_term1(void)
{
/* summary */
const char *expr)
{
const char *msg = "unknown";
+ int printit=1;
test_total++;
switch(type)
test_failed++;
msg = "FAILED";
if (test_verbose < 1)
- return;
+ printit=0;
break;
case YAZ_TEST_TYPE_OK:
msg = "ok";
if (test_verbose < 3)
- return;
+ printit=0;
break;
}
- fprintf(get_file(), "%s:%d %s: ", file, line, msg);
- fprintf(get_file(), "%s\n", expr);
+ if (printit) {
+ fprintf(get_file(), "%s:%d %s: ", file, line, msg);
+ fprintf(get_file(), "%s\n", expr);
+ }
+ if (log_tests) {
+ yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
+ yaz_log(YLOG_LOG, "%s\n", expr);
+ }
}
/* Copyright (C) 2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: nfaxmltest1.c,v 1.2 2006-07-06 10:17:55 adam Exp $
+ * $Id: nfaxmltest1.c,v 1.3 2006-07-06 13:10:31 heikki Exp $
*
*/
#include <libxml/parser.h>
+/** \brief Test parsing of a minimal, valid xml string */
void test1() {
char *xmlstr="<ruleset> "
"<rule> "
" <tostring>bar</tostring> "
"</rule>"
"</ruleset>";
- xmlDocPtr doc = xmlParseMemory(xmlstr, strlen(xmlstr));
- YAZ_CHECK(doc);
- if (!doc)
- return;
+ yaz_nfa *nfa=yaz_nfa_parse_xml_memory(xmlstr);
+ YAZ_CHECK(nfa);
}
+
+
+/** \brief Test parsing of a minimal, invalid xml string */
+void test2() {
+ char *xmlstr="<ruleset> "
+ "<rule> "
+ " <fromstring>foo</fromstring> "
+ " <tostring>bar</tostring> "
+ "</rule>";
+ /* missing "</ruleset>" */
+ yaz_log(YLOG_LOG,"Parsing bad xml, expecting errors:");
+ yaz_nfa *nfa=yaz_nfa_parse_xml_memory(xmlstr);
+ YAZ_CHECK(!nfa);
+}
+
+
int main(int argc, char **argv)
{
YAZ_CHECK_INIT(argc, argv);
+ YAZ_CHECK_LOG();
nmem_init ();
test1();
+ test2();
nmem_exit ();
YAZ_CHECK_TERM;