zoom: tweak diagnostics for Torus auth
[metaproxy-moved-to-github.git] / src / filter_log.cpp
index f7ea13c..7b3a2aa 100644 (file)
@@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <metaproxy/util.hpp>
 #include <metaproxy/xmlutil.hpp>
 
+#include <yaz/oid_db.h>
 #include <yaz/zgdu.h>
 #include <yaz/wrbuf.h>
 #include <yaz/log.h>
@@ -149,13 +150,23 @@ yf::Log::Impl::~Impl()
 static void log_DefaultDiagFormat(WRBUF w, Z_DefaultDiagFormat *e)
 {
     if (e->condition)
-        wrbuf_printf(w, ODR_INT_PRINTF " ",*e->condition);
+        wrbuf_printf(w, ODR_INT_PRINTF " ", *e->condition);
     else
         wrbuf_puts(w, "?? ");
     if (e->which == Z_DefaultDiagFormat_v2Addinfo && e->u.v2Addinfo)
+    {
+        wrbuf_puts(w, "\"");
         wrbuf_puts(w, e->u.v2Addinfo);
+        wrbuf_puts(w, "\"");
+    }
     else if (e->which == Z_DefaultDiagFormat_v3Addinfo && e->u.v3Addinfo)
+    {
+        wrbuf_puts(w, "\"");
         wrbuf_puts(w, e->u.v3Addinfo);
+        wrbuf_puts(w, "\"");
+    }
+    else
+        wrbuf_puts(w, "-");
 }
 
 static void log_DiagRecs(WRBUF w, int num_diagRecs, Z_DiagRec **diags)
@@ -169,13 +180,26 @@ static void log_DiagRecs(WRBUF w, int num_diagRecs, Z_DiagRec **diags)
     }
 }
 
-static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
+static void log_syntax(WRBUF w, const Odr_oid *syntax)
+{
+    if (syntax)
+    {
+        char oid_name[OID_STR_MAX+1];
+        wrbuf_puts(w, yaz_oid_to_string_buf(syntax, 0, oid_name));
+    }
+    else
+        wrbuf_puts(w, "-");
+}
+
+static void log_1line_Z_APDU(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
 {
     switch (z_req->which)
     {
     case Z_APDU_initRequest:
         wrbuf_puts(w, "Init ");
-        if (z_res->which != Z_APDU_initResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_initResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -229,7 +253,9 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         break;
     case Z_APDU_searchRequest:
         wrbuf_puts(w, "Search ");
-        if (z_res->which != Z_APDU_searchResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_searchResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -267,6 +293,8 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
                     res->records->u.multipleNonSurDiagnostics->num_diagRecs,
                     res->records->u.multipleNonSurDiagnostics->diagRecs);
             }
+            wrbuf_puts(w, " ");
+            log_syntax(w, req->preferredRecordSyntax);
             wrbuf_printf(w, " 1+" ODR_INT_PRINTF " ",
                          res->numberOfRecordsReturned
                          ? *res->numberOfRecordsReturned : 0);
@@ -275,7 +303,9 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         break;
     case Z_APDU_presentRequest:
         wrbuf_puts(w, "Present ");
-        if (z_res->which != Z_APDU_presentResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_presentResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -304,6 +334,10 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
                     res->records->u.multipleNonSurDiagnostics->num_diagRecs,
                     res->records->u.multipleNonSurDiagnostics->diagRecs);
             }
+            wrbuf_puts(w, " ");
+            assert(req->preferredRecordSyntax);
+            log_syntax(w, req->preferredRecordSyntax);
+                       
             wrbuf_printf(w, " %s " ODR_INT_PRINTF "+" ODR_INT_PRINTF " ",
                 req->resultSetId, *req->resultSetStartPoint,
                          *req->numberOfRecordsRequested);
@@ -326,7 +360,9 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         break;
     case Z_APDU_scanRequest:
         wrbuf_puts(w, "Scan ");
-        if (z_res->which != Z_APDU_scanResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_scanResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -362,7 +398,9 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         break;
     case Z_APDU_sortRequest:
         wrbuf_puts(w, "sort ");
-        if (z_res->which != Z_APDU_sortResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_sortResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -398,8 +436,10 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         wrbuf_puts(w, "segmentRequest");
         break;
     case Z_APDU_extendedServicesRequest:
-        wrbuf_puts(w, "extendedServices");
-        if (z_res->which != Z_APDU_extendedServicesResponse)
+        wrbuf_puts(w, "extendedServices ");
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_extendedServicesResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -428,7 +468,9 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
         break;
     case Z_APDU_duplicateDetectionRequest:
         wrbuf_puts(w, "duplicateDetention ");
-        if (z_res->which != Z_APDU_duplicateDetectionResponse)
+        if (!z_res)
+            wrbuf_puts(w, "?");
+        else if (z_res->which != Z_APDU_duplicateDetectionResponse)
             wrbuf_printf(w, "? response=%d", z_res->which);
         else
         {
@@ -451,6 +493,32 @@ static void log_1_line(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
     }
 }
 
+
+static void log_1line_Z_HTTP(Z_HTTP_Request *req, Z_HTTP_Response *res, WRBUF w)
+{
+    wrbuf_printf(w, "%s %s HTTP/%s", req->method, req->path, req->version);
+    if (res)
+        wrbuf_printf(w, " %d %d", res->code, res->content_len);
+    else
+        wrbuf_printf(w, " ?");
+}
+
+static void log_1line_Z_GDU(Z_GDU *gdu_req, Z_GDU *gdu_res, WRBUF w)
+{
+    if (gdu_req && gdu_req->which == Z_GDU_Z3950)
+    {
+        log_1line_Z_APDU(gdu_req->u.z3950, 
+                         (gdu_res && gdu_res->which == Z_GDU_Z3950) ?
+                         gdu_res->u.z3950 : 0, w);
+    }
+    else if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
+    {
+        log_1line_Z_HTTP(gdu_req->u.HTTP_Request,
+                         (gdu_res && gdu_res->which == Z_GDU_HTTP_Response) ?
+                         gdu_res->u.HTTP_Response : 0, w);
+    }
+}
+
 void yf::Log::Impl::configure(const xmlNode *ptr)
 {
     for (ptr = ptr->children; ptr; ptr = ptr->next)
@@ -641,14 +709,12 @@ void yf::Log::Impl::process(mp::Package &package)
         
         if (m_1line)
         {
-            if (gdu_req && gdu_res && gdu_req->which == Z_GDU_Z3950
-                && gdu_res->which == Z_GDU_Z3950)
+            mp::wrbuf w;
+            
+            log_1line_Z_GDU(gdu_req, gdu_res, w);
+            if (w.len() > 0)
             {
-                mp::wrbuf w;
-
-                log_1_line(gdu_req->u.z3950, gdu_res->u.z3950, w);
                 const char *message = wrbuf_cstr(w);
-
                 std::ostringstream os;
                 os  << m_msg_config << " "
                     << package << " "