SOAP, SRW codecs and HTTP transport for YAZ using libxml2.
[yaz-moved-to-github.git] / zutil / logrpn.c
index a263cdf..9d46fad 100644 (file)
@@ -1,58 +1,35 @@
 /*
- * Copyright (C) 1995-1998, Index Data
+ * Copyright (C) 1995-2001, Index Data
  * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: logrpn.c,v $
- * Revision 1.1  1999-06-08 10:10:16  adam
- * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
- *
- * Revision 1.1  1998/11/16 16:02:35  adam
- * Added loggin utilies, log_rpn_query and log_scan_term. These used
- * to be part of Zebra.
- *
- *
- *
- * Old logs from zlogs.c:
- *
- * Revision 1.9  1998/10/13 20:36:02  adam
- * Changed "indent" format string in log messages.
- *
- * Revision 1.8  1998/04/02 14:35:29  adam
- * First version of Zebra that works with compiled ASN.1.
- *
- * Revision 1.7  1998/01/29 13:40:11  adam
- * Better logging for scan service.
- *
- * Revision 1.6  1997/09/29 09:06:41  adam
- * Removed static var to make this module thread safe.
- *
- * Revision 1.5  1997/04/30 08:56:07  quinn
- * null
- *
- * Revision 1.4  1996/10/08  09:41:25  quinn
- * Fixed diagnostic.
- *
- * Revision 1.3  1996/03/20  09:36:40  adam
- * Function dict_lookup_grep got extra parameter, init_pos, which marks
- * from which position in pattern approximate pattern matching should occur.
- * Approximate pattern matching is used in relevance=re-2.
- *
- * Revision 1.2  1996/01/03  16:22:11  quinn
- * operator->roperator
- *
- * Revision 1.1  1995/11/16  17:00:55  adam
- * Better logging of rpn query.
  *
+ * $Id: logrpn.c,v 1.10 2003-02-12 15:06:44 adam Exp $
  */
 #include <stdio.h>
-#include <assert.h>
 
-#include <log.h>
-#include <logrpn.h>
+#include <yaz/log.h>
+#include <yaz/logrpn.h>
 
