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