2 * Copyright (C) 1995-2007, Index Data ApS
5 * $Id: logrpn.c,v 1.16 2007-04-12 13:52:57 adam Exp $
10 * \brief Implements Z39.50 Query Printing
17 #include <yaz/logrpn.h>
18 #include <yaz/oid_db.h>
20 static const char *relToStr(int v)
25 case 1: str = "Less than"; break;
26 case 2: str = "Less than or equal"; break;
27 case 3: str = "Equal"; break;
28 case 4: str = "Greater or equal"; break;
29 case 5: str = "Greater than"; break;
30 case 6: str = "Not equal"; break;
31 case 100: str = "Phonetic"; break;
32 case 101: str = "Stem"; break;
33 case 102: str = "Relevance"; break;
34 case 103: str = "AlwaysMatches"; break;
39 static void attrStr (int type, int value, char *str)
49 rstr = relToStr(value);
51 sprintf(str, "relation=%s", rstr);
53 sprintf(str, "relation=%d", value);
59 sprintf(str, "position=First in field");
62 sprintf(str, "position=First in any subfield");
65 sprintf(str, "position=Any position in field");
68 sprintf(str, "position");
75 sprintf(str, "structure=Phrase");
78 sprintf(str, "structure=Word");
81 sprintf(str, "structure=Key");
84 sprintf(str, "structure=Year");
87 sprintf(str, "structure=Date");
90 sprintf(str, "structure=Word list");
93 sprintf(str, "structure=Date (un)");
96 sprintf(str, "structure=Name (norm)");
99 sprintf(str, "structure=Name (un)");
102 sprintf(str, "structure=Structure");
105 sprintf(str, "structure=urx");
108 sprintf(str, "structure=free-form-text");
111 sprintf(str, "structure=document-text");
114 sprintf(str, "structure=local-number");
117 sprintf(str, "structure=string");
120 sprintf(str, "structure=numeric string");
123 sprintf(str, "structure");
130 sprintf(str, "truncation=Right");
133 sprintf(str, "truncation=Left");
136 sprintf(str, "truncation=Left&right");
139 sprintf(str, "truncation=Do not truncate");
142 sprintf(str, "truncation=Process #");
145 sprintf(str, "truncation=re-1");
148 sprintf(str, "truncation=re-2");
151 sprintf(str, "truncation=CCL");
154 sprintf(str, "truncation");
161 sprintf(str, "completeness=Incomplete subfield");
164 sprintf(str, "completeness=Complete subfield");
167 sprintf(str, "completeness=Complete field");
170 sprintf(str, "completeness");
175 sprintf(str + strlen(str), " (%d=%d)", type, value);
177 sprintf(str, "%d=%d", type, value);
181 * zlog_attributes: print attributes of term
183 static void zlog_attributes(Z_AttributesPlusTerm *t, int depth,
184 const int *ast, int loglevel)
188 int num_attributes = t->attributes->num_attributes;
190 for (of = 0; of < num_attributes; of++)
192 char attset_name_buf[OID_STR_MAX];
193 const char *attset_name = 0;
194 Z_AttributeElement *element;
195 element = t->attributes->attributes[of];
196 if (element->attributeSet)
198 attset_name = yaz_oid_to_string_buf(element->attributeSet,
203 switch (element->which)
205 case Z_AttributeValue_numeric:
206 attrStr (*element->attributeType,
207 *element->value.numeric, str);
208 yaz_log (loglevel, "%*.0s%s %s", depth, "", attset_name, str);
210 case Z_AttributeValue_complex:
211 yaz_log (loglevel, "%*.0s%s attributeType=%d complex",
212 depth, "", attset_name, *element->attributeType);
213 for (i = 0; i<element->value.complex->num_list; i++)
215 if (element->value.complex->list[i]->which ==
216 Z_StringOrNumeric_string)
217 yaz_log (loglevel, "%*.0s string: '%s'", depth, "",
218 element->value.complex->list[i]->u.string);
219 else if (element->value.complex->list[i]->which ==
220 Z_StringOrNumeric_numeric)
221 yaz_log (loglevel, "%*.0s numeric: '%d'", depth, "",
222 *element->value.complex->list[i]->u.numeric);
226 yaz_log (loglevel, "%.*s%s attribute unknown",
227 depth, "", attset_name);
232 static char *complex_op_name(Z_Operator *op)
240 case Z_Operator_and_not:
242 case Z_Operator_prox:
245 return "unknown complex operator";
249 static char *prox_unit_name(Z_ProximityOperator *op)
251 if (op->which!=Z_ProximityOperator_known)
255 case Z_ProxUnit_character: return "character";
256 case Z_ProxUnit_word: return "word";
257 case Z_ProxUnit_sentence: return "sentence";
258 case Z_ProxUnit_paragraph: return "paragraph";
259 case Z_ProxUnit_section: return "section";
260 case Z_ProxUnit_chapter: return "chapter";
261 case Z_ProxUnit_document: return "document";
262 case Z_ProxUnit_element: return "element";
263 case Z_ProxUnit_subelement: return "subelement";
264 case Z_ProxUnit_elementType: return "elementType";
265 case Z_ProxUnit_byte: return "byte";
266 default: return "unknown";
270 static void zlog_structure(Z_RPNStructure *zs, int depth,
271 const int *ast, int loglevel)
273 if (zs->which == Z_RPNStructure_complex)
275 Z_Operator *op = zs->u.complex->roperator;
280 case Z_Operator_and_not:
281 yaz_log (loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
283 case Z_Operator_prox:
284 yaz_log (loglevel, "%*.0s prox excl=%s dist=%d order=%s "
286 depth, "", op->u.prox->exclusion ?
287 (*op->u.prox->exclusion ? "T" : "F") : "N",
288 *op->u.prox->distance,
289 *op->u.prox->ordered ? "T" : "F",
290 relToStr(*op->u.prox->relationType),
291 prox_unit_name(op->u.prox) );
294 yaz_log (loglevel, "%*.0s unknown complex", depth, "");
297 zlog_structure (zs->u.complex->s1, depth+2, ast, loglevel);
298 zlog_structure (zs->u.complex->s2, depth+2, ast, loglevel);
300 else if (zs->which == Z_RPNStructure_simple)
302 if (zs->u.simple->which == Z_Operand_APT)
304 Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
306 switch (zapt->term->which)
309 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
310 zapt->term->u.general->len,
311 zapt->term->u.general->buf);
313 case Z_Term_characterString:
314 yaz_log (loglevel, "%*.0s term '%s' (string)", depth, "",
315 zapt->term->u.characterString);
318 yaz_log (loglevel, "%*.0s term '%d' (numeric)", depth, "",
319 *zapt->term->u.numeric);
322 yaz_log (loglevel, "%*.0s term (null)", depth, "");
325 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
327 zlog_attributes(zapt, depth+2, ast, loglevel);
329 else if (zs->u.simple->which == Z_Operand_resultSetId)
331 yaz_log (loglevel, "%*.0s set '%s'", depth, "",
332 zs->u.simple->u.resultSetId);
335 yaz_log (loglevel, "%*.0s unknown simple structure", depth, "");
338 yaz_log (loglevel, "%*.0s unknown structure", depth, "");
341 void log_rpn_query_level (int loglevel, Z_RPNQuery *rpn)
343 zlog_structure(rpn->RPNStructure, 0, rpn->attributeSetId, loglevel);
346 void log_rpn_query(Z_RPNQuery *rpn)
348 log_rpn_query_level(YLOG_LOG, rpn);
351 void log_scan_term_level(int loglevel,
352 Z_AttributesPlusTerm *zapt, const int *ast)
357 if (zapt->term->which == Z_Term_general)
359 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
360 zapt->term->u.general->len, zapt->term->u.general->buf);
363 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
364 zlog_attributes(zapt, depth+2, ast, loglevel);
367 void log_scan_term(Z_AttributesPlusTerm *zapt, const int *ast)
369 log_scan_term_level (YLOG_LOG, zapt, ast);
372 void yaz_log_zquery_level (int loglevel, Z_Query *q)
378 case Z_Query_type_1: case Z_Query_type_101:
379 log_rpn_query_level (loglevel, q->u.type_1);
382 yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
384 case Z_Query_type_100:
385 yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
388 case Z_Query_type_104:
389 if (q->u.type_104->which == Z_External_CQL)
390 yaz_log (loglevel, "CQL: %s", q->u.type_104->u.cql);
394 void yaz_log_zquery (Z_Query *q)
396 yaz_log_zquery_level(YLOG_LOG, q);
399 void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
401 /* we only dump the first diag - that keeps the log cleaner. */
402 wrbuf_printf(b," ERROR ");
403 if (diags[0]->which != Z_DiagRec_defaultFormat)
404 wrbuf_printf(b,"(diag not in default format?)");
407 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
409 wrbuf_printf(b, "%d ",*e->condition);
411 wrbuf_printf(b, "?? ");
412 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
413 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
414 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
415 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
421 * indent-tabs-mode: nil
423 * vim: shiftwidth=4 tabstop=8 expandtab