From 67f6a0148c170294a82240a0ab92bbfaa6ee667e Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 24 Aug 2014 14:38:08 +1000 Subject: [PATCH] Provide a macro helper for json_pack that checks the return value and describes what the error is and where it came from --- src/libckpool.c | 10 ++++++++++ src/libckpool.h | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 572c860c..bc51fa40 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -933,6 +933,16 @@ out: } +void _json_check(json_t *val, json_error_t *err, const char *file, const char *func, const int line) +{ + if (likely(val)) + return; + + LOGERR("Invalid json line:%d col:%d pos:%d text: %s from %s %s:%d", + err->line, err->column, err->position, err->text, + file, func, line); +} + /* Extracts a string value from a json array with error checking. To be used * when the value of the string returned is only examined and not to be stored. * See json_array_string below */ diff --git a/src/libckpool.h b/src/libckpool.h index b3251370..98d3e54c 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -266,6 +266,17 @@ typedef struct unixsock unixsock_t; typedef struct proc_instance proc_instance_t; + +void _json_check(json_t *val, json_error_t *err, const char *file, const char *func, const int line); +#define json_check(val, err) _json_check(val, err, __FILE__, __func__, __LINE__) + +/* Check and pack json */ +#define JSON_CPACK(val, ...) do { \ + json_error_t err; \ + val = json_pack_ex(&err, 0, ##__VA_ARGS__); \ + json_check(val, &err); \ +} while (0) + /* No error checking with these, make sure we know they're valid already! */ static inline void json_strcpy(char *buf, json_t *val, const char *key) {