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