Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / gduutil.cpp
index 7fd718f..b39fd28 100644 (file)
@@ -1,7 +1,5 @@
-/* $Id: gduutil.cpp,v 1.22 2007-05-22 13:03:32 adam Exp $
-   Copyright (c) 2005-2007, Index Data.
-
-This file is part of Metaproxy.
+/* This file is part of Metaproxy.
+   Copyright (C) Index Data
 
 Metaproxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -14,29 +12,24 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Metaproxy; see the file LICENSE.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
- */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
 
 #include "gduutil.hpp"
-#include "util.hpp"
+#include <metaproxy/util.hpp>
 
 #include <yaz/wrbuf.h>
 #include <yaz/oid_db.h>
 #include <yaz/querytowrbuf.h>
 
 #include <iostream>
-#include <list>
 
 namespace mp = metaproxy_1;
 
 // Doxygen doesn't like mp::gdu, so we use this instead
 namespace mp_util = metaproxy_1::util;
 
-
-
-
 std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
 {
     if (zgdu.which == Z_GDU_Z3950)
@@ -65,21 +58,21 @@ std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Request& httpreq)
 {
     os << httpreq.method << " ";
-    os << httpreq.path;    
+    os << httpreq.path;
     return os;
 }
 
-
 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Response& httpres)
 {
     os << httpres.code << " ";
-    os << httpres.content_len;   
+    os << httpres.content_len;
     return os;
 }
 
 std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
 {
-    switch(rs.which) {
+    switch (rs.which)
+    {
     case Z_Records_DBOSD :
         break;
     case Z_Records_NSD:
@@ -87,19 +80,33 @@ std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
             os << *(rs.u.nonSurrogateDiagnostic);
         break;
     case Z_Records_multipleNSD:
-        os << "Z_Records_multipleNSD";
-        //os << *(rs.u.multipleNonSurDiagnostics);
+        os << *(rs.u.multipleNonSurDiagnostics);
         break;
     default:
         os << "Z_Records" ;
     }
-    
+
+    return os;
+}
+
+std::ostream& std::operator<<(std::ostream& os, Z_DiagRecs& dr)
+{
+    if (dr.num_diagRecs >= 1)
+    {
+        os << *dr.diagRecs[0];
+    }
+    else
+    {
+        os << "multipleNSD";
+    }
     return os;
 }
 
+
 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
 {
-    switch(dr.which) {
+    switch (dr.which)
+    {
     case Z_DiagRec_defaultFormat:
         if (dr.u.defaultFormat)
             os << *(dr.u.defaultFormat);
@@ -110,7 +117,7 @@ std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
     default:
         os << "Z_DiagRec" ;
     }
-    
+
     return os;
 }
 
@@ -119,17 +126,22 @@ std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
     if (ddf.condition)
         os << *(ddf.condition) << " ";
 
-    switch(ddf.which) {
+    switch (ddf.which)
+    {
     case Z_DefaultDiagFormat_v2Addinfo:
+        os << "\"";
         os << ddf.u.v2Addinfo;
+        os << "\"";
         break;
     case Z_DefaultDiagFormat_v3Addinfo:
+        os << "\"";
         os << ddf.u.v3Addinfo;
+        os << "\"";
         break;
     default:
-        os << "Z_DefaultDiagFormat" ;
+        os << "-";
     }
-    
+
     return os;
 }
 
@@ -142,24 +154,29 @@ static void dump_opt_string(std::ostream& os, const char *s)
         os << "-";
 }
 
-static void dump_opt_int(std::ostream& os, const int *i)
+static void dump_opt_int_l(std::ostream& os, const Odr_int *i, const char *lead)
 {
-    os << " ";
+    os << lead;
     if (i)
         os << *i;
     else
         os << "-";
 }
 
+static void dump_opt_int(std::ostream& os, const Odr_int *i)
+{
+    dump_opt_int_l(os, i, " ");
+}
+
 std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
 {
     switch(zapdu.which) {
 
     case Z_APDU_initRequest:
         os << " " << "initRequest";
-                        
+
         {
-            Z_InitRequest *ir 
+            Z_InitRequest *ir
                 = zapdu.u.initRequest;
 
             Z_IdAuthentication *a = ir->idAuthentication;
@@ -169,17 +186,17 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 dump_opt_string(os, a->u.open);
             else
                 dump_opt_string(os, 0);
-            
+
             os << " ";
             std::list<std::string> vhosts;
             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
             if (vhosts.size()){
-                copy(vhosts.begin(), vhosts.end(), 
+                copy(vhosts.begin(), vhosts.end(),
                      ostream_iterator<string>(os, " "));
             }
             else
                 os << "-" ;
-            
+
             dump_opt_string(os, ir->implementationId);
             dump_opt_string(os, ir->implementationName);
             dump_opt_string(os, ir->implementationVersion);
@@ -188,7 +205,7 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
     case Z_APDU_initResponse:
         os << " " << "initResponse ";
         {
-            Z_InitResponse *ir 
+            Z_InitResponse *ir
                 = zapdu.u.initResponse;
             if (ir->result && *(ir->result))
             {
@@ -205,10 +222,10 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
         break;
     case Z_APDU_searchRequest:
         os << " " << "searchRequest" << " ";
-        { 
-            Z_SearchRequest *sr 
+        {
+            Z_SearchRequest *sr
                 = zapdu.u.searchRequest;
-                            
+
             for (int i = 0; i < sr->num_databaseNames; i++)
             {
                 os << sr->databaseNames[i];
@@ -222,38 +239,38 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
             if (sr->preferredRecordSyntax)
             {
                 char oid_name_str[OID_STR_MAX];
-                const char *oid_name = yaz_oid_to_string_buf(
+                os << yaz_oid_to_string_buf(
                     sr->preferredRecordSyntax, 0, oid_name_str);
-                
-                os << oid_name;
             }
             else
                 os << "-";
 
             os << " ";
-            WRBUF wr = wrbuf_alloc();
+            mp::wrbuf wr;
             yaz_query_to_wrbuf(wr, sr->query);
             os << wrbuf_cstr(wr);
-            wrbuf_destroy(wr);
         }
         break;
     case Z_APDU_searchResponse:
         os << " " << "searchResponse ";
         {
-            Z_SearchResponse *sr 
-                = zapdu.u.searchResponse;
-            if (sr->searchStatus && *(sr->searchStatus))
-            {
+            Z_SearchResponse *sr = zapdu.u.searchResponse;
+            if (!sr->searchStatus)
+                os << "Unknown";
+            else if (*sr->searchStatus)
                 os << "OK";
+            else
+                os << "Failure";
+            if (sr->records && sr->records->which != Z_Records_DBOSD)
+            {
+                os << " DIAG " << *sr->records;
+            }
+            else
+            {
                 dump_opt_int(os, sr->resultCount);
-                dump_opt_int(os, sr->numberOfRecordsReturned);
+                dump_opt_int_l(os, sr->numberOfRecordsReturned, " 1+");
                 dump_opt_int(os, sr->nextResultSetPosition);
             }
-            else 
-                if (sr->records)
-                    os << "DIAG " << *(sr->records);
-                else
-                    os << "ERROR";
         }
         break;
     case Z_APDU_presentRequest:
@@ -262,13 +279,13 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
             Z_PresentRequest *pr = zapdu.u.presentRequest;
             dump_opt_string(os, pr->resultSetId);
             dump_opt_int(os, pr->resultSetStartPoint);
-            dump_opt_int(os, pr->numberOfRecordsRequested);
+            dump_opt_int_l(os, pr->numberOfRecordsRequested, "+");
             if (pr->preferredRecordSyntax)
             {
                 char oid_name_str[OID_STR_MAX];
                 const char *oid_name = yaz_oid_to_string_buf(
                     pr->preferredRecordSyntax, 0, oid_name_str);
-                    
+
                 os << " " << oid_name;
             }
             else
@@ -282,32 +299,37 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
     case Z_APDU_presentResponse:
         os << " " << "presentResponse" << " ";
         {
-            Z_PresentResponse *pr 
+            Z_PresentResponse *pr
                 = zapdu.u.presentResponse;
-            if ((pr->presentStatus) && !*(pr->presentStatus))
+            if (!pr->presentStatus)
+                os << "Unknown";
+            else
             {
-                os << "OK";
-                //<< pr->referenceId << " "
-                if (pr->numberOfRecordsReturned)
-                    os << " " << *(pr->numberOfRecordsReturned);
-                else
-                    os << " -";
-                if (pr->nextResultSetPosition)
-                    os << " " << *(pr->nextResultSetPosition);
-                else
-                    os << " -";
+                switch (*pr->presentStatus)
+                {
+                case Z_PresentStatus_success:
+                    os << "OK"; break;
+                case Z_PresentStatus_partial_1:
+                    os << "Partial-1"; break;
+                case Z_PresentStatus_partial_2:
+                    os << "Partial-2"; break;
+                case Z_PresentStatus_partial_3:
+                    os << "Partial-3"; break;
+                case Z_PresentStatus_partial_4:
+                    os << "Partial-4"; break;
+                case Z_PresentStatus_failure:
+                    os << "Failure"; break;
+                default:
+                    os << "Unknown"; break;
+                }
             }
+            if (pr->records && pr->records->which != Z_Records_DBOSD)
+                os << " DIAG " << *pr->records;
             else
-                if (pr->records)
-                    os << "DIAG " << *(pr->records);
-                else
-                    os << "ERROR";
-
-            //os << "DIAG" << " "
-            //<< "-" << " "
-            //<< pr->referenceId << " "
-            //<< *(pr->numberOfRecordsReturned) << " "
-            //<< *(pr->nextResultSetPosition);
+            {
+                dump_opt_int(os, pr->numberOfRecordsReturned);
+                dump_opt_int(os, pr->nextResultSetPosition);
+            }
         }
         break;
     case Z_APDU_deleteResultSetRequest:
@@ -339,10 +361,9 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
         break;
     case Z_APDU_scanRequest:
         os << " " << "scanRequest" << " ";
-        { 
-            Z_ScanRequest *sr 
-                = zapdu.u.scanRequest;
-                        
+        {
+            Z_ScanRequest *sr = zapdu.u.scanRequest;
+
             if (sr)
             {
                 for (int i = 0; i < sr->num_databaseNames; i++)
@@ -358,11 +379,10 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 os << " ";
                 if (sr->termListAndStartPoint)
                 {
-                    WRBUF wr = wrbuf_alloc();
-                    yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, 
+                    mp::wrbuf wr;
+                    yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint,
                                       sr->attributeSet);
                     os << wrbuf_cstr(wr);
-                    wrbuf_destroy(wr);
                 }
                 else
                     os << "-";
@@ -372,17 +392,15 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
     case Z_APDU_scanResponse:
         os << " " << "scanResponse" << " ";
         {
-            Z_ScanResponse *sr 
-                = zapdu.u.scanResponse;
+            Z_ScanResponse *sr = zapdu.u.scanResponse;
             if (sr)
             {
                 if (!sr->scanStatus)
-                {
                     os << "OK";
-                }
                 else
                 {
-                    switch (*(sr->scanStatus)){
+                    switch (*sr->scanStatus)
+                    {
                     case Z_Scan_success:
                         os << "OK";
                         break;
@@ -425,8 +443,8 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
         break;
     case Z_APDU_extendedServicesRequest:
         os << " " << "extendedServicesRequest";
-        { 
-            Z_ExtendedServicesRequest *er 
+        {
+            Z_ExtendedServicesRequest *er
                 = zapdu.u.extendedServicesRequest;
             if (er)
             {
@@ -450,18 +468,18 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                 }
                 else
                     os << " -";
-                    
-                
+
+
                 if (er->userId)
                     os << " " << er->userId ;
                 else
                     os << " -";
-                
+
                 if (er->packageName)
                     os << " " << er->packageName;
                 else
                     os << " -";
-                
+
                 if (er->description)
                     os << " " << er->description;
                 else
@@ -471,48 +489,49 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
         break;
     case Z_APDU_extendedServicesResponse:
         os << " " << "extendedServicesResponse";
-         { 
-             Z_ExtendedServicesResponse *er 
-                 = zapdu.u.extendedServicesResponse;
-             if (er)
-             {
-                 if (er->operationStatus)
-                 {
-                     os << " ";
-                     switch (*(er->operationStatus)){
-                     case Z_ExtendedServicesResponse_done:
-                         os << "OK";
-                         break;
-                     case Z_ExtendedServicesResponse_accepted:
-                         os << "ACCEPT";
-                         break;
-                     case Z_ExtendedServicesResponse_failure:
-                         if (er->num_diagnostics)
-                             os << "DIAG " << **(er->diagnostics);
-                         else
-                             os << "ERROR";
-                         break;
-                     default:
-                         os << "unknown";
-                     }
-                 }
-                 else
-                     os << " -";
-             }
-         }
+        {
+            Z_ExtendedServicesResponse *er
+                = zapdu.u.extendedServicesResponse;
+            if (er)
+            {
+                if (er->operationStatus)
+                {
+                    os << " ";
+                    switch (*er->operationStatus)
+                    {
+                    case Z_ExtendedServicesResponse_done:
+                        os << "OK";
+                        break;
+                    case Z_ExtendedServicesResponse_accepted:
+                        os << "ACCEPT";
+                        break;
+                    case Z_ExtendedServicesResponse_failure:
+                        if (er->num_diagnostics)
+                            os << "DIAG " << **(er->diagnostics);
+                        else
+                            os << "ERROR";
+                        break;
+                    default:
+                        os << "unknown";
+                    }
+                }
+                else
+                    os << " -";
+            }
+        }
         break;
     case Z_APDU_close:
         os  << " " << "close" << " ";
-        { 
-            Z_Close  *c 
-                = zapdu.u.close;
+        {
+            Z_Close *c = zapdu.u.close;
             if (c)
             {
                 if (c->closeReason)
                 {
                     os << *(c->closeReason) << " ";
 
-                    switch (*(c->closeReason)) {
+                    switch (*c->closeReason)
+                    {
                     case Z_Close_finished:
                         os << "finished";
                         break;
@@ -547,7 +566,7 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
                         os << "unknown";
                     }
                 }
-                
+
                 if (c->diagnosticInformation)
                     os << " " << c->diagnosticInformation;
             }
@@ -559,7 +578,7 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
     case Z_APDU_duplicateDetectionResponse:
         os << " " << "duplicateDetectionResponse";
         break;
-    default: 
+    default:
         os << " " << "Z_APDU " << "UNKNOWN";
     }
 
@@ -572,8 +591,9 @@ std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+