Module: sems Branch: master Commit: b19122046c6d7d94159a12a7d681236f2d368cb3 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=b19122046c6d7d94159a12a7d681236f2d368cb3
Author: Stefan Sayer <[email protected]> Committer: Stefan Sayer <[email protected]> Date: Mon Apr 16 16:58:58 2012 +0200 DSM: mod_xml: libxml logging to SEMS log, added xml.setLogLevel function --- apps/dsm/mods/mod_xml/ModXml.cpp | 48 ++++++++++++++++++++++++++++++++++++- apps/dsm/mods/mod_xml/ModXml.h | 1 + doc/dsm/mods/Readme.mod_xml.txt | 4 ++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/apps/dsm/mods/mod_xml/ModXml.cpp b/apps/dsm/mods/mod_xml/ModXml.cpp index c71b259..de65884 100644 --- a/apps/dsm/mods/mod_xml/ModXml.cpp +++ b/apps/dsm/mods/mod_xml/ModXml.cpp @@ -32,10 +32,16 @@ SC_EXPORT(MOD_CLS_NAME); +void xml_err_func(void *ctx, const char *msg, ...); +xmlGenericErrorFunc handler = (xmlGenericErrorFunc)xml_err_func; +int xml_log_level = L_ERR; + int MOD_CLS_NAME::preload() { DBG("initializing libxml2...\n"); xmlInitParser(); - + initGenericErrorDefaultFunc(&handler); + handler = (xmlGenericErrorFunc)xml_err_func; + xmlSetGenericErrorFunc(NULL, &xml_err_func); return 0; } @@ -46,6 +52,8 @@ MOD_ACTIONEXPORT_BEGIN(MOD_CLS_NAME) { DEF_CMD("xml.evalXPath", MODXMLEvalXPathAction); DEF_CMD("xml.XPathResultCount", MODXMLXPathResultNodeCount); + DEF_CMD("xml.setLoglevel", MODXMLSetLogLevelAction); + } MOD_ACTIONEXPORT_END; MOD_CONDITIONEXPORT_NONE(MOD_CLS_NAME); @@ -68,6 +76,17 @@ ModXmlXPathObj::~ModXmlXPathObj() { } } +#define TMP_BUF_SIZE 256 +void xml_err_func(void *ctx, const char *msg, ...) { + char _string[TMP_BUF_SIZE]; + va_list arg_ptr; + va_start(arg_ptr, msg); + vsnprintf(_string, TMP_BUF_SIZE, msg, arg_ptr); + va_end(arg_ptr); + + _LOG(xml_log_level, "%s", _string); +} + CONST_ACTION_2P(MODXMLParseSIPMsgBodyAction, ',', false); EXEC_ACTION_START(MODXMLParseSIPMsgBodyAction) { string msgbody_var = resolveVars(par1, sess, sc_sess, event_params); @@ -94,6 +113,8 @@ EXEC_ACTION_START(MODXMLParseSIPMsgBodyAction) { EXEC_ACTION_STOP; } + xmlSetGenericErrorFunc(NULL, &xml_err_func); + xmlDocPtr doc = xmlReadMemory((const char*)b, msgbody->getLen(), "noname.xml", NULL, 0); if (doc == NULL) { @@ -103,6 +124,8 @@ EXEC_ACTION_START(MODXMLParseSIPMsgBodyAction) { EXEC_ACTION_STOP; } + xmlSetGenericErrorFunc(doc, &xml_err_func); + ModXmlDoc* xml_doc = new ModXmlDoc(doc); sc_sess->avar[dstname] = xml_doc; DBG("parsed XML body document to '%s'\n", dstname.c_str()); @@ -115,6 +138,8 @@ EXEC_ACTION_START(MODXMLParseAction) { string xml_doc = resolveVars(par1, sess, sc_sess, event_params); string dstname = resolveVars(par2, sess, sc_sess, event_params); + xmlSetGenericErrorFunc(NULL, &xml_err_func); + xmlDocPtr doc = xmlReadMemory(xml_doc.c_str(), xml_doc.length(), "noname.xml", NULL, 0); if (doc == NULL) { @@ -123,6 +148,7 @@ EXEC_ACTION_START(MODXMLParseAction) { sc_sess->SET_STRERROR("failed parsing XML document from " + xml_doc); EXEC_ACTION_STOP; } + xmlSetGenericErrorFunc(doc, &xml_err_func); ModXmlDoc* xml_doc_var = new ModXmlDoc(doc); sc_sess->avar[dstname] = xml_doc_var; @@ -154,6 +180,8 @@ EXEC_ACTION_START(MODXMLEvalXPathAction) { string xpath_expr = resolveVars(par1, sess, sc_sess, event_params); string xml_doc_var = resolveVars(par2, sess, sc_sess, event_params); + xmlSetGenericErrorFunc(NULL, &xml_err_func); + ModXmlDoc* xml_doc = getXMLElemFromVariable<ModXmlDoc>(sc_sess, xml_doc_var); if (NULL == xml_doc) EXEC_ACTION_STOP; @@ -167,6 +195,7 @@ EXEC_ACTION_START(MODXMLEvalXPathAction) { sc_sess->SET_STRERROR("unable to create new XPath context"); EXEC_ACTION_STOP; } + xmlSetGenericErrorFunc(xpathCtx, &xml_err_func); string xml_doc_ns = sc_sess->var[xml_doc_var+".ns"]; vector<string> ns_entries = explode(xml_doc_ns, " "); @@ -209,7 +238,6 @@ EXEC_ACTION_START(MODXMLEvalXPathAction) { } EXEC_ACTION_END; - CONST_ACTION_2P(MODXMLXPathResultNodeCount, '=', false); EXEC_ACTION_START(MODXMLXPathResultNodeCount) { string cnt_var = par1; @@ -234,3 +262,19 @@ EXEC_ACTION_START(MODXMLXPathResultNodeCount) { DBG("set count $%s=%u\n", cnt_var.c_str(), res); } EXEC_ACTION_END; + +EXEC_ACTION_START(MODXMLSetLogLevelAction) { + string xml_log_level_s = resolveVars(arg, sess, sc_sess, event_params); + if (xml_log_level_s == "error") + xml_log_level = L_ERR; + else if (xml_log_level_s == "warn") + xml_log_level = L_WARN; + else if (xml_log_level_s == "info") + xml_log_level = L_INFO; + else if (xml_log_level_s == "debug") + xml_log_level = L_DBG; + else { + ERROR("script writer error: '%s' is no valid log level (error, warn, info, debug)\n", + xml_log_level_s.c_str()); + } +} EXEC_ACTION_END; diff --git a/apps/dsm/mods/mod_xml/ModXml.h b/apps/dsm/mods/mod_xml/ModXml.h index bc3d1b3..5b9ac21 100644 --- a/apps/dsm/mods/mod_xml/ModXml.h +++ b/apps/dsm/mods/mod_xml/ModXml.h @@ -47,6 +47,7 @@ DEF_ACTION_2P(MODXMLParseSIPMsgBodyAction); DEF_ACTION_2P(MODXMLParseAction); DEF_ACTION_2P(MODXMLEvalXPathAction); DEF_ACTION_2P(MODXMLXPathResultNodeCount); +DEF_ACTION_1P(MODXMLSetLogLevelAction); class ModXmlDoc : public DSMDisposable, diff --git a/doc/dsm/mods/Readme.mod_xml.txt b/doc/dsm/mods/Readme.mod_xml.txt index 5985449..d096e81 100644 --- a/doc/dsm/mods/Readme.mod_xml.txt +++ b/doc/dsm/mods/Readme.mod_xml.txt @@ -27,4 +27,6 @@ xml.XPathResultCount($cntvar=xpath_object) Example: xml.XPathResultCount($rescnt="substatus.xpath"); - +xml.setLoglevel(level) + set libxml2 error logging level. Default: error + Valid: error, warn, info, debug _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
