From fc42e0dc251df1d80ffc4c9279478d64d8a7027c Mon Sep 17 00:00:00 2001 From: Heikki Levanto Date: Thu, 6 Jul 2006 13:10:29 +0000 Subject: [PATCH] Added yaz_log_trunc() to truncate the log file Added YAZ_CHECK_LOG macro for enabling logging in the test files Changed test.c to write to the log, if logging enabled Small changes to the interface of nfaxml's interface --- include/yaz/log.h | 5 ++++- include/yaz/nfaxml.h | 46 +++++++++++++++++++++++++++++++++++++++------- include/yaz/test.h | 10 +++++++++- src/log.c | 26 ++++++++++++++++++++------ src/nfaxml.c | 37 +++++++++++++++---------------------- src/test.c | 32 +++++++++++++++++++++++++++----- test/nfaxmltest1.c | 27 ++++++++++++++++++++++----- 7 files changed, 136 insertions(+), 47 deletions(-) diff --git a/include/yaz/log.h b/include/yaz/log.h index fb3b161..1aea84f 100644 --- a/include/yaz/log.h +++ b/include/yaz/log.h @@ -23,7 +23,7 @@ * 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 $ */ /** @@ -187,6 +187,9 @@ YAZ_EXPORT void yaz_log_set_handler(void (*func)(int, const char *, 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); diff --git a/include/yaz/nfaxml.h b/include/yaz/nfaxml.h index 8ddb0a2..6c7436d 100644 --- a/include/yaz/nfaxml.h +++ b/include/yaz/nfaxml.h @@ -1,6 +1,6 @@ /* 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 $ */ /** @@ -35,6 +35,10 @@ #ifndef YAZ_NFA_XML_H #define YAZ_NFA_XML_H +#if YAZ_HAVE_XML2 + +#include + #include #include #include @@ -50,11 +54,15 @@ YAZ_BEGIN_CDECL * * 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 @@ -66,16 +74,40 @@ yaz_nfa *yaz_nfa_parse_xml_doc(void *xmlDocPtr); * * 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 */ /* diff --git a/include/yaz/test.h b/include/yaz/test.h index c57b197..62c57e2 100644 --- a/include/yaz/test.h +++ b/include/yaz/test.h @@ -2,7 +2,7 @@ * 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 @@ -50,12 +50,20 @@ int yaz_test_get_verbosity(); /** \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); diff --git a/src/log.c b/src/log.c index 0689f67..5c41f07 100644 --- a/src/log.c +++ b/src/log.c @@ -2,7 +2,7 @@ * 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 $ */ /** @@ -187,6 +187,7 @@ static void rotate_log(const char *cur_fname) #endif } + void yaz_log_init_level(int level) { init_mutex(); @@ -274,7 +275,7 @@ void log_event_end(void (*func)(int, const char *, void *), void *info) 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] = ""; @@ -301,14 +302,14 @@ static void yaz_log_open_check(struct tm *tm, int force) 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 @@ -323,10 +324,23 @@ void yaz_log_reopen() #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) { @@ -374,7 +388,7 @@ static void yaz_log_to_file(int level, FILE *file, const char *buf) 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) diff --git a/src/nfaxml.c b/src/nfaxml.c index 4bd2c4e..c72b8f3 100644 --- a/src/nfaxml.c +++ b/src/nfaxml.c @@ -1,7 +1,7 @@ /* 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 $ */ /** @@ -14,7 +14,7 @@ #include -#include +/* #include */ #include #include @@ -27,33 +27,16 @@ /** \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 */ @@ -73,6 +56,16 @@ yaz_nfa *yaz_nfa_parse_xml_file(const char *filepath) { 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 */ diff --git a/src/test.c b/src/test.c index c7267e3..7d521b8 100644 --- a/src/test.c +++ b/src/test.c @@ -2,7 +2,7 @@ * 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 @@ -16,14 +16,17 @@ #include #include #include +#include #include +#include 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() { @@ -102,6 +105,18 @@ void yaz_check_init1(int *argc_p, char ***argv_p) *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 */ @@ -140,6 +155,7 @@ void yaz_check_print1(int type, const char *file, int line, const char *expr) { const char *msg = "unknown"; + int printit=1; test_total++; switch(type) @@ -148,16 +164,22 @@ void yaz_check_print1(int type, const char *file, int line, 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); + } } diff --git a/test/nfaxmltest1.c b/test/nfaxmltest1.c index 106c78d..38d48e3 100644 --- a/test/nfaxmltest1.c +++ b/test/nfaxmltest1.c @@ -1,7 +1,7 @@ /* 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 $ * */ @@ -17,6 +17,7 @@ #include +/** \brief Test parsing of a minimal, valid xml string */ void test1() { char *xmlstr=" " " " @@ -24,18 +25,34 @@ void test1() { " bar " "" ""; - 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=" " + " " + " foo " + " bar " + ""; + /* missing "" */ + 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; -- 1.7.10.4