16af8ba26c5ed5d1fad5d82426190739ae99d4a6
[yaz-moved-to-github.git] / util / logrpn.c
1 /*
2  * Copyright (C) 1995-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: logrpn.c,v $
7  * Revision 1.1  1998-11-16 16:02:35  adam
8  * Added loggin utilies, log_rpn_query and log_scan_term. These used
9  * to be part of Zebra.
10  *
11  *
12  *
13  * Old logs from zlogs.c:
14  *
15  * Revision 1.9  1998/10/13 20:36:02  adam
16  * Changed "indent" format string in log messages.
17  *
18  * Revision 1.8  1998/04/02 14:35:29  adam
19  * First version of Zebra that works with compiled ASN.1.
20  *
21  * Revision 1.7  1998/01/29 13:40:11  adam
22  * Better logging for scan service.
23  *
24  * Revision 1.6  1997/09/29 09:06:41  adam
25  * Removed static var to make this module thread safe.
26  *
27  * Revision 1.5  1997/04/30 08:56:07  quinn
28  * null
29  *
30  * Revision 1.4  1996/10/08  09:41:25  quinn
31  * Fixed diagnostic.
32  *
33  * Revision 1.3  1996/03/20  09:36:40  adam
34  * Function dict_lookup_grep got extra parameter, init_pos, which marks
35  * from which position in pattern approximate pattern matching should occur.
36  * Approximate pattern matching is used in relevance=re-2.
37  *
38  * Revision 1.2  1996/01/03  16:22:11  quinn
39  * operator->roperator
40  *
41  * Revision 1.1  1995/11/16  17:00:55  adam
42  * Better logging of rpn query.
43  *
44  */
45 #include <stdio.h>
46 #include <assert.h>
47
48 #include <log.h>
49 #include <logrpn.h>
50
51 static void attrStr (int type, int value, enum oid_value ast, char *str)
52 {
53     *str = '\0';
54     switch (ast)
55     {
56     case VAL_BIB1:
57     case VAL_EXP1:
58     case VAL_GILS:
59         switch (type)
60         {
61         case 1:
62             sprintf (str, "use");
63             break;
64         case 2:
65             switch (value)
66             {
67             case 1:
68                 sprintf (str, "relation=Less than");
69                 break;
70             case 2:
71                 sprintf (str, "relation=Less than or equal");
72                 break;
73             case 3:
74                 sprintf (str, "relation=Equal");
75                 break;
76             case 4:
77                 sprintf (str, "relation=Greater or equal");
78                 break;
79             case 5:
80                 sprintf (str, "relation=Greater than");
81                 break;
82             case 6:
83                 sprintf (str, "relation=Not equal");
84                 break;
85             case 100:
86                 sprintf (str, "relation=Phonetic");
87                 break;
88             case 101:
89                 sprintf (str, "relation=Stem");
90                 break;
91             case 102:
92                 sprintf (str, "relation=Relevance");
93                 break;
94             case 103:
95                 sprintf (str, "relation=AlwaysMatches");
96                 break;
97             default:
98                 sprintf (str, "relation");
99             }
100             break;
101         case 3:
102             switch (value)
103             {
104             case 1:
105                 sprintf (str, "position=First in field");
106                 break;
107             case 2:
108                 sprintf (str, "position=First in any subfield");
109                 break;
110             case 3:
111                 sprintf (str, "position=Any position in field");
112                 break;
113             default:
114                 sprintf (str, "position");
115             }
116             break;
117         case 4:
118             switch (value)
119             {
120             case 1:
121                 sprintf (str, "structure=Phrase");
122                 break;
123             case 2:
124                 sprintf (str, "structure=Word");
125                 break;
126             case 3:
127                 sprintf (str, "structure=Key");
128                 break;
129             case 4:
130                 sprintf (str, "structure=Year");
131                 break;
132             case 5:
133                 sprintf (str, "structure=Date");
134                 break;
135             case 6:
136                 sprintf (str, "structure=Word list");
137                 break;
138             case 100:
139                 sprintf (str, "structure=Date (un)");
140                 break;
141             case 101:
142                 sprintf (str, "structure=Name (norm)");
143                 break;
144             case 102:
145                 sprintf (str, "structure=Name (un)");
146                 break;
147             case 103:
148                 sprintf (str, "structure=Structure");
149                 break;
150             case 104:
151                 sprintf (str, "structure=urx");
152                 break;
153             case 105:
154                 sprintf (str, "structure=free-form-text");
155                 break;
156             case 106:
157                 sprintf (str, "structure=document-text");
158                 break;
159             case 107:
160                 sprintf (str, "structure=local-number");
161                 break;
162             case 108:
163                 sprintf (str, "structure=string");
164                 break;
165             case 109:
166                 sprintf (str, "structure=numeric string");
167                 break;
168             default:
169                 sprintf (str, "structure");
170             }
171             break;
172         case 5:
173             switch (value)
174             {
175             case 1:
176                 sprintf (str, "truncation=Right");
177                 break;
178             case 2:
179                 sprintf (str, "truncation=Left");
180                 break;
181             case 3:
182                 sprintf (str, "truncation=Left&right");
183                 break;
184             case 100:
185                 sprintf (str, "truncation=Do not truncate");
186                 break;
187             case 101:
188                 sprintf (str, "truncation=Process #");
189                 break;
190             case 102:
191                 sprintf (str, "truncation=re-1");
192                 break;
193             case 103:
194                 sprintf (str, "truncation=re-2");
195                 break;
196             default:
197                 sprintf (str, "truncation");
198             }
199             break;
200         case 6:
201             switch (value)
202             {
203             case 1:
204                 sprintf (str, "completeness=Incomplete subfield");
205                 break;
206             case 2:
207                 sprintf (str, "completeness=Complete subfield");
208                 break;
209             case 3:
210                 sprintf (str, "completeness=Complete field");
211                 break;
212             default:
213                 sprintf (str, "completeness");
214             }
215             break;
216         }
217         break;
218     default:
219         break;
220     }
221     if (*str)
222         sprintf (str + strlen(str), " (%d=%d)", type, value);
223     else
224         sprintf (str, "%d=%d", type, value);
225 }
226
227 /*
228  * zlog_attributes: print attributes of term
229  */
230 static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
231                              enum oid_value ast)
232 {
233     int of, i;
234     char str[80];
235 #ifdef ASN_COMPILED
236     int num_attributes = t->attributes->num_attributes;
237 #else
238     int num_attributes = t->num_attributes;
239 #endif
240     
241     for (of = 0; of < num_attributes; of++)
242     {
243         const char *attset_name = "";
244         Z_AttributeElement *element;
245 #ifdef ASN_COMPILED
246         element = t->attributes->attributes[of];
247 #else
248         element = t->attributeList[of];
249 #endif
250         if (element->attributeSet)
251         {
252             oident *attrset;
253             attrset = oid_getentbyoid (element->attributeSet);
254             attset_name = attrset->desc;
255         }
256         switch (element->which) 
257         {
258         case Z_AttributeValue_numeric:
259             attrStr (*element->attributeType,
260                      *element->value.numeric, ast, str);
261             logf (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
262             break;
263         case Z_AttributeValue_complex:
264             logf (LOG_LOG, "%*.0s%s attributeType=%d complex",
265                   level, "", attset_name, *element->attributeType);
266             for (i = 0; i<element->value.complex->num_list; i++)
267             {
268                 if (element->value.complex->list[i]->which ==
269                     Z_StringOrNumeric_string)
270                     logf (LOG_LOG, "%*.0s  string: '%s'", level, "",
271                           element->value.complex->list[i]->u.string);
272                 else if (element->value.complex->list[i]->which ==
273                          Z_StringOrNumeric_numeric)
274                     logf (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
275                           *element->value.complex->list[i]->u.numeric);
276             }
277             break;
278         default:
279             logf (LOG_LOG, "%.*s%s attribute unknown",
280                   level, "", attset_name);
281         }
282     }
283 }
284
285 static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
286 {
287     if (zs->which == Z_RPNStructure_complex)
288     {
289         switch (zs->u.complex->roperator->which)
290         {
291         case Z_Operator_and:
292             logf (LOG_LOG, "%*.0s and", level, "");
293             break;
294         case Z_Operator_or:
295             logf (LOG_LOG, "%*.0s or", level, "");
296             break;
297         case Z_Operator_and_not:
298             logf (LOG_LOG, "%*.0s and-not", level, "");
299             break;
300         default:
301             logf (LOG_LOG, "%*.0s unknown complex", level, "");
302             return;
303         }
304         zlog_structure (zs->u.complex->s1, level+2, ast);
305         zlog_structure (zs->u.complex->s2, level+2, ast);
306     }
307     else if (zs->which == Z_RPNStructure_simple)
308     {
309         if (zs->u.simple->which == Z_Operand_APT)
310         {
311             Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
312
313             if (zapt->term->which == Z_Term_general) 
314             {
315                 logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
316                       zapt->term->u.general->len, zapt->term->u.general->buf);
317             }
318             else
319             {
320                 logf (LOG_LOG, "%*.0s term (not general)", level, "");
321             }
322             zlog_attributes (zapt, level+2, ast);
323         }
324         else if (zs->u.simple->which == Z_Operand_resultSetId)
325         {
326             logf (LOG_LOG, "%*.0s set '%s'", level, "",
327                   zs->u.simple->u.resultSetId);
328         }
329         else
330             logf (LOG_LOG, "%*.0s unknown simple structure", level, "");
331     }
332     else
333         logf (LOG_LOG, "%*.0s unknown structure", level, "");
334 }
335
336 void log_rpn_query (Z_RPNQuery *rpn)
337 {
338     oident *attrset;
339     enum oid_value ast;
340     
341     attrset = oid_getentbyoid (rpn->attributeSetId);
342     if (attrset)
343     {
344         ast = attrset->value;
345         logf (LOG_LOG, "RPN query. Type: %s", attrset->desc);
346     } 
347     else
348     {
349         ast = VAL_NONE;
350         logf (LOG_LOG, "RPN query. Unknown type");
351     }
352     zlog_structure (rpn->RPNStructure, 0, ast);
353 }
354
355 void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
356 {
357     int level = 0;
358     if (zapt->term->which == Z_Term_general) 
359     {
360         logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
361               zapt->term->u.general->len, zapt->term->u.general->buf);
362     }
363     else
364         logf (LOG_LOG, "%*.0s term (not general)", level, "");
365     zlog_attributes (zapt, level+2, ast);
366 }