started access logging of Z39.50 packages, partially done init, search
authorMarc Cromme <marc@indexdata.dk>
Mon, 28 Aug 2006 21:40:24 +0000 (21:40 +0000)
committerMarc Cromme <marc@indexdata.dk>
Mon, 28 Aug 2006 21:40:24 +0000 (21:40 +0000)
and present. Still need to do error/diagnostic logging right for
these. All other package types still missing.

src/filter_log.cpp

index 2e60d6d..917cc1b 100644 (file)
@@ -1,14 +1,14 @@
-/* $Id: filter_log.cpp,v 1.21 2006-06-21 12:50:35 adam Exp $
+/* $Id: filter_log.cpp,v 1.22 2006-08-28 21:40:24 marc Exp $
    Copyright (c) 2005-2006, Index Data.
 
    See the LICENSE file for details
  */
 
 #include "config.hpp"
-
 #include "package.hpp"
 
 #include <string>
+#include <sstream>
 #include <boost/thread/mutex.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 
@@ -18,6 +18,9 @@
 
 #include <fstream>
 #include <yaz/zgdu.h>
+#include <yaz/wrbuf.h>
+#include <yaz/querytowrbuf.h>
+
 
 namespace mp = metaproxy_1;
 namespace yf = metaproxy_1::filter;
