YAZ 5.37.0
logrpn.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
9#if HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <stdio.h>
14#include <assert.h>
15
16#include <yaz/log.h>
17#include <yaz/logrpn.h>
18#include <yaz/oid_db.h>
19#include <yaz/proxunit.h>
20#include <yaz/snprintf.h>
21
22static const char *relToStr(Odr_int v)
23{
24 const char *str = 0;
25 switch (v)
26 {
27 case 1: str = "Less than"; break;
28 case 2: str = "Less than or equal"; break;
29 case 3: str = "Equal"; break;
30 case 4: str = "Greater or equal"; break;
31 case 5: str = "Greater than"; break;
32 case 6: str = "Not equal"; break;
33 case 100: str = "Phonetic"; break;
34 case 101: str = "Stem"; break;
35 case 102: str = "Relevance"; break;
36 case 103: str = "AlwaysMatches"; break;
37 }
38 return str;
39}
40
41static void attrStr(Odr_int type, Odr_int value, char *str, size_t sz)
42{
43 const char *rstr;
44 *str = '\0';
45 switch (type)
46 {
47 case 1:
48 yaz_snprintf(str, sz, "use");
49 break;
50 case 2:
51 rstr = relToStr(value);
52 if (rstr)
53 yaz_snprintf(str, sz, "relation=%s", rstr);
54 else
55 yaz_snprintf(str, sz, "relation=" ODR_INT_PRINTF, value);
56 break;
57 case 3:
58 switch (value)
59 {
60 case 1:
61 yaz_snprintf(str, sz, "position=First in field");
62 break;
63 case 2:
64 yaz_snprintf(str, sz, "position=First in any subfield");
65 break;
66 case 3:
67 yaz_snprintf(str, sz, "position=Any position in field");
68 break;
69 default:
70 yaz_snprintf(str, sz, "position");
71 }
72 break;
73 case 4:
74 switch (value)
75 {
76 case 1:
77 yaz_snprintf(str, sz, "structure=Phrase");
78 break;
79 case 2:
80 yaz_snprintf(str, sz, "structure=Word");
81 break;
82 case 3:
83 yaz_snprintf(str, sz, "structure=Key");
84 break;
85 case 4:
86 yaz_snprintf(str, sz, "structure=Year");
87 break;
88 case 5:
89 yaz_snprintf(str, sz, "structure=Date");
90 break;
91 case 6:
92 yaz_snprintf(str, sz, "structure=Word list");
93 break;
94 case 100:
95 yaz_snprintf(str, sz, "structure=Date (un)");
96 break;
97 case 101:
98 yaz_snprintf(str, sz, "structure=Name (norm)");
99 break;
100 case 102:
101 yaz_snprintf(str, sz, "structure=Name (un)");
102 break;
103 case 103:
104 yaz_snprintf(str, sz, "structure=Structure");
105 break;
106 case 104:
107 yaz_snprintf(str, sz, "structure=urx");
108 break;
109 case 105:
110 yaz_snprintf(str, sz, "structure=free-form-text");
111 break;
112 case 106:
113 yaz_snprintf(str, sz, "structure=document-text");
114 break;
115 case 107:
116 yaz_snprintf(str, sz, "structure=local-number");
117 break;
118 case 108:
119 yaz_snprintf(str, sz, "structure=string");
120 break;
121 case 109:
122 yaz_snprintf(str, sz, "structure=numeric string");
123 break;
124 default:
125 yaz_snprintf(str, sz, "structure");
126 }
127 break;
128 case 5:
129 switch (value)
130 {
131 case 1:
132 yaz_snprintf(str, sz, "truncation=Right");
133 break;
134 case 2:
135 yaz_snprintf(str, sz, "truncation=Left");
136 break;
137 case 3:
138 yaz_snprintf(str, sz, "truncation=Left&right");
139 break;
140 case 100:
141 yaz_snprintf(str, sz, "truncation=Do not truncate");
142 break;
143 case 101:
144 yaz_snprintf(str, sz, "truncation=Process #");
145 break;
146 case 102:
147 yaz_snprintf(str, sz, "truncation=re-1");
148 break;
149 case 103:
150 yaz_snprintf(str, sz, "truncation=re-2");
151 break;
152 case 104:
153 yaz_snprintf(str, sz, "truncation=CCL");
154 break;
155 default:
156 yaz_snprintf(str, sz, "truncation");
157 }
158 break;
159 case 6:
160 switch(value)
161 {
162 case 1:
163 yaz_snprintf(str, sz, "completeness=Incomplete subfield");
164 break;
165 case 2:
166 yaz_snprintf(str, sz, "completeness=Complete subfield");
167 break;
168 case 3:
169 yaz_snprintf(str, sz, "completeness=Complete field");
170 break;
171 default:
172 yaz_snprintf(str, sz, "completeness");
173 }
174 break;
175 }
176 if (*str)
177 yaz_snprintf(str + strlen(str), sz - strlen(str), " (" ODR_INT_PRINTF "=" ODR_INT_PRINTF")",
178 type, value);
179 else
180 yaz_snprintf(str, sz, ODR_INT_PRINTF "=" ODR_INT_PRINTF, type, value);
181}
182
183/*
184 * zlog_attributes: print attributes of term
185 */
186static void zlog_attributes(Z_AttributesPlusTerm *t, int depth,
187 const Odr_oid *ast, int loglevel)
188{
189 int of, i;
190 char str[80];
191 int num_attributes = t->attributes->num_attributes;
192
193 for (of = 0; of < num_attributes; of++)
194 {
195 char attset_name_buf[OID_STR_MAX];
196 const char *attset_name = 0;
197 Z_AttributeElement *element;
198 element = t->attributes->attributes[of];
199 if (element->attributeSet)
200 {
201 attset_name = yaz_oid_to_string_buf(element->attributeSet,
202 0, attset_name_buf);
203 }
204 if (!attset_name)
205 attset_name = "";
206 switch (element->which)
207 {
209 attrStr(*element->attributeType,
210 *element->value.numeric, str, sizeof str);
211 yaz_log(loglevel, "%*.0s%s %s", depth, "", attset_name, str);
212 break;
214 yaz_log(loglevel, "%*.0s%s attributeType=" ODR_INT_PRINTF
215 " complex",
216 depth, "", attset_name, *element->attributeType);
217 for (i = 0; i<element->value.complex->num_list; i++)
218 {
219 if (element->value.complex->list[i]->which ==
221 yaz_log(loglevel, "%*.0s string: '%s'", depth, "",
222 element->value.complex->list[i]->u.string);
223 else if (element->value.complex->list[i]->which ==
225 yaz_log(loglevel, "%*.0s numeric: '" ODR_INT_PRINTF
226 " '", depth, "",
227 *element->value.complex->list[i]->u.numeric);
228 }
229 break;
230 default:
231 yaz_log(loglevel, "%.*s%s attribute unknown",
232 depth, "", attset_name);
233 }
234 }
235}
236
238{
239 switch (op->which)
240 {
241 case Z_Operator_and:
242 return "and";
243 case Z_Operator_or:
244 return "or";
246 return "not";
247 case Z_Operator_prox:
248 return "prox";
249 default:
250 return "unknown complex operator";
251 }
252}
253
255{
257 return "private";
258 const char *n = z_ProxUnit_to_str(*op->u.known);
259 if (n)
260 return n;
261 return "unknown";
262}
263
264static void zlog_structure(Z_RPNStructure *zs, int depth,
265 const Odr_oid *ast, int loglevel)
266{
267 if (zs->which == Z_RPNStructure_complex)
268 {
269 Z_Operator *op = zs->u.complex->roperator;
270 switch (op->which)
271 {
272 case Z_Operator_and:
273 case Z_Operator_or:
275 yaz_log(loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
276 break;
277 case Z_Operator_prox:
278 yaz_log(loglevel, "%*.0s prox excl=%s dist=" ODR_INT_PRINTF
279 " order=%s "
280 "rel=%s unit=%s",
281 depth, "", op->u.prox->exclusion ?
282 (*op->u.prox->exclusion ? "T" : "F") : "N",
283 *op->u.prox->distance,
284 *op->u.prox->ordered ? "T" : "F",
287 break;
288 default:
289 yaz_log(loglevel, "%*.0s unknown complex", depth, "");
290 return;
291 }
292 zlog_structure(zs->u.complex->s1, depth+2, ast, loglevel);
293 zlog_structure(zs->u.complex->s2, depth+2, ast, loglevel);
294 }
295 else if (zs->which == Z_RPNStructure_simple)
296 {
297 if (zs->u.simple->which == Z_Operand_APT)
298 {
300
301 switch (zapt->term->which)
302 {
303 case Z_Term_general:
304 yaz_log(loglevel, "%*.0s term '%.*s' (general)", depth, "",
305 zapt->term->u.general->len,
306 zapt->term->u.general->buf);
307 break;
309 yaz_log(loglevel, "%*.0s term '%s' (string)", depth, "",
310 zapt->term->u.characterString);
311 break;
312 case Z_Term_numeric:
313 yaz_log(loglevel, "%*.0s term '" ODR_INT_PRINTF
314 "' (numeric)", depth, "",
315 *zapt->term->u.numeric);
316 break;
317 case Z_Term_null:
318 yaz_log(loglevel, "%*.0s term (null)", depth, "");
319 break;
320 default:
321 yaz_log(loglevel, "%*.0s term (not general)", depth, "");
322 }
323 zlog_attributes(zapt, depth+2, ast, loglevel);
324 }
325 else if (zs->u.simple->which == Z_Operand_resultSetId)
326 {
327 yaz_log(loglevel, "%*.0s set '%s'", depth, "",
328 zs->u.simple->u.resultSetId);
329 }
330 else
331 yaz_log(loglevel, "%*.0s unknown simple structure", depth, "");
332 }
333 else
334 yaz_log(loglevel, "%*.0s unknown structure", depth, "");
335}
336
337void log_rpn_query_level(int loglevel, Z_RPNQuery *rpn)
338{
339 zlog_structure(rpn->RPNStructure, 0, rpn->attributeSetId, loglevel);
340}
341
343{
345}
346
347void log_scan_term_level(int loglevel,
348 Z_AttributesPlusTerm *zapt, const Odr_oid *ast)
349{
350 int depth = 0;
351 if (!loglevel)
352 return;
353 if (zapt->term->which == Z_Term_general)
354 {
355 yaz_log(loglevel, "%*.0s term '%.*s' (general)", depth, "",
356 zapt->term->u.general->len, zapt->term->u.general->buf);
357 }
358 else
359 yaz_log(loglevel, "%*.0s term (not general)", depth, "");
360 zlog_attributes(zapt, depth+2, ast, loglevel);
361}
362
364{
365 log_scan_term_level(YLOG_LOG, zapt, ast);
366}
367
368void yaz_log_zquery_level(int loglevel, Z_Query *q)
369{
370 if (!loglevel)
371 return;
372 switch (q->which)
373 {
375 log_rpn_query_level(loglevel, q->u.type_1);
376 break;
377 case Z_Query_type_2:
378 yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
379 break;
380 case Z_Query_type_100:
381 yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
382 q->u.type_100->buf);
383 break;
384 case Z_Query_type_104:
385 if (q->u.type_104->which == Z_External_CQL)
386 yaz_log(loglevel, "CQL: %s", q->u.type_104->u.cql);
387 }
388}
389
394
395/*
396 * Local variables:
397 * c-basic-offset: 4
398 * c-file-style: "Stroustrup"
399 * indent-tabs-mode: nil
400 * End:
401 * vim: shiftwidth=4 tabstop=8 expandtab
402 */
403
enum l_file_type type
Definition log.c:47
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition log.c:487
Logging utility.
#define YLOG_LOG
log level: log (regular)
Definition log.h:48
static void attrStr(Odr_int type, Odr_int value, char *str, size_t sz)
Definition logrpn.c:41
void yaz_log_zquery(Z_Query *q)
Definition logrpn.c:390
void log_scan_term_level(int loglevel, Z_AttributesPlusTerm *zapt, const Odr_oid *ast)
Definition logrpn.c:347
void log_rpn_query(Z_RPNQuery *rpn)
Definition logrpn.c:342
void log_rpn_query_level(int loglevel, Z_RPNQuery *rpn)
Definition logrpn.c:337
void yaz_log_zquery_level(int loglevel, Z_Query *q)
Definition logrpn.c:368
static const char * relToStr(Odr_int v)
Definition logrpn.c:22
void log_scan_term(Z_AttributesPlusTerm *zapt, const Odr_oid *ast)
Definition logrpn.c:363
const char * yaz_prox_unit_name(Z_ProximityOperator *op)
Definition logrpn.c:254
static char * complex_op_name(Z_Operator *op)
Definition logrpn.c:237
static void zlog_attributes(Z_AttributesPlusTerm *t, int depth, const Odr_oid *ast, int loglevel)
Definition logrpn.c:186
static void zlog_structure(Z_RPNStructure *zs, int depth, const Odr_oid *ast, int loglevel)
Definition logrpn.c:264
Header for Z39.50 Query Printing.
#define ODR_INT_PRINTF
Definition odr.h:49
nmem_int_t Odr_int
Definition odr.h:47
const char * yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
maps any OID to string (named or dot-notation)
Definition oid_db.c:99
Header for OID database.
short Odr_oid
Definition oid_util.h:42
#define OID_STR_MAX
Definition oid_util.h:40
const char * z_ProxUnit_to_str(int u)
converts prox unit integer to string
Definition proxunit.c:33
Header for JSON functions.
#define Z_External_CQL
Definition prt-ext.h:93
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Definition snprintf.c:31
Header for config file reading utilities.
Odr_int * attributeType
Definition z-core.h:581
Z_ComplexAttribute * complex
Definition z-core.h:585
Z_AttributeSetId * attributeSet
Definition z-core.h:580
Odr_int * numeric
Definition z-core.h:584
union Z_AttributeElement::@260252174261265367116362007076255375270002041323 value
int num_attributes
Definition z-core.h:532
Z_AttributeElement ** attributes
Definition z-core.h:533
Z_AttributeList * attributes
Definition z-core.h:522
Z_StringOrNumeric ** list
Definition z-core.h:574
Z_RPNStructure * s2
Definition z-core.h:495
Z_Operator * roperator
Definition z-core.h:496
Z_RPNStructure * s1
Definition z-core.h:494
union Z_External::@173112132151266201036013025012152147264102163302 u
Z_InternationalString * cql
Definition prt-ext.h:138
int which
Definition prt-ext.h:63
Z_ResultSetId * resultSetId
Definition z-core.h:513
int which
Definition z-core.h:510
Z_AttributesPlusTerm * attributesPlusTerm
Definition z-core.h:512
union Z_Operand::@072322006164213251104156071070134267373322123052 u
int which
Definition z-core.h:559
Z_ProximityOperator * prox
Definition z-core.h:564
union Z_Operator::@171225215357037113014354143242105145344254361002 u
Odr_bool * ordered
Definition z-core.h:594
Odr_int * distance
Definition z-core.h:593
Z_ProxUnit * known
Definition z-core.h:604
Odr_int * relationType
Definition z-core.h:601
Odr_bool * exclusion
Definition z-core.h:592
union Z_ProximityOperator::@031336250334025346341060256205106006053041305237 u
union Z_Query::@270220245041066023256025363242165325012357336235 u
Z_External * type_104
Definition z-core.h:477
Z_RPNQuery * type_1
Definition z-core.h:472
Odr_oct * type_2
Definition z-core.h:473
int which
Definition z-core.h:469
Odr_oct * type_100
Definition z-core.h:474
Z_AttributeSetId * attributeSetId
Definition z-core.h:489
Z_RPNStructure * RPNStructure
Definition z-core.h:490
Z_Complex * complex
Definition z-core.h:503
union Z_RPNStructure::@272042053041255367154306203353273370010236313243 u
Z_Operand * simple
Definition z-core.h:502
union Z_StringOrNumeric::@255321135104110073015132067151226017215302214211 u
Odr_int * numeric
Definition z-core.h:1322
Z_InternationalString * string
Definition z-core.h:1321
union Z_Term::@023217361022206241314262227377164117366363003164 u
Odr_oct * general
Definition z-core.h:539
Z_InternationalString * characterString
Definition z-core.h:541
int which
Definition z-core.h:537
Odr_int * numeric
Definition z-core.h:540
int len
Definition odr.h:102
char * buf
Definition odr.h:101
#define Z_AttributeValue_numeric
Definition z-core.h:586
#define Z_Operator_and_not
Definition z-core.h:567
#define Z_Operator_or
Definition z-core.h:566
#define Z_Term_characterString
Definition z-core.h:549
#define Z_StringOrNumeric_numeric
Definition z-core.h:1324
#define Z_Operand_APT
Definition z-core.h:515
#define Z_ProximityOperator_known
Definition z-core.h:606
#define Z_AttributeValue_complex
Definition z-core.h:587
#define Z_Term_general
Definition z-core.h:547
#define Z_StringOrNumeric_string
Definition z-core.h:1323
#define Z_Query_type_1
Definition z-core.h:479
#define Z_Operator_prox
Definition z-core.h:568
#define Z_Query_type_101
Definition z-core.h:482
#define Z_Operand_resultSetId
Definition z-core.h:516
#define Z_RPNStructure_complex
Definition z-core.h:505
#define Z_Operator_and
Definition z-core.h:565
#define Z_Term_numeric
Definition z-core.h:548
#define Z_RPNStructure_simple
Definition z-core.h:504
#define Z_Term_null
Definition z-core.h:554
#define Z_Query_type_104
Definition z-core.h:484
#define Z_Query_type_100
Definition z-core.h:481
#define Z_Query_type_2
Definition z-core.h:480