/// @TODO we should print all with this function static void print_monitor_value_enrichment(struct printbuf *buf, const json_object *const_enrichment) { json_object *enrichment = (json_object *)const_enrichment; for (struct json_object_iterator i = json_object_iter_begin(enrichment), end = json_object_iter_end(enrichment); !json_object_iter_equal(&i, &end); json_object_iter_next(&i)) { const char *key = json_object_iter_peek_name(&i); json_object *val = json_object_iter_peek_value(&i); const json_type type = json_object_get_type(val); switch (type) { case json_type_string: print_monitor_value_enrichment_str(buf, key, val); break; case json_type_int: print_monitor_value_enrichment_int(buf, key, val); break; case json_type_null: sprintbuf(buf, ",\"%s\":null", key); break; case json_type_boolean: { const json_bool b = json_object_get_boolean(val); sprintbuf(buf, ",\"%s\":%s", key, b == FALSE ? "false" : "true"); break; } case json_type_double: { const double d = json_object_get_double(val); sprintbuf(buf, ",\"%s\":%lf", key, d); break; } case json_type_object: case json_type_array: { rdlog(LOG_ERR, "Can't enrich with objects/array at this time"); break; } default: rdlog(LOG_ERR, "Don't know how to duplicate JSON type " "%d", type); break; }; } }
static int custom_serializer(struct json_object *o, struct printbuf *pb, int level, int flags) { sprintbuf(pb, "Custom Output"); return 0; }
static void test_basic_printbuf_memset() { struct printbuf *pb; printf("%s: starting test\n", __func__); pb = printbuf_new(); sprintbuf(pb, "blue:%d", 1); printbuf_memset(pb, -1, 'x', 52); printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf); printbuf_free(pb); printf("%s: end test\n", __func__); }
static void print_monitor_value_enrichment_str(struct printbuf *buf, const char *key, json_object *val) { const char *str = json_object_get_string(val); if (NULL == str) { rdlog(LOG_ERR, "Cannot extract string value of enrichment key %s", key); } else { sprintbuf(buf, ",\"%s\":\"%s\"", key, str); } }
static void test_sprintbuf(int before_resize) { struct printbuf *pb; printf("%s: starting test\n", __func__); pb = printbuf_new(); printf("Buffer length: %d\n", printbuf_length(pb)); char *data = malloc(before_resize + 1 + 1); memset(data, 'X', before_resize + 1 + 1); data[before_resize + 1] = '\0'; sprintbuf(pb, "%s", data); free(data); printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf)); printbuf_reset(pb); sprintbuf(pb, "plain"); printf("%d, [%s]\n", printbuf_length(pb), pb->buf); sprintbuf(pb, "%d", 1); printf("%d, [%s]\n", printbuf_length(pb), pb->buf); sprintbuf(pb, "%d", INT_MAX); printf("%d, [%s]\n", printbuf_length(pb), pb->buf); sprintbuf(pb, "%d", INT_MIN); printf("%d, [%s]\n", printbuf_length(pb), pb->buf); sprintbuf(pb, "%s", "%s"); printf("%d, [%s]\n", printbuf_length(pb), pb->buf); printbuf_free(pb); printf("%s: end test\n", __func__); }
int main (int argc, char **argv) { struct arguments arguments; struct list_elem elem; struct list_elem *elemptr = &elem; elem.has_next = 0; arguments.include_files = elemptr; arguments.output_file = "-"; arguments.parsley = "-"; argp_parse (&argp, argc, argv, 0, 0, &arguments); struct printbuf* parsley = printbuf_new(); struct printbuf* incl = printbuf_new(); sprintbuf(parsley, ""); sprintbuf(incl, ""); FILE* in = parsley_fopen(arguments.parsley, "r"); printbuf_file_read(in, parsley); while(elemptr->has_next) { elemptr = elemptr->next; FILE* f = parsley_fopen(elemptr->string, "r"); printbuf_file_read(f, incl); fclose(f); } parsleyPtr compiled = parsley_compile(parsley->buf, incl->buf); if(compiled->error != NULL) { fprintf(stderr, "%s\n", compiled->error); exit(1); } FILE* fo = parsley_fopen(arguments.output_file, "w"); xmlDocFormatDump(fo, compiled->stylesheet->doc, 1); fclose(fo); return 0; }
static void print_monitor_value_enrichment_int(struct printbuf *buf, const char *key, json_object *val) { errno = 0; int64_t integer = json_object_get_int64(val); if (errno != 0) { char errbuf[BUFSIZ]; const char *errstr = strerror_r(errno, errbuf, sizeof(errbuf)); rdlog(LOG_ERR, "Cannot extract int value of enrichment key %s: %s", key, errstr); } else { sprintbuf(buf, ",\"%s\":%ld", key, integer); } }
static void print_monitor_value0(rb_message *message, const struct monitor_value *monitor_value, const rb_monitor_t *monitor, int instance) { assert(monitor_value->type == MONITOR_VALUE_T__VALUE); struct printbuf *buf = printbuf_new(); if (likely(NULL != buf)) { const char *monitor_instance_prefix = rb_monitor_instance_prefix(monitor); const char *monitor_name_split_suffix = rb_monitor_name_split_suffix(monitor); const struct json_object *monitor_enrichment = rb_monitor_enrichment(monitor); // @TODO use printbuf_memappend_fast instead! */ sprintbuf(buf, "{"); sprintbuf(buf, "\"timestamp\":%lu", monitor_value->value.timestamp); if (NO_INSTANCE != instance && monitor_name_split_suffix) { sprintbuf(buf, ",\"monitor\":\"%s%s\"", rb_monitor_name(monitor), monitor_name_split_suffix); } else { sprintbuf(buf, ",\"monitor\":\"%s\"", rb_monitor_name(monitor)); } if (NO_INSTANCE != instance && monitor_instance_prefix) { sprintbuf(buf, ",\"instance\":\"%s%d\"", monitor_instance_prefix, instance); } if (rb_monitor_is_integer(monitor)) { sprintbuf(buf, ",\"value\":%" PRId64, (int64_t)monitor_value->value.value); } else { sprintbuf(buf, ",\"value\":\"%lf\"", monitor_value->value.value); } if (rb_monitor_group_id(monitor)) { sprintbuf(buf, ",\"group_id\":%s", rb_monitor_group_id(monitor)); } if (monitor_enrichment) { print_monitor_value_enrichment(buf, monitor_enrichment); } sprintbuf(buf, "}"); message->payload = buf->buf; message->len = (size_t)buf->bpos; buf->buf = NULL; printbuf_free(buf); } }