@@ -38,8 +41,9 @@ namespace metaproxy_1 {
             friend class Log;
             Rep();
             void openfile(const std::string &fname);
-            std::string m_msg;
+            std::string m_msg_config;
             LFilePtr m_file;
+            bool m_access;
             bool m_req_apdu;
             bool m_res_apdu;
             bool m_req_session;
@@ -63,7 +67,7 @@ yf::Log::Rep::Rep()
 
 yf::Log::Log(const std::string &x) : m_p(new Rep)
 {
-    m_p->m_msg = x;
+    m_p->m_msg_config = x;
 }
 
 yf::Log::Log() : m_p(new Rep)
@@ -92,13 +96,167 @@ void yf::Log::process(mp::Package &package) const
     boost::posix_time::ptime receive_time
         = boost::posix_time::microsec_clock::local_time();
 
+
+    std::ostringstream msg_request;
+    std::ostringstream msg_request_2;
+    std::ostringstream msg_response;
+    std::ostringstream msg_response_2;
+
     // scope for locking Ostream 
     { 
         boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
         
+        if (m_p->m_access)
+        {
+            
+            gdu = package.request().get();
+            WRBUF wr = wrbuf_alloc();
+
+           if (gdu && gdu->which == Z_GDU_Z3950)          
+                {
+                  
+                    msg_request << "Z39.50" << " ";
+
+                    switch(gdu->u.z3950->which)
+                    {
+                    case Z_APDU_initRequest:
+                        msg_request 
+                            << "initRequest" << " "
+                            << "OK" << " ";
+                        
+                        {
+                            Z_InitRequest *ir 
+                                = gdu->u.z3950->u.initRequest;
+                            msg_request_2 
+                                << (ir->implementationId) << " "
+                                //<< ir->referenceId << " "
+                                << (ir->implementationName) << " "
+                                << (ir->implementationVersion) << " ";
+                         }
+                        break;
+                    case Z_APDU_searchRequest:
+                        msg_request 
+                            << "searchRequest" << " "
+                            << "OK" << " ";
+                        {
+                            Z_SearchRequest *sr 
+                                = gdu->u.z3950->u.searchRequest;
+                            
+                            for (int i = 0; i < sr->num_databaseNames; i++)
+                            {
+                                msg_request << sr->databaseNames[i];
+                                if (i+1 ==  sr->num_databaseNames)
+                                    msg_request << " ";
+                                else
+                                    msg_request << "+";
+                            }
+                         
+                            yaz_query_to_wrbuf(wr, sr->query);
+                        }
+                        msg_request_2 << wrbuf_buf(wr) << " ";
+                        break;
+                    case Z_APDU_presentRequest:
+                        msg_request 
+                            << "presentRequest" << " "
+                            << "OK" << " "; 
+                        {
+                            Z_PresentRequest *pr 
+                                = gdu->u.z3950->u.presentRequest;
+                                msg_request_2 
+                                    << pr->resultSetId << " "
+                                    //<< pr->referenceId << " "
+                                    << *(pr->resultSetStartPoint) << " "
+                                    << *(pr->numberOfRecordsRequested) << " ";
+                        }
+                        break;
+                    case Z_APDU_deleteResultSetRequest:
+                        msg_request 
+                            << "deleteResultSetRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_accessControlRequest:
+                        msg_request 
+                            << "accessControlRequest" << " "
+                            << "OK" << " "; 
+                        break;
+                    case Z_APDU_resourceControlRequest:
+                        msg_request 
+                            << "resourceControlRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_triggerResourceControlRequest:
+                        msg_request 
+                            << "triggerResourceControlRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_resourceReportRequest:
+                        msg_request 
+                            << "resourceReportRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_scanRequest:
+                        msg_request 
+                            << "scanRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_sortRequest:
+                        msg_request 
+                            << "sortRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_segmentRequest:
+                        msg_request 
+                            << "segmentRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_extendedServicesRequest:
+                        msg_request 
+                            << "extendedServicesRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_close:
+                        msg_response 
+                            << "close" << " "
+                            << "OK" << " ";
+                        break;
+                    case Z_APDU_duplicateDetectionRequest:
+                        msg_request 
+                            << "duplicateDetectionRequest" << " "
+                            << "OK" << " ";
+                        break;
+                    default: 
+                        msg_request 
+                            << "unknown" << " "
+                            << "ERROR" << " ";
+                    }
+                }
+           else if (gdu && gdu->which == Z_GDU_HTTP_Request)
+               msg_request << "HTTP " << "unknown " ;
+           else if (gdu && gdu->which == Z_GDU_HTTP_Response)
+               msg_request << "HTTP-Response " << "unknown " ;
+           else
+               msg_request << "unknown " << "unknown " ;
+
+           wrbuf_free(wr, 1);
+
+           m_p->m_file->out
+               << m_p->m_msg_config << " "
+               << package.session().id() << " "
+               << receive_time << " "
+               // << send_time << " "
+               << "00:00:00.000000" << " " 
+               // << duration  << " "
+               << msg_request.str()
+               << msg_request_2.str()
+               //<< msg_response.str()
+               //<< msg_response_2.str()
+               << "\n";
+        }
+
         if (m_p->m_req_session)
         {
-            m_p->m_file->out << receive_time << " " << m_p->m_msg;
+            m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
             m_p->m_file->out << " request id=" << package.session().id();
             m_p->m_file->out << " close=" 
                              << (package.session().is_closed() ? "yes" : "no")
@@ -110,7 +268,7 @@ void yf::Log::process(mp::Package &package) const
             if (gdu && gdu->which == Z_GDU_Z3950 &&
                 gdu->u.z3950->which == Z_APDU_initRequest)
             {
-                m_p->m_file->out << receive_time << " " << m_p->m_msg;
+                m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
                 m_p->m_file->out << " init options:";
                 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
                                     option_write, m_p->m_file.get());
@@ -143,9 +301,142 @@ void yf::Log::process(mp::Package &package) const
     // scope for locking Ostream 
     { 
         boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
+        if (m_p->m_access)
+        {
+            gdu = package.response().get();
+            //WRBUF wr = wrbuf_alloc();
+
+
+           if (gdu && gdu->which == Z_GDU_Z3950)          
+                {
+                  
+                    msg_response << "Z39.50" << " ";
+
+                    switch(gdu->u.z3950->which)
+                    {
+                    case Z_APDU_initResponse:
+                        msg_response << "initResponse" << " ";
+                        {
+                            Z_InitResponse *ir 
+                                = gdu->u.z3950->u.initResponse;
+                            if (*(ir->result))
+                                msg_response_2 
+                                    << "OK" << " "
+                                    << (ir->implementationId) << " "
+                                    //<< ir->referenceId << " "
+                                    << (ir->implementationName) << " "
+                                    << (ir->implementationVersion) << " ";
+                            else
+                                msg_response_2 
+                                    << "ERROR" << " "
+                                    << "- - -" << " ";
+
+                         }
+                        break;
+                    case Z_APDU_searchResponse:
+                        msg_response << "searchResponse" << " ";
+                        {
+                            Z_SearchResponse *sr 
+                                = gdu->u.z3950->u.searchResponse;
+                            if (*(sr->searchStatus))
+                                msg_response_2 
+                                    << "OK" << " "
+                                    << *(sr->resultCount) << " "
+                                    //<< sr->referenceId << " "
+                                    << *(sr->numberOfRecordsReturned) << " "
+                                    << *(sr->nextResultSetPosition) << " ";
+                            else
+                                msg_response_2 
+                                    << "ERROR" << " "
+                                    << "- - -" << " ";
+
+                         }
+                        //msg_response << wrbuf_buf(wr) << " ";
+                        break;
+                    case Z_APDU_presentResponse:
+                        msg_response << "presentResponse" << " ";
+                        {
+                            Z_PresentResponse *pr 
+                                = gdu->u.z3950->u.presentResponse;
+                            if (!*(pr->presentStatus))
+                                msg_response_2 
+                                    << "OK" << " "
+                                    << "-" << " "
+                                    //<< pr->referenceId << " "
+                                    << *(pr->numberOfRecordsReturned) << " "
+                                    << *(pr->nextResultSetPosition) << " ";
+                            else
+                                msg_response_2 
+                                    << "ERROR" << " "
+                                    << "-" << " "
+                                    //<< pr->referenceId << " "
+                                    << *(pr->numberOfRecordsReturned) << " "
+                                    << *(pr->nextResultSetPosition) << " ";
+                                    //<< "- - -" << " ";
+                         }
+                        break;
+                    case Z_APDU_deleteResultSetResponse:
+                        msg_response << "deleteResultSetResponse" << " ";
+                        break;
+                    case Z_APDU_accessControlResponse:
+                        msg_response << "accessControlResponse" << " ";
+                        break;
+                    case Z_APDU_resourceControlResponse:
+                        msg_response << "resourceControlResponse" << " ";
+                        break;
+                        //case Z_APDU_triggerResourceControlResponse:
+                        //msg_response << "triggerResourceControlResponse" << " ";
+                        //break;
+                    case Z_APDU_resourceReportResponse:
+                        msg_response << "resourceReportResponse" << " ";
+                        break;
+                    case Z_APDU_scanResponse:
+                        msg_response << "scanResponse" << " ";
+                        break;
+                    case Z_APDU_sortResponse:
+                        msg_response << "sortResponse" << " ";
+                        break;
+                        // case Z_APDU_segmentResponse:
+                        // msg_response << "segmentResponse" << " ";
+                        // break;
+                    case Z_APDU_extendedServicesResponse:
+                        msg_response << "extendedServicesResponse" << " ";
+                        break;
+                    case Z_APDU_close:
+                        msg_response << "close" << " ";
+                        break;
+                    case Z_APDU_duplicateDetectionResponse:
+                        msg_response << "duplicateDetectionResponse" << " ";
+                        break;
+                    default: 
+                        msg_response << "unknown" << " ";
+                    }
+                }
+           else if (gdu && gdu->which == Z_GDU_HTTP_Request)
+               msg_response << "HTTP " << "unknown " ;
+           else if (gdu && gdu->which == Z_GDU_HTTP_Response)
+               msg_response << "HTTP-Response " << "unknown " ;
+           else
+               msg_response << "unknown " << "unknown " ;
+
+           m_p->m_file->out
+               << m_p->m_msg_config << " "
+               << package.session().id() << " "
+               // << receive_time << " "
+                << send_time << " "
+               //<< "-" << " "
+                << duration  << " "
+               //<< msg_request.str()
+               //<< msg_request_2.str()
+               << msg_response.str()
+               << msg_response_2.str()
+               << "\n";
+
+            //wrbuf_free(wr, 1);
+        }
         if (m_p->m_res_session)
         {
-            m_p->m_file->out << send_time << " " << m_p->m_msg;
+            m_p->m_file->out << send_time << " " << m_p->m_msg_config;
             m_p->m_file->out << " response id=" << package.session().id();
             m_p->m_file->out << " close=" 
                              << (package.session().is_closed() ? "yes " : "no ")
@@ -158,7 +449,7 @@ void yf::Log::process(mp::Package &package) const
             if (gdu && gdu->which == Z_GDU_Z3950 &&
                 gdu->u.z3950->which == Z_APDU_initResponse)
             {
-                m_p->m_file->out << receive_time << " " << m_p->m_msg;
+                m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
                 m_p->m_file->out << " init options:";
                 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
                                     option_write, m_p->m_file.get());
@@ -215,7 +506,7 @@ void yf::Log::configure(const xmlNode *ptr)
         if (ptr->type != XML_ELEMENT_NODE)
             continue;
         if (!strcmp((const char *) ptr->name, "message"))
-            m_p->m_msg = mp::xml::get_text(ptr);
+            m_p->m_msg_config = mp::xml::get_text(ptr);
         else if (!strcmp((const char *) ptr->name, "filename"))
         {
             std::string fname = mp::xml::get_text(ptr);
@@ -226,7 +517,11 @@ void yf::Log::configure(const xmlNode *ptr)
             const struct _xmlAttr *attr;
             for (attr = ptr->properties; attr; attr = attr->next)
             {
-                if (!strcmp((const char *) attr->name, "request-apdu"))
+                if (!strcmp((const char *) attr->name, 
+                                 "access"))
+                    m_p->m_access = 
+                        mp::xml::get_bool(attr->children, true);
+                else if (!strcmp((const char *) attr->name, "request-apdu"))
                     m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
                 else if (!strcmp((const char *) attr->name, "response-apdu"))
                     m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);