+static const char *relToStr(int v)
+{
+    const char *str = 0;
+    switch (v)
+    {
+    case 1: str = "Less than"; break;
+    case 2: str = "Less than or equal"; break;
+    case 3: str = "Equal"; break;
+    case 4: str = "Greater or equal"; break;
+    case 5: str = "Greater than"; break;
+    case 6: str = "Not equal"; break;
+    case 100: str = "Phonetic"; break;
+    case 101: str = "Stem"; break;
+    case 102: str = "Relevance"; break;
+    case 103: str = "AlwaysMatches"; break;
+    }
+    return str;
+}
 static void attrStr (int type, int value, enum oid_value ast, char *str)
 {
+    const char *rstr;
     *str = '\0';
     switch (ast)
     {
@@ -65,41 +42,11 @@ static void attrStr (int type, int value, enum oid_value ast, char *str)
             sprintf (str, "use");
             break;
         case 2:
-            switch (value)
-            {
-            case 1:
-                sprintf (str, "relation=Less than");
-                break;
-            case 2:
-                sprintf (str, "relation=Less than or equal");
-                break;
-            case 3:
-                sprintf (str, "relation=Equal");
-                break;
-            case 4:
-                sprintf (str, "relation=Greater or equal");
-                break;
-            case 5:
-                sprintf (str, "relation=Greater than");
-                break;
-            case 6:
-                sprintf (str, "relation=Not equal");
-                break;
-            case 100:
-                sprintf (str, "relation=Phonetic");
-                break;
-            case 101:
-                sprintf (str, "relation=Stem");
-                break;
-            case 102:
-                sprintf (str, "relation=Relevance");
-                break;
-            case 103:
-                sprintf (str, "relation=AlwaysMatches");
-                break;
-            default:
-                sprintf (str, "relation");
-            }
+            rstr = relToStr(value);
+            if (rstr)
+                sprintf (str, "relation=%s", rstr);
+            else
+                sprintf (str, "relation=%d", value);
             break;
         case 3:
             switch (value)
@@ -235,21 +182,13 @@ static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
 {
     int of, i;
     char str[80];
-#ifdef ASN_COMPILED
     int num_attributes = t->attributes->num_attributes;
-#else
-    int num_attributes = t->num_attributes;
-#endif
     
     for (of = 0; of < num_attributes; of++)
     {
        const char *attset_name = "";
         Z_AttributeElement *element;
-#ifdef ASN_COMPILED
        element = t->attributes->attributes[of];
-#else
-        element = t->attributeList[of];
-#endif
        if (element->attributeSet)
        {
            oident *attrset;
@@ -261,26 +200,26 @@ static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
         case Z_AttributeValue_numeric:
            attrStr (*element->attributeType,
                     *element->value.numeric, ast, str);
-            logf (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
+            yaz_log (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
             break;
         case Z_AttributeValue_complex:
-            logf (LOG_LOG, "%*.0s%s attributeType=%d complex",
+            yaz_log (LOG_LOG, "%*.0s%s attributeType=%d complex",
                  level, "", attset_name, *element->attributeType);
             for (i = 0; i<element->value.complex->num_list; i++)
             {
                 if (element->value.complex->list[i]->which ==
                     Z_StringOrNumeric_string)
-                    logf (LOG_LOG, "%*.0s  string: '%s'", level, "",
-                          element->value.complex->list[i]->u.string);
+                    yaz_log (LOG_LOG, "%*.0s  string: '%s'", level, "",
+                            element->value.complex->list[i]->u.string);
                 else if (element->value.complex->list[i]->which ==
                          Z_StringOrNumeric_numeric)
-                    logf (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
-                          *element->value.complex->list[i]->u.numeric);
+                    yaz_log (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
+                            *element->value.complex->list[i]->u.numeric);
             }
             break;
         default:
-            logf (LOG_LOG, "%.*s%s attribute unknown",
-                 level, "", attset_name);
+            yaz_log (LOG_LOG, "%.*s%s attribute unknown",
+                    level, "", attset_name);
         }
     }
 }
@@ -289,19 +228,51 @@ static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
 {
     if (zs->which == Z_RPNStructure_complex)
     {
-        switch (zs->u.complex->roperator->which)
+        Z_Operator *op = zs->u.complex->roperator;
+        const char *rstr = 0;
+        const char *unit = "private";
+        switch (op->which)
         {
         case Z_Operator_and:
-            logf (LOG_LOG, "%*.0s and", level, "");
+            yaz_log (LOG_LOG, "%*.0s and", level, "");
             break;
         case Z_Operator_or:
-            logf (LOG_LOG, "%*.0s or", level, "");
+            yaz_log (LOG_LOG, "%*.0s or", level, "");
             break;
         case Z_Operator_and_not:
-            logf (LOG_LOG, "%*.0s and-not", level, "");
+            yaz_log (LOG_LOG, "%*.0s and-not", level, "");
             break;
+       case Z_Operator_prox:
+            if (op->u.prox->which == Z_ProximityOperator_known)
+            {
+                switch(*op->u.prox->u.known)
+                {
+                case Z_ProxUnit_character: unit = "character"; break;
+                case Z_ProxUnit_word: unit = "word"; break;
+                case Z_ProxUnit_sentence: unit = "sentence"; break;
+                case Z_ProxUnit_paragraph: unit = "paragraph"; break;
+                case Z_ProxUnit_section: unit = "section"; break;
+                case Z_ProxUnit_chapter: unit = "chapter"; break;
+                case Z_ProxUnit_document: unit = "document"; break;
+                case Z_ProxUnit_element: unit = "element"; break;
+                case Z_ProxUnit_subelement: unit = "subelement"; break;
+                case Z_ProxUnit_elementType: unit = "elementType"; break;
+                case Z_ProxUnit_byte: unit = "byte"; break;
+                default: unit = "unknown"; break;
+                }
+            }
+            rstr = relToStr(*op->u.prox->relationType);
+            yaz_log (LOG_LOG, "%*.0s prox excl=%s dist=%d order=%s "
+                     "rel=%s unit=%s",
+                     level, "", op->u.prox->exclusion ?
+                     (*op->u.prox->exclusion ? "T" : "F") : "N", 
+                     *op->u.prox->distance,
+                     *op->u.prox->ordered ? "T" : "F",
+                     rstr ? rstr : "unknown",
+                     unit);
+           break;
         default:
-            logf (LOG_LOG, "%*.0s unknown complex", level, "");
+            yaz_log (LOG_LOG, "%*.0s unknown complex", level, "");
             return;
         }
         zlog_structure (zs->u.complex->s1, level+2, ast);
@@ -313,27 +284,39 @@ static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
         {
             Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
 
-            if (zapt->term->which == Z_Term_general) 
+            switch (zapt->term->which)
             {
-                logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
-                      zapt->term->u.general->len, zapt->term->u.general->buf);
-            }
-            else
-            {
-                logf (LOG_LOG, "%*.0s term (not general)", level, "");
+            case Z_Term_general:
+                yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
+                        zapt->term->u.general->len,
+                        zapt->term->u.general->buf);
+                break;
+            case Z_Term_characterString:
+                yaz_log (LOG_LOG, "%*.0s term '%s' (string)", level, "",
+                        zapt->term->u.characterString);
+                break;
+            case Z_Term_numeric:
+                yaz_log (LOG_LOG, "%*.0s term '%d' (numeric)", level, "",
+                        *zapt->term->u.numeric);
+                break;
+            case Z_Term_null:
+                yaz_log (LOG_LOG, "%*.0s term (null)", level, "");
+                break;
+            default:
+                yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
             }
             zlog_attributes (zapt, level+2, ast);
         }
         else if (zs->u.simple->which == Z_Operand_resultSetId)
         {
-            logf (LOG_LOG, "%*.0s set '%s'", level, "",
-                  zs->u.simple->u.resultSetId);
+            yaz_log (LOG_LOG, "%*.0s set '%s'", level, "",
+                    zs->u.simple->u.resultSetId);
         }
         else
-            logf (LOG_LOG, "%*.0s unknown simple structure", level, "");
+            yaz_log (LOG_LOG, "%*.0s unknown simple structure", level, "");
     }
     else
-        logf (LOG_LOG, "%*.0s unknown structure", level, "");
+        yaz_log (LOG_LOG, "%*.0s unknown structure", level, "");
 }
 
 void log_rpn_query (Z_RPNQuery *rpn)
@@ -345,12 +328,12 @@ void log_rpn_query (Z_RPNQuery *rpn)
     if (attrset)
     {
         ast = attrset->value;
-       logf (LOG_LOG, "RPN query. Type: %s", attrset->desc);
+       yaz_log (LOG_LOG, "RPN query. Type: %s", attrset->desc);
     } 
     else
     {
        ast = VAL_NONE;
-       logf (LOG_LOG, "RPN query. Unknown type");
+       yaz_log (LOG_LOG, "RPN query. Unknown type");
     }
     zlog_structure (rpn->RPNStructure, 0, ast);
 }
@@ -360,10 +343,23 @@ void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
     int level = 0;
     if (zapt->term->which == Z_Term_general) 
     {
-       logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
-             zapt->term->u.general->len, zapt->term->u.general->buf);
+       yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
+                zapt->term->u.general->len, zapt->term->u.general->buf);
     }
     else
-       logf (LOG_LOG, "%*.0s term (not general)", level, "");
+       yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
     zlog_attributes (zapt, level+2, ast);
 }
+
+void yaz_log_zquery (Z_Query *q)
+{
+    switch (q->which)
+    {
+    case Z_Query_type_1: case Z_Query_type_101:
+       log_rpn_query (q->u.type_1);
+        break;
+    case Z_Query_type_104:
+        if (q->u.type_104->which == Z_External_CQL)
+            yaz_log (LOG_LOG, "CQL: %s", q->u.type_104->u.cql);
+    }
+}