MPSPARQL-19: Clean up logging
[mp-sparql-moved-to-github.git] / src / filter_sparql.cpp
index f168571..d9b51eb 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <boost/scoped_ptr.hpp>
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition.hpp>
+#include <boost/algorithm/string.hpp>
 #include "sparql.h"
 
 #include <yaz/zgdu.h>
@@ -179,21 +180,29 @@ void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only,
                     conf->schema = mp::xml::get_text(attr->children);
                 else if (!strcmp((const char *) attr->name, "include"))
                 {
-                    std::string name = mp::xml::get_text(attr->children);
-                    std::list<ConfPtr>::const_iterator it = db_conf.begin();
-                    while (1)
-                        if (it == db_conf.end())
-                        {
-                            throw mp::filter::FilterException(
-                                "include db not found: " + name);
-                        }
-                        else if (name.compare((*it)->db) == 0)
-                        {
-                            yaz_sparql_include(s, (*it)->s);
-                            break;
-                        }
-                        else
-                            it++;
+                    std::vector<std::string> dbs;
+                    std::string db = mp::xml::get_text(attr->children);
+                    boost::split(dbs, db, boost::is_any_of(" \t"));
+                    size_t i;
+                    for (i = 0; i < dbs.size(); i++)
+                    {
+                        if (dbs[i].length() == 0)
+                            continue;
+                        std::list<ConfPtr>::const_iterator it = db_conf.begin();
+                        while (1)
+                            if (it == db_conf.end())
+                            {
+                                throw mp::filter::FilterException(
+                                    "include db not found: " + dbs[i]);
+                            }
+                            else if (dbs[i].compare((*it)->db) == 0)
+                            {
+                                yaz_sparql_include(s, (*it)->s);
+                                break;
+                            }
+                            else
+                                it++;
+                    }
                 }
                 else
                     throw mp::filter::FilterException(
@@ -439,6 +448,7 @@ Z_Records *yf::SPARQL::Session::fetch(
     std::list<Result>::iterator it = fset->results.begin();
     const char *schema = 0;
     bool uri_lookup = false;
+    bool fetch_logged = false;
     if (esn && esn->which == Z_ElementSetNames_generic)
         schema = esn->u.generic;
 
@@ -497,7 +507,9 @@ Z_Records *yf::SPARQL::Session::fetch(
             {
                 if (n->type == XML_ELEMENT_NODE)
                 {
-                    if (!strcmp((const char *) n->name, "uri"))
+                    //if (!strcmp((const char *) n->name, "uri"))
+                    if (!strcmp((const char *) n->name, "uri") ||
+                        !strcmp((const char *) n->name, "bnode") )
                     {
                         uri = mp::xml::get_text(n->children);
 
@@ -525,7 +537,18 @@ Z_Records *yf::SPARQL::Session::fetch(
                                                       uri.c_str(), schema);
                 if (!error)
                 {
-                    yaz_log(YLOG_LOG, "query=%s", query.c_str());
+                    if (!fetch_logged)
+                    { // Log the fetch query only once
+                        package.log("sparql", YLOG_LOG,
+                            "fetch query: for %s \n%s",
+                            uri.c_str(), query.c_str() );
+                        fetch_logged = true;
+                    }
+                    else
+                    {
+                        package.log("sparql", YLOG_LOG,
+                            "fetch uri:%s", uri.c_str() );
+                    }
                     error = invoke_sparql(package, query.c_str(),
                                           it->conf, w);
                 }
@@ -592,7 +615,7 @@ int yf::SPARQL::Session::invoke_sparql(mp::Package &package,
     gdu->u.HTTP_Request->content_buf = path;
     gdu->u.HTTP_Request->content_len = strlen(path);
 
-    yaz_log(YLOG_LOG, "sparql: HTTP request\n%s", sparql_query);
+    yaz_log(YLOG_DEBUG, "sparql: HTTP request\n%s", sparql_query);
 
     http_package.request() = gdu;
     http_package.move();
@@ -606,8 +629,14 @@ int yf::SPARQL::Session::invoke_sparql(mp::Package &package,
     }
     else if (gdu_resp->u.HTTP_Response->code != 200)
     {
+        Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
         wrbuf_printf(w, "sparql: HTTP error %d from backend",
-                     gdu_resp->u.HTTP_Response->code);
+                     resp->code);
+        package.log("sparql", YLOG_LOG,
+            "HTTP error %d from backend ",
+            resp->code );
+        package.log("sparql", YLOG_LOG,
+            "%.*s" , resp->content_len, resp->content_buf );
         return YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
     }
     Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
@@ -625,6 +654,9 @@ Z_APDU *yf::SPARQL::Session::search(mp::Package &package,
     Z_APDU *apdu_res = 0;
     mp::wrbuf w;
 
+    package.log("sparql", YLOG_LOG,
+        "search query:\n%s", sparql_query );
+
     int error = invoke_sparql(package, sparql_query, conf, w);
     if (error)
     {
@@ -654,7 +686,7 @@ Z_APDU *yf::SPARQL::Session::search(mp::Package &package,
             result.doc = doc;
             result.conf = conf;
             fset->results.push_back(result);
-            yaz_log(YLOG_LOG, "saving sparql result xmldoc=%p", doc);
+            yaz_log(YLOG_DEBUG, "saving sparql result xmldoc=%p", doc);
 
             get_result(result.doc, &fset->hits, -1, 0);
             m_frontend_sets[req->resultSetName] = fset;