Improvements in the log system, especially on the server side. Now logging on
[yaz-moved-to-github.git] / src / logrpn.c
index b79b2e3..d2e2b34 100644 (file)
@@ -1,10 +1,17 @@
 /*
- * Copyright (C) 1995-2003, Index Data
+ * Copyright (C) 1995-2004, Index Data
  * All rights reserved.
  *
- * $Id: logrpn.c,v 1.1 2003-10-27 12:21:30 adam Exp $
+ * $Id: logrpn.c,v 1.4 2004-11-16 17:08:11 heikki Exp $
  */
+
+/**
+ * \file logrpn.c
+ * \brief Implements Z39.50 Query Printing
+ */
+
 #include <stdio.h>
+#include <assert.h>
 
 #include <yaz/log.h>
 #include <yaz/logrpn.h>
@@ -177,11 +184,52 @@ static void attrStr (int type, int value, enum oid_value ast, char *str)
         sprintf (str, "%d=%d", type, value);
 }
 
+static void wrbuf_attr(WRBUF b, Z_AttributeElement *element)
+{
+    int i;
+    char *setname="";
+    char *sep=""; /* optional space after attrset name */
+    if (element->attributeSet)
+    {
+        oident *attrset;
+        attrset = oid_getentbyoid (element->attributeSet);
+        setname=attrset->desc;
+        sep=" ";
+    }
+    switch (element->which) 
+    {
+        case Z_AttributeValue_numeric:
+            wrbuf_printf(b,"@attr %s%s%d=%d ", setname,sep,
+                    *element->attributeType, *element->value.numeric);
+            break;
+        case Z_AttributeValue_complex:
+            wrbuf_printf(b,"@attr %s%s%d=", setname,sep,*element->attributeType);
+            for (i = 0; i<element->value.complex->num_list; i++)
+            {
+                if (i)
+                    wrbuf_printf(b,",");
+                if (element->value.complex->list[i]->which ==
+                    Z_StringOrNumeric_string)
+                    wrbuf_printf (b, "'%s'",
+                            element->value.complex->list[i]->u.string);
+                else if (element->value.complex->list[i]->which ==
+                         Z_StringOrNumeric_numeric)
+                    wrbuf_printf (b, "%d", 
+                            *element->value.complex->list[i]->u.numeric);
+            }
+            wrbuf_printf(b," ");
+            break;
+        default:
+            wrbuf_printf (b, "(unknown attr type) ");
+                    
+    }
+}
+
 /*
  * zlog_attributes: print attributes of term
  */
