AT&T ECOMP Vendor Event Listener library  0.1
evel_json_buffer.c
Go to the documentation of this file.
1 /**************************************************************************/
37 #include <assert.h>
38 #include <string.h>
39 
40 #include "evel_throttle.h"
41 
42 /*****************************************************************************/
43 /* Local prototypes. */
44 /*****************************************************************************/
45 static char * evel_json_kv_comma(EVEL_JSON_BUFFER * jbuf);
46 
47 /**************************************************************************/
56  char * const json,
57  const int max_size,
58  EVEL_THROTTLE_SPEC * throttle_spec)
59 {
60  EVEL_ENTER();
61 
62  assert(jbuf != NULL);
63  assert(json != NULL);
64  jbuf->json = json;
65  jbuf->max_size = max_size;
66  jbuf->offset = 0;
67  jbuf->throttle_spec = throttle_spec;
68  jbuf->depth = 0;
69  jbuf->checkpoint = -1;
70 
71  EVEL_EXIT();
72 }
73 
74 /**************************************************************************/
81  const int value)
82 {
83  EVEL_ENTER();
84 
85  /***************************************************************************/
86  /* Check preconditions. */
87  /***************************************************************************/
88  assert(jbuf != NULL);
89 
90  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
91  jbuf->max_size - jbuf->offset,
92  "%d", value);
93 
94  EVEL_EXIT();
95 }
96 
97 /**************************************************************************/
106  const char * const key,
107  const EVEL_OPTION_STRING * const option)
108 {
109  bool added = false;
110 
111  EVEL_ENTER();
112 
113  /***************************************************************************/
114  /* Check preconditions. */
115  /***************************************************************************/
116  assert(option != NULL);
117 
118  if (option->is_set)
119  {
120  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
121  (jbuf->throttle_spec != NULL) &&
123  {
124  EVEL_INFO("Suppressed: %s, %s", key, option->value);
125  }
126  else
127  {
128  EVEL_DEBUG("Encoded: %s, %s", key, option->value);
129  evel_enc_kv_string(jbuf, key, option->value);
130  added = true;
131  }
132  }
133 
134  EVEL_EXIT();
135 
136  return added;
137 }
138 
139 /**************************************************************************/
147  const char * const key,
148  const char * const value)
149 {
150  int index;
151  int length;
152 
153  EVEL_ENTER();
154 
155  /***************************************************************************/
156  /* Check preconditions. */
157  /***************************************************************************/
158  assert(jbuf != NULL);
159  assert(key != NULL);
160 
161  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
162  jbuf->max_size - jbuf->offset,
163  "%s\"%s\": \"",
164  evel_json_kv_comma(jbuf),
165  key);
166 
167  /***************************************************************************/
168  /* We need to escape quotation marks and backslashes in the value. */
169  /***************************************************************************/
170  length = strlen(value);
171 
172  for (index = 0; index < length; index++)
173  {
174  /*************************************************************************/
175  /* Drop out if no more space. */
176  /*************************************************************************/
177  if (jbuf->max_size - jbuf->offset < 2)
178  {
179  break;
180  }
181 
182  /*************************************************************************/
183  /* Add an escape character if necessary, then write the character */
184  /* itself. */
185  /*************************************************************************/
186  if ((value[index] == '\"') || (value[index] == '\\'))
187  {
188  jbuf->json[jbuf->offset] = '\\';
189  jbuf->offset++;
190  }
191 
192  jbuf->json[jbuf->offset] = value[index];
193  jbuf->offset++;
194  }
195 
196  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
197  jbuf->max_size - jbuf->offset,
198  "\"");
199 
200  EVEL_EXIT();
201 }
202 
203 /**************************************************************************/
212  const char * const key,
213  const EVEL_OPTION_INT * const option)
214 {
215  bool added = false;
216 
217  EVEL_ENTER();
218 
219  /***************************************************************************/
220  /* Check preconditions. */
221  /***************************************************************************/
222  assert(option != NULL);
223 
224  if (option->is_set)
225  {
226  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
227  (jbuf->throttle_spec != NULL) &&
229  {
230  EVEL_INFO("Suppressed: %s, %d", key, option->value);
231  }
232  else
233  {
234  EVEL_DEBUG("Encoded: %s, %d", key, option->value);
235  evel_enc_kv_int(jbuf, key, option->value);
236  added = true;
237  }
238  }
239 
240  EVEL_EXIT();
241 
242  return added;
243 }
244 
245 /**************************************************************************/
253  const char * const key,
254  const int value)
255 {
256  EVEL_ENTER();
257 
258  /***************************************************************************/
259  /* Check preconditions. */
260  /***************************************************************************/
261  assert(jbuf != NULL);
262  assert(key != NULL);
263 
264  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
265  jbuf->max_size - jbuf->offset,
266  "%s\"%s\": %d",
267  evel_json_kv_comma(jbuf),
268  key,
269  value);
270 
271  EVEL_EXIT();
272 }
273 
274 /**************************************************************************/
283  const char * const key,
284  const EVEL_OPTION_DOUBLE * const option)
285 {
286  bool added = false;
287 
288  EVEL_ENTER();
289 
290  /***************************************************************************/
291  /* Check preconditions. */
292  /***************************************************************************/
293  assert(option != NULL);
294 
295  if (option->is_set)
296  {
297  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
298  (jbuf->throttle_spec != NULL) &&
300  {
301  EVEL_INFO("Suppressed: %s, %1f", key, option->value);
302  }
303  else
304  {
305  EVEL_DEBUG("Encoded: %s, %1f", key, option->value);
306  evel_enc_kv_double(jbuf, key, option->value);
307  added = true;
308  }
309  }
310 
311  EVEL_EXIT();
312 
313  return added;
314 }
315 
316 /**************************************************************************/
324  const char * const key,
325  const double value)
326 {
327  EVEL_ENTER();
328 
329  /***************************************************************************/
330  /* Check preconditions. */
331  /***************************************************************************/
332  assert(jbuf != NULL);
333  assert(key != NULL);
334 
335  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
336  jbuf->max_size - jbuf->offset,
337  "%s\"%s\": %1f",
338  evel_json_kv_comma(jbuf),
339  key,
340  value);
341 
342  EVEL_EXIT();
343 }
344 
345 /**************************************************************************/
354  const char * const key,
355  const EVEL_OPTION_ULL * const option)
356 {
357  bool added = false;
358 
359  EVEL_ENTER();
360 
361  /***************************************************************************/
362  /* Check preconditions. */
363  /***************************************************************************/
364  assert(option != NULL);
365 
366  if (option->is_set)
367  {
368  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
369  (jbuf->throttle_spec != NULL) &&
371  {
372  EVEL_INFO("Suppressed: %s, %1lu", key, option->value);
373  }
374  else
375  {
376  EVEL_DEBUG("Encoded: %s, %1lu", key, option->value);
377  evel_enc_kv_ull(jbuf, key, option->value);
378  added = true;
379  }
380  }
381 
382  EVEL_EXIT();
383 
384  return added;
385 }
386 
387 /**************************************************************************/
395  const char * const key,
396  const unsigned long long value)
397 {
398  EVEL_ENTER();
399 
400  /***************************************************************************/
401  /* Check preconditions. */
402  /***************************************************************************/
403  assert(jbuf != NULL);
404  assert(key != NULL);
405 
406  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
407  jbuf->max_size - jbuf->offset,
408  "%s\"%s\": %llu",
409  evel_json_kv_comma(jbuf),
410  key,
411  value);
412 
413  EVEL_EXIT();
414 }
415 
416 /**************************************************************************/
425  const char * const key,
426  const EVEL_OPTION_TIME * const option)
427 {
428  bool added = false;
429 
430  EVEL_ENTER();
431 
432  /***************************************************************************/
433  /* Check preconditions. */
434  /***************************************************************************/
435  assert(option != NULL);
436 
437  if (option->is_set)
438  {
439  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
440  (jbuf->throttle_spec != NULL) &&
442  {
443  EVEL_INFO("Suppressed time: %s", key);
444  }
445  else
446  {
447  EVEL_DEBUG("Encoded time: %s", key);
448  evel_enc_kv_time(jbuf, key, &option->value);
449  added = true;
450  }
451  }
452 
453  EVEL_EXIT();
454 
455  return added;
456 }
457 
458 /**************************************************************************/
466  const char * const key,
467  const time_t * time)
468 {
469  EVEL_ENTER();
470 
471  /***************************************************************************/
472  /* Check preconditions. */
473  /***************************************************************************/
474  assert(jbuf != NULL);
475  assert(key != NULL);
476  assert(time != NULL);
477 
478  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
479  jbuf->max_size - jbuf->offset,
480  "%s\"%s\": \"",
481  evel_json_kv_comma(jbuf),
482  key);
483  jbuf->offset += strftime(jbuf->json + jbuf->offset,
484  jbuf->max_size - jbuf->offset,
486  localtime(time));
487  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
488  jbuf->max_size - jbuf->offset,
489  "\"");
490  EVEL_EXIT();
491 }
492 
493 /**************************************************************************/
502  const char * const key,
503  const int major_version,
504  const int minor_version)
505 {
506  EVEL_ENTER();
507 
508  /***************************************************************************/
509  /* Check preconditions. */
510  /***************************************************************************/
511  assert(jbuf != NULL);
512  assert(key != NULL);
513 
514  evel_enc_kv_int(jbuf, key, major_version);
515  if (minor_version != 0)
516  {
517  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
518  jbuf->max_size - jbuf->offset,
519  ".%d",
520  minor_version);
521  }
522 
523  EVEL_EXIT();
524 }
525 
526 /**************************************************************************/
534  const char * const key)
535 {
536  bool opened = false;
537 
538  EVEL_ENTER();
539 
540  /***************************************************************************/
541  /* Check preconditions. */
542  /***************************************************************************/
543  assert(jbuf != NULL);
544  assert(key != NULL);
545 
546  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
547  (jbuf->throttle_spec != NULL) &&
549  {
550  EVEL_INFO("Suppressed: %s", key);
551  opened = false;
552  }
553  else
554  {
555  evel_json_open_named_list(jbuf, key);
556  opened = true;
557  }
558 
559  EVEL_EXIT();
560 
561  return opened;
562 }
563 
564 /**************************************************************************/
571  const char * const key)
572 {
573  EVEL_ENTER();
574 
575  /***************************************************************************/
576  /* Check preconditions. */
577  /***************************************************************************/
578  assert(jbuf != NULL);
579  assert(key != NULL);
580 
581  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
582  jbuf->max_size - jbuf->offset,
583  "%s\"%s\": [",
584  evel_json_kv_comma(jbuf),
585  key);
586  jbuf->depth++;
587 
588  EVEL_EXIT();
589 }
590 
591 /**************************************************************************/
597 {
598  EVEL_ENTER();
599 
600  /***************************************************************************/
601  /* Check preconditions. */
602  /***************************************************************************/
603  assert(jbuf != NULL);
604 
605  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
606  jbuf->max_size - jbuf->offset,
607  "]");
608  jbuf->depth--;
609 
610  EVEL_EXIT();
611 }
612 
613 /**************************************************************************/
621  const char * const format,
622  ...)
623 {
624  va_list largs;
625 
626  EVEL_ENTER();
627 
628  /***************************************************************************/
629  /* Check preconditions. */
630  /***************************************************************************/
631  assert(jbuf != NULL);
632  assert(format != NULL);
633 
634  /***************************************************************************/
635  /* Add a comma unless we're at the start of the list. */
636  /***************************************************************************/
637  if (jbuf->json[jbuf->offset - 1] != '[')
638  {
639  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
640  jbuf->max_size - jbuf->offset,
641  ", ");
642  }
643 
644  va_start(largs, format);
645  jbuf->offset += vsnprintf(jbuf->json + jbuf->offset,
646  jbuf->max_size - jbuf->offset,
647  format,
648  largs);
649  va_end(largs);
650 
651  EVEL_EXIT();
652 }
653 
654 /**************************************************************************/
661  const char * const key)
662 {
663  bool opened = false;
664 
665  EVEL_ENTER();
666 
667  /***************************************************************************/
668  /* Check preconditions. */
669  /***************************************************************************/
670  assert(jbuf != NULL);
671  assert(key != NULL);
672 
673  if ((jbuf->depth == EVEL_THROTTLE_FIELD_DEPTH) &&
674  (jbuf->throttle_spec != NULL) &&
676  {
677  EVEL_INFO("Suppressed: %s", key);
678  opened = false;
679  }
680  else
681  {
682  evel_json_open_named_object(jbuf, key);
683  opened = true;
684  }
685 
686  EVEL_EXIT();
687 
688  return opened;
689 }
690 
691 /**************************************************************************/
699  const char * const key)
700 {
701  EVEL_ENTER();
702 
703  /***************************************************************************/
704  /* Check preconditions. */
705  /***************************************************************************/
706  assert(jbuf != NULL);
707  assert(key != NULL);
708 
709  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
710  jbuf->max_size - jbuf->offset,
711  "%s\"%s\": {",
712  evel_json_kv_comma(jbuf),
713  key);
714  jbuf->depth++;
715 
716  EVEL_EXIT();
717 }
718 
719 /**************************************************************************/
725 {
726  char * comma;
727 
728  EVEL_ENTER();
729 
730  /***************************************************************************/
731  /* Check preconditions. */
732  /***************************************************************************/
733  assert(jbuf != NULL);
734 
735  if ((jbuf->offset != 0) && (jbuf->json[jbuf->offset-1] == '}'))
736  {
737  comma = ", ";
738  }
739  else
740  {
741  comma = "";
742  }
743 
744  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
745  jbuf->max_size - jbuf->offset,
746  "%s{",
747  comma);
748  jbuf->depth++;
749 
750  EVEL_EXIT();
751 }
752 
753 /**************************************************************************/
759 {
760  EVEL_ENTER();
761 
762  /***************************************************************************/
763  /* Check preconditions. */
764  /***************************************************************************/
765  assert(jbuf != NULL);
766 
767  jbuf->offset += snprintf(jbuf->json + jbuf->offset,
768  jbuf->max_size - jbuf->offset,
769  "}");
770  jbuf->depth--;
771 
772  EVEL_EXIT();
773 }
774 
775 /**************************************************************************/
781 char * evel_json_kv_comma(EVEL_JSON_BUFFER * jbuf)
782 {
783  char * result;
784 
785  EVEL_ENTER();
786 
787  /***************************************************************************/
788  /* Check preconditions. */
789  /***************************************************************************/
790  assert(jbuf != NULL);
791 
792  if ((jbuf->offset == 0) ||
793  (jbuf->json[jbuf->offset-1] == '{') ||
794  (jbuf->json[jbuf->offset-1] == '['))
795  {
796  result = "";
797  }
798  else
799  {
800  result = ", ";
801  }
802 
803  EVEL_EXIT();
804 
805  return result;
806 }
807 
808 /**************************************************************************/
814 {
815  EVEL_ENTER();
816 
817  /***************************************************************************/
818  /* Check preconditions. */
819  /***************************************************************************/
820  assert(jbuf != NULL);
821 
822  /***************************************************************************/
823  /* Store the current offset. */
824  /***************************************************************************/
825  jbuf->checkpoint = jbuf->offset;
826 
827  EVEL_EXIT();
828 }
829 
830 /**************************************************************************/
836 {
837  EVEL_ENTER();
838 
839  /***************************************************************************/
840  /* Check preconditions. */
841  /***************************************************************************/
842  assert(jbuf != NULL);
843  assert(jbuf->checkpoint >= 0);
844  assert(jbuf->checkpoint <= jbuf->offset);
845 
846  /***************************************************************************/
847  /* Reinstate the offset from the last checkpoint. */
848  /***************************************************************************/
849  jbuf->offset = jbuf->checkpoint;
850  jbuf->checkpoint = -1;
851 
852  EVEL_EXIT();
853 }
#define EVEL_DEBUG(FMT,...)
Definition: evel.h:3621
void evel_enc_kv_int(EVEL_JSON_BUFFER *jbuf, const char *const key, const int value)
Encode a string key and integer value to a EVEL_JSON_BUFFER.
void evel_json_checkpoint(EVEL_JSON_BUFFER *jbuf)
Add a checkpoint - a stake in the ground to which we can rewind.
double value
Definition: evel.h:360
#define EVEL_INFO(FMT,...)
Definition: evel.h:3622
Optional parameter holder for unsigned long long.
Definition: evel.h:385
Optional parameter holder for double.
Definition: evel.h:358
Optional parameter holder for string.
Definition: evel.h:367
void evel_enc_int(EVEL_JSON_BUFFER *jbuf, const int value)
Encode an integer value to a JSON buffer.
void evel_json_rewind(EVEL_JSON_BUFFER *jbuf)
Rewind to the latest checkoint.
bool evel_enc_kv_opt_time(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_TIME *const option)
Encode a string key and time value to a EVEL_JSON_BUFFER.
EVEL_THROTTLE_SPEC * throttle_spec
#define EVEL_THROTTLE_FIELD_DEPTH
bool evel_enc_kv_opt_double(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_DOUBLE *const option)
Encode a string key and double value to a EVEL_JSON_BUFFER.
bool evel_json_open_opt_named_object(EVEL_JSON_BUFFER *jbuf, const char *const key)
Add the opening bracket of an optional named object to a JSON buffer.
bool evel_throttle_suppress_field(EVEL_THROTTLE_SPEC *throttle_spec, const char *const field_name)
Determine whether a field_name should be suppressed.
void evel_enc_kv_ull(EVEL_JSON_BUFFER *jbuf, const char *const key, const unsigned long long value)
Encode a string key and unsigned long long value to a EVEL_JSON_BUFFER.
#define EVEL_EXIT()
Definition: evel.h:3631
void evel_json_buffer_init(EVEL_JSON_BUFFER *jbuf, char *const json, const int max_size, EVEL_THROTTLE_SPEC *throttle_spec)
Initialize a EVEL_JSON_BUFFER.
#define EVEL_ENTER()
Definition: evel.h:3626
EVEL_BOOLEAN is_set
Definition: evel.h:379
void evel_enc_kv_double(EVEL_JSON_BUFFER *jbuf, const char *const key, const double value)
Encode a string key and double value to a EVEL_JSON_BUFFER.
bool evel_enc_kv_opt_int(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_INT *const option)
Encode a string key and integer value to a EVEL_JSON_BUFFER.
EVEL_BOOLEAN is_set
Definition: evel.h:370
#define EVEL_RFC2822_STRFTIME_FORMAT
time_t value
Definition: evel.h:396
void evel_enc_kv_string(EVEL_JSON_BUFFER *jbuf, const char *const key, const char *const value)
Encode a string key and string value to a EVEL_JSON_BUFFER.
Event Throttling Specification for a domain which is in a throttled state.
Optional parameter holder for int.
Definition: evel.h:376
bool evel_enc_kv_opt_ull(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_ULL *const option)
Encode a string key and unsigned long long value to a EVEL_JSON_BUFFER.
unsigned long long value
Definition: evel.h:387
void evel_json_open_named_object(EVEL_JSON_BUFFER *jbuf, const char *const key)
Add the opening bracket of an object to a JSON buffer.
void evel_enc_version(EVEL_JSON_BUFFER *jbuf, const char *const key, const int major_version, const int minor_version)
Encode a key and version.
void evel_json_open_named_list(EVEL_JSON_BUFFER *jbuf, const char *const key)
Add the key and opening bracket of a named list to a JSON buffer.
EVEL_BOOLEAN is_set
Definition: evel.h:388
void evel_enc_kv_time(EVEL_JSON_BUFFER *jbuf, const char *const key, const time_t *time)
Encode a string key and time value to a EVEL_JSON_BUFFER.
char * value
Definition: evel.h:369
bool evel_enc_kv_opt_string(EVEL_JSON_BUFFER *jbuf, const char *const key, const EVEL_OPTION_STRING *const option)
Encode a string key and string value to a EVEL_JSON_BUFFER.
EVEL_BOOLEAN is_set
Definition: evel.h:397
EVEL throttle definitions.
EVEL_BOOLEAN is_set
Definition: evel.h:361
Optional parameter holder for time_t.
Definition: evel.h:394
void evel_json_close_list(EVEL_JSON_BUFFER *jbuf)
Add the closing bracket of a list to a JSON buffer.
void evel_enc_list_item(EVEL_JSON_BUFFER *jbuf, const char *const format,...)
Encode a list item with format and param list to a EVEL_JSON_BUFFER.
void evel_json_open_object(EVEL_JSON_BUFFER *jbuf)
Add the opening bracket of an object to a JSON buffer.
void evel_json_close_object(EVEL_JSON_BUFFER *jbuf)
Add the closing bracket of an object to a JSON buffer.
bool evel_json_open_opt_named_list(EVEL_JSON_BUFFER *jbuf, const char *const key)
Add the key and opening bracket of an optional named list to a JSON buffer.