2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: logrpn.c,v 1.14 2006-01-20 10:34:51 adam Exp $
10 * \brief Implements Z39.50 Query Printing
17 #include <yaz/logrpn.h>
19 static const char *relToStr(int v)
24 case 1: str = "Less than"; break;
25 case 2: str = "Less than or equal"; break;
26 case 3: str = "Equal"; break;
27 case 4: str = "Greater or equal"; break;
28 case 5: str = "Greater than"; break;
29 case 6: str = "Not equal"; break;
30 case 100: str = "Phonetic"; break;
31 case 101: str = "Stem"; break;
32 case 102: str = "Relevance"; break;
33 case 103: str = "AlwaysMatches"; break;
38 static void attrStr (int type, int value, enum oid_value ast, char *str)
53 rstr = relToStr(value);
55 sprintf(str, "relation=%s", rstr);
57 sprintf(str, "relation=%d", value);
63 sprintf(str, "position=First in field");
66 sprintf(str, "position=First in any subfield");
69 sprintf(str, "position=Any position in field");
72 sprintf(str, "position");
79 sprintf(str, "structure=Phrase");
82 sprintf(str, "structure=Word");
85 sprintf(str, "structure=Key");
88 sprintf(str, "structure=Year");
91 sprintf(str, "structure=Date");
94 sprintf(str, "structure=Word list");
97 sprintf(str, "structure=Date (un)");
100 sprintf(str, "structure=Name (norm)");
103 sprintf(str, "structure=Name (un)");
106 sprintf(str, "structure=Structure");
109 sprintf(str, "structure=urx");
112 sprintf(str, "structure=free-form-text");
115 sprintf(str, "structure=document-text");
118 sprintf(str, "structure=local-number");
121 sprintf(str, "structure=string");
124 sprintf(str, "structure=numeric string");
127 sprintf(str, "structure");
134 sprintf(str, "truncation=Right");
137 sprintf(str, "truncation=Left");
140 sprintf(str, "truncation=Left&right");
143 sprintf(str, "truncation=Do not truncate");
146 sprintf(str, "truncation=Process #");
149 sprintf(str, "truncation=re-1");
152 sprintf(str, "truncation=re-2");
155 sprintf(str, "truncation=CCL");
158 sprintf(str, "truncation");
165 sprintf(str, "completeness=Incomplete subfield");
168 sprintf(str, "completeness=Complete subfield");
171 sprintf(str, "completeness=Complete field");
174 sprintf(str, "completeness");
183 sprintf(str + strlen(str), " (%d=%d)", type, value);
185 sprintf(str, "%d=%d", type, value);
189 * zlog_attributes: print attributes of term
191 static void zlog_attributes (Z_AttributesPlusTerm *t, int depth,
192 enum oid_value ast, int loglevel)
196 int num_attributes = t->attributes->num_attributes;
198 for (of = 0; of < num_attributes; of++)
200 const char *attset_name = "";
201 Z_AttributeElement *element;
202 element = t->attributes->attributes[of];
203 if (element->attributeSet)
206 attrset = oid_getentbyoid (element->attributeSet);
207 attset_name = attrset->desc;
209 switch (element->which)
211 case Z_AttributeValue_numeric:
212 attrStr (*element->attributeType,
213 *element->value.numeric, ast, str);
214 yaz_log (loglevel, "%*.0s%s %s", depth, "", attset_name, str);
216 case Z_AttributeValue_complex:
217 yaz_log (loglevel, "%*.0s%s attributeType=%d complex",
218 depth, "", attset_name, *element->attributeType);
219 for (i = 0; i<element->value.complex->num_list; i++)
221 if (element->value.complex->list[i]->which ==
222 Z_StringOrNumeric_string)
223 yaz_log (loglevel, "%*.0s string: '%s'", depth, "",
224 element->value.complex->list[i]->u.string);
225 else if (element->value.complex->list[i]->which ==
226 Z_StringOrNumeric_numeric)
227 yaz_log (loglevel, "%*.0s numeric: '%d'", depth, "",
228 *element->value.complex->list[i]->u.numeric);
232 yaz_log (loglevel, "%.*s%s attribute unknown",
233 depth, "", attset_name);
238 static char *complex_op_name(Z_Operator *op)
246 case Z_Operator_and_not:
248 case Z_Operator_prox:
251 return "unknown complex operator";
255 static char *prox_unit_name(Z_ProximityOperator *op)
257 if (op->which!=Z_ProximityOperator_known)
261 case Z_ProxUnit_character: return "character";
262 case Z_ProxUnit_word: return "word";
263 case Z_ProxUnit_sentence: return "sentence";
264 case Z_ProxUnit_paragraph: return "paragraph";
265 case Z_ProxUnit_section: return "section";
266 case Z_ProxUnit_chapter: return "chapter";
267 case Z_ProxUnit_document: return "document";
268 case Z_ProxUnit_element: return "element";
269 case Z_ProxUnit_subelement: return "subelement";
270 case Z_ProxUnit_elementType: return "elementType";
271 case Z_ProxUnit_byte: return "byte";
272 default: return "unknown";
276 static void zlog_structure (Z_RPNStructure *zs, int depth,
277 enum oid_value ast, int loglevel)
279 if (zs->which == Z_RPNStructure_complex)
281 Z_Operator *op = zs->u.complex->roperator;
286 case Z_Operator_and_not:
287 yaz_log (loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
289 case Z_Operator_prox:
290 yaz_log (loglevel, "%*.0s prox excl=%s dist=%d order=%s "
292 depth, "", op->u.prox->exclusion ?
293 (*op->u.prox->exclusion ? "T" : "F") : "N",
294 *op->u.prox->distance,
295 *op->u.prox->ordered ? "T" : "F",
296 relToStr(*op->u.prox->relationType),
297 prox_unit_name(op->u.prox) );
300 yaz_log (loglevel, "%*.0s unknown complex", depth, "");
303 zlog_structure (zs->u.complex->s1, depth+2, ast, loglevel);
304 zlog_structure (zs->u.complex->s2, depth+2, ast, loglevel);
306 else if (zs->which == Z_RPNStructure_simple)
308 if (zs->u.simple->which == Z_Operand_APT)
310 Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
312 switch (zapt->term->which)
315 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
316 zapt->term->u.general->len,
317 zapt->term->u.general->buf);
319 case Z_Term_characterString:
320 yaz_log (loglevel, "%*.0s term '%s' (string)", depth, "",
321 zapt->term->u.characterString);
324 yaz_log (loglevel, "%*.0s term '%d' (numeric)", depth, "",
325 *zapt->term->u.numeric);
328 yaz_log (loglevel, "%*.0s term (null)", depth, "");
331 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
333 zlog_attributes (zapt, depth+2, ast, loglevel);
335 else if (zs->u.simple->which == Z_Operand_resultSetId)
337 yaz_log (loglevel, "%*.0s set '%s'", depth, "",
338 zs->u.simple->u.resultSetId);
341 yaz_log (loglevel, "%*.0s unknown simple structure", depth, "");
344 yaz_log (loglevel, "%*.0s unknown structure", depth, "");
347 void log_rpn_query_level (int loglevel, Z_RPNQuery *rpn)
352 attrset = oid_getentbyoid (rpn->attributeSetId);
355 ast = attrset->value;
356 yaz_log (loglevel, "RPN query. Type: %s", attrset->desc);
361 yaz_log (loglevel, "RPN query. Unknown type");
363 zlog_structure (rpn->RPNStructure, 0, ast, loglevel);
366 void log_rpn_query(Z_RPNQuery *rpn)
368 log_rpn_query_level(YLOG_LOG, rpn);
371 void log_scan_term_level(int loglevel,
372 Z_AttributesPlusTerm *zapt, oid_value ast)
377 if (zapt->term->which == Z_Term_general)
379 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
380 zapt->term->u.general->len, zapt->term->u.general->buf);
383 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
384 zlog_attributes (zapt, depth+2, ast, loglevel);
387 void log_scan_term(Z_AttributesPlusTerm *zapt, oid_value ast)
389 log_scan_term_level (YLOG_LOG, zapt, ast);
392 void yaz_log_zquery_level (int loglevel, Z_Query *q)
398 case Z_Query_type_1: case Z_Query_type_101:
399 log_rpn_query_level (loglevel, q->u.type_1);
402 yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
404 case Z_Query_type_100:
405 yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
408 case Z_Query_type_104:
409 if (q->u.type_104->which == Z_External_CQL)
410 yaz_log (loglevel, "CQL: %s", q->u.type_104->u.cql);
414 void yaz_log_zquery (Z_Query *q)
416 yaz_log_zquery_level(YLOG_LOG, q);
419 void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
421 /* we only dump the first diag - that keeps the log cleaner. */
422 wrbuf_printf(b," ERROR ");
423 if (diags[0]->which != Z_DiagRec_defaultFormat)
424 wrbuf_printf(b,"(diag not in default format?)");
427 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
429 wrbuf_printf(b, "%d ",*e->condition);
431 wrbuf_printf(b, "?? ");
432 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
433 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
434 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
435 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
441 * indent-tabs-mode: nil
443 * vim: shiftwidth=4 tabstop=8 expandtab