-static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
-                             enum oid_value ast)
+static void zlog_attributes (Z_AttributesPlusTerm *t, int depth,
+                             enum oid_value ast, int loglevel)
 {
     int of, i;
     char str[80];
@@ -203,84 +251,98 @@ static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
         case Z_AttributeValue_numeric:
            attrStr (*element->attributeType,
                     *element->value.numeric, ast, str);
-            yaz_log (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
+            yaz_log (loglevel, "%*.0s%s %s", depth, "", attset_name, str);
             break;
         case Z_AttributeValue_complex:
-            yaz_log (LOG_LOG, "%*.0s%s attributeType=%d complex",
-                 level, "", attset_name, *element->attributeType);
+            yaz_log (loglevel, "%*.0s%s attributeType=%d complex",
+                 depth, "", attset_name, *element->attributeType);
             for (i = 0; i<element->value.complex->num_list; i++)
             {
                 if (element->value.complex->list[i]->which ==
                     Z_StringOrNumeric_string)
-                    yaz_log (LOG_LOG, "%*.0s  string: '%s'", level, "",
+                    yaz_log (loglevel, "%*.0s  string: '%s'", depth, "",
                             element->value.complex->list[i]->u.string);
                 else if (element->value.complex->list[i]->which ==
                          Z_StringOrNumeric_numeric)
-                    yaz_log (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
+                    yaz_log (loglevel, "%*.0s  numeric: '%d'", depth, "",
                             *element->value.complex->list[i]->u.numeric);
             }
             break;
         default:
-            yaz_log (LOG_LOG, "%.*s%s attribute unknown",
-                    level, "", attset_name);
+            yaz_log (loglevel, "%.*s%s attribute unknown",
+                    depth, "", attset_name);
         }
     }
 }
 
-static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
+static char *complex_op_name(Z_Operator *op)
+{
+    switch (op->which)
+    {
+        case Z_Operator_and:
+            return "and";
+        case Z_Operator_or:
+            return "or";
+        case Z_Operator_and_not:
+            return "and-not";
+       case Z_Operator_prox:
+            return "prox";
+        default:
+            return "unknown complex operator";
+    }
+}
+
+static char *prox_unit_name(Z_ProximityOperator *op)
+{
+    if (op->which!=Z_ProximityOperator_known)
+         return "private";
+    switch(*op->u.known)
+    {
+        case Z_ProxUnit_character: return "character";
+        case Z_ProxUnit_word: return "word";
+        case Z_ProxUnit_sentence: return "sentence";
+        case Z_ProxUnit_paragraph: return "paragraph";
+        case Z_ProxUnit_section: return "section";
+        case Z_ProxUnit_chapter: return "chapter";
+        case Z_ProxUnit_document: return "document";
+        case Z_ProxUnit_element: return "element";
+        case Z_ProxUnit_subelement: return "subelement";
+        case Z_ProxUnit_elementType: return "elementType";
+        case Z_ProxUnit_byte: return "byte";
+        default: return "unknown";
+    }
+}
+
+static void zlog_structure (Z_RPNStructure *zs, int depth, 
+        enum oid_value ast, int loglevel)
 {
     if (zs->which == Z_RPNStructure_complex)
     {
         Z_Operator *op = zs->u.complex->roperator;
-        const char *rstr = 0;
-        const char *unit = "private";
         switch (op->which)
         {
         case Z_Operator_and:
-            yaz_log (LOG_LOG, "%*.0s and", level, "");
-            break;
         case Z_Operator_or:
-            yaz_log (LOG_LOG, "%*.0s or", level, "");
-            break;
         case Z_Operator_and_not:
-            yaz_log (LOG_LOG, "%*.0s and-not", level, "");
+            yaz_log (loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
             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 "
+            yaz_log (loglevel, "%*.0s prox excl=%s dist=%d order=%s "
                      "rel=%s unit=%s",
-                     level, "", op->u.prox->exclusion ?
+                     depth, "", 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);
+                     relToStr(*op->u.prox->relationType),
+                     prox_unit_name(op->u.prox) );
            break;
         default:
-            yaz_log (LOG_LOG, "%*.0s unknown complex", level, "");
+            yaz_log (loglevel, "%*.0s unknown complex", depth, "");
             return;
         }
-        zlog_structure (zs->u.complex->s1, level+2, ast);
-        zlog_structure (zs->u.complex->s2, level+2, ast);
-    }
+        zlog_structure (zs->u.complex->s1, depth+2, ast, loglevel);
+        zlog_structure (zs->u.complex->s2, depth+2, ast, loglevel);
+    } 
     else if (zs->which == Z_RPNStructure_simple)
     {
         if (zs->u.simple->which == Z_Operand_APT)
@@ -290,39 +352,100 @@ static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
             switch (zapt->term->which)
             {
             case Z_Term_general:
-                yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
+                yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
                         zapt->term->u.general->len,
                         zapt->term->u.general->buf);
                 break;
             case Z_Term_characterString:
-                yaz_log (LOG_LOG, "%*.0s term '%s' (string)", level, "",
+                yaz_log (loglevel, "%*.0s term '%s' (string)", depth, "",
                         zapt->term->u.characterString);
                 break;
             case Z_Term_numeric:
-                yaz_log (LOG_LOG, "%*.0s term '%d' (numeric)", level, "",
+                yaz_log (loglevel, "%*.0s term '%d' (numeric)", depth, "",
                         *zapt->term->u.numeric);
                 break;
             case Z_Term_null:
-                yaz_log (LOG_LOG, "%*.0s term (null)", level, "");
+                yaz_log (loglevel, "%*.0s term (null)", depth, "");
                 break;
             default:
-                yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
+                yaz_log (loglevel, "%*.0s term (not general)", depth, "");
             }
-            zlog_attributes (zapt, level+2, ast);
+            zlog_attributes (zapt, depth+2, ast, loglevel);
         }
         else if (zs->u.simple->which == Z_Operand_resultSetId)
         {
-            yaz_log (LOG_LOG, "%*.0s set '%s'", level, "",
+            yaz_log (loglevel, "%*.0s set '%s'", depth, "",
                     zs->u.simple->u.resultSetId);
         }
         else
-            yaz_log (LOG_LOG, "%*.0s unknown simple structure", level, "");
+            yaz_log (loglevel, "%*.0s unknown simple structure", depth, "");
     }
     else
-        yaz_log (LOG_LOG, "%*.0s unknown structure", level, "");
+        yaz_log (loglevel, "%*.0s unknown structure", depth, "");
 }
 
-void log_rpn_query (Z_RPNQuery *rpn)
+static void wrbuf_structure (WRBUF b, Z_RPNStructure *zs, enum oid_value ast)
+{
+    int i;
+    if (zs->which == Z_RPNStructure_complex)
+    {
+        Z_Operator *op = zs->u.complex->roperator;
+        wrbuf_printf(b, "@%s ", complex_op_name(op) );
+        if (op->which== Z_Operator_prox)
+        {
+            wrbuf_printf(b, "(excl=%s dist=%d order=%s "
+                     "rel=%s unit=%s) ",
+                     op->u.prox->exclusion ?
+                     (*op->u.prox->exclusion ? "T" : "F") : "N", 
+                     *op->u.prox->distance,
+                     *op->u.prox->ordered ? "T" : "F",
+                     relToStr(*op->u.prox->relationType),
+                     prox_unit_name(op->u.prox) );
+        }
+        wrbuf_structure (b,zs->u.complex->s1, ast);
+        wrbuf_structure (b,zs->u.complex->s2, ast);
+    }
+    else if (zs->which == Z_RPNStructure_simple)
+    {
+        if (zs->u.simple->which == Z_Operand_APT)
+        {
+            Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
+            int num_attributes = zapt->attributes->num_attributes;
+            for (i=0;i<num_attributes;i++)
+                 wrbuf_attr(b,zapt->attributes->attributes[i]);
+
+            switch (zapt->term->which)
+            {
+            case Z_Term_general:
+                wrbuf_printf(b, "'%.*s' ",
+                        zapt->term->u.general->len,
+                        zapt->term->u.general->buf);
+                break;
+            case Z_Term_characterString:
+                wrbuf_printf(b, "\"%s\" ", zapt->term->u.characterString);
+                break;
+            case Z_Term_numeric:
+                wrbuf_printf(b, "%d ", *zapt->term->u.numeric);
+                break;
+            case Z_Term_null:
+                wrbuf_printf(b, "(null) ");
+                break;
+            default:
+                wrbuf_printf(b, "(unknown term type %d) ", zapt->term->which);
+            }
+        }
+        else if (zs->u.simple->which == Z_Operand_resultSetId)
+        {
+            wrbuf_printf(b, "@set '%s' ", zs->u.simple->u.resultSetId);
+        }
+        else
+            wrbuf_printf (b, "(unknown simple structure)");
+    }
+    else
+        wrbuf_puts(b, "(unknown structure)");
+}
+
+void log_rpn_query_level (int loglevel, Z_RPNQuery *rpn)
 {
     oident *attrset;
     enum oid_value ast;
@@ -331,38 +454,147 @@ void log_rpn_query (Z_RPNQuery *rpn)
     if (attrset)
     {
         ast = attrset->value;
-       yaz_log (LOG_LOG, "RPN query. Type: %s", attrset->desc);
+       yaz_log (loglevel, "RPN query. Type: %s", attrset->desc);
     } 
     else
     {
        ast = VAL_NONE;
-       yaz_log (LOG_LOG, "RPN query. Unknown type");
+       yaz_log (loglevel, "RPN query. Unknown type");
     }
-    zlog_structure (rpn->RPNStructure, 0, ast);
+    zlog_structure (rpn->RPNStructure, 0, ast, loglevel);
 }
 
-void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
+static void wrbuf_rpn_query(WRBUF b, Z_RPNQuery *rpn)
 {
-    int level = 0;
+    oident *attrset;
+    enum oid_value ast;
+    
+    attrset = oid_getentbyoid (rpn->attributeSetId);
+    if (attrset)
+    {
+        ast = attrset->value;
+       wrbuf_printf(b, " @attrset %s ", attrset->desc);
+    } 
+    else
+    {
+       ast = VAL_NONE;
+       wrbuf_printf (b, "Unknown:");
+    }
+    wrbuf_structure (b,rpn->RPNStructure, ast);
+
+}
+
+void log_rpn_query (Z_RPNQuery *rpn)
+{
+    log_rpn_query_level(LOG_LOG, rpn);
+}
+
+void log_scan_term_level (int loglevel, 
+         Z_AttributesPlusTerm *zapt, oid_value ast)
+{
+    if (!loglevel)
+        return;
+    int depth = 0;
     if (zapt->term->which == Z_Term_general) 
     {
-       yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
+       yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
                 zapt->term->u.general->len, zapt->term->u.general->buf);
     }
     else
-       yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
-    zlog_attributes (zapt, level+2, ast);
+       yaz_log (loglevel, "%*.0s term (not general)", depth, "");
+    zlog_attributes (zapt, depth+2, ast, loglevel);
 }
 
-void yaz_log_zquery (Z_Query *q)
+void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
 {
+    log_scan_term_level (LOG_LOG, zapt, ast);
+}
+
+void wrbuf_scan_term(WRBUF b, Z_AttributesPlusTerm *zapt, oid_value ast)
+{
+    int num_attributes = zapt->attributes->num_attributes;
+    int i;
+    for (i=0;i<num_attributes;i++)
+        wrbuf_attr(b,zapt->attributes->attributes[i]);
+    if (zapt->term->which == Z_Term_general)
+    {
+        wrbuf_printf (b, "'%.*s' ", 
+           zapt->term->u.general->len, 
+           zapt->term->u.general->buf);
+    }
+    else
+        wrbuf_printf (b, "(not a general term)");
+}
+
+void yaz_log_zquery_level (int loglevel, Z_Query *q)
+{
+    if (!loglevel)
+        return; 
     switch (q->which)
     {
     case Z_Query_type_1: case Z_Query_type_101:
-       log_rpn_query (q->u.type_1);
+       log_rpn_query_level (loglevel, q->u.type_1);
         break;
+    case Z_Query_type_2:
+       yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
+       break;
+    case Z_Query_type_100:
+       yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
+               q->u.type_100->buf);
+       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);
+            yaz_log (loglevel, "CQL: %s", q->u.type_104->u.cql);
+    }
+}
+
+void yaz_log_zquery (Z_Query *q)
+{
+    yaz_log_zquery_level(LOG_LOG,q);
+}
+
+void wrbuf_put_zquery(WRBUF b, Z_Query *q)
+{
+    assert(q);
+    assert(b);
+    switch (q->which)
+    {
+        case Z_Query_type_1: 
+        case Z_Query_type_101:
+            wrbuf_printf(b,"Z:");
+            wrbuf_rpn_query(b,q->u.type_1);
+            break;
+        case Z_Query_type_2:
+           wrbuf_printf(b, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
+           break;
+        case Z_Query_type_100:
+           wrbuf_printf(b, "Z39.58: %.*s", q->u.type_100->len,
+                   q->u.type_100->buf);
+           break;
+        case Z_Query_type_104:
+            if (q->u.type_104->which == Z_External_CQL)
+                wrbuf_printf(b, "CQL: %s", q->u.type_104->u.cql);
+            else
+                wrbuf_printf(b,"Unknown type 104 query %d", q->u.type_104->which);
+    }
+}
+
+void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
+{
+    /* we only dump the first diag - that keeps the log cleaner. */
+    wrbuf_printf(b," ERROR ");
+    if (diags[0]->which != Z_DiagRec_defaultFormat)
+        wrbuf_printf(b,"(diag not in default format?)");
+    else
+    {
+        Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
+        if (e->condition)
+            wrbuf_printf(b, "%d ",*e->condition);
+        else
+            wrbuf_printf(b, "?? ");
+        if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
+            wrbuf_printf(b,"%s ",e->u.v2Addinfo);
+        else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
+            wrbuf_printf(b,"%s ",e->u.v3Addinfo);
     }
 }