* Add macros to enable compiling out debug code

Geoffrey Young, geoff at modperlcookbook dot org


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@18 327403b1-1117-474d-bef2-5cb71233fd97
This commit is contained in:
Michael Clark
2007-10-25 02:26:00 +00:00
parent ecf2c9b74b
commit dfaf670436
9 changed files with 49 additions and 25 deletions

View File

@@ -1,3 +1,7 @@
0.8
* Add macros to enable compiling out debug code
Geoffrey Young, geoff at modperlcookbook dot org
0.7 0.7
* Add escaping of backslash to json output * Add escaping of backslash to json output
* Add escaping of foward slash on tokenizing and output * Add escaping of foward slash on tokenizing and output

View File

@@ -113,5 +113,5 @@
/* Define to rpl_realloc if the replacement function should be used. */ /* Define to rpl_realloc if the replacement function should be used. */
#undef realloc #undef realloc
/* Define to `unsigned' if <sys/types.h> does not define. */ /* Define to `unsigned int' if <sys/types.h> does not define. */
#undef size_t #undef size_t

18
debug.h
View File

@@ -21,4 +21,22 @@ extern void mc_debug(const char *msg, ...);
extern void mc_error(const char *msg, ...); extern void mc_error(const char *msg, ...);
extern void mc_info(const char *msg, ...); extern void mc_info(const char *msg, ...);
#ifdef MC_MAINTAINER_MODE
#define MC_SET_DEBUG(x) mc_set_debug(x)
#define MC_GET_DEBUG() mc_get_debug()
#define MC_SET_SYSLOG(x) mc_set_syslog(x)
#define MC_ABORT(x, ...) mc_abort(x, ##__VA_ARGS__)
#define MC_DEBUG(x, ...) mc_debug(x, ##__VA_ARGS__)
#define MC_ERROR(x, ...) mc_error(x, ##__VA_ARGS__)
#define MC_INFO(x, ...) mc_info(x, ##__VA_ARGS__)
#else
#define MC_SET_DEBUG(x) if (0) mc_set_debug(x)
#define MC_GET_DEBUG() (0)
#define MC_SET_SYSLOG(x) if (0) mc_set_syslog(x)
#define MC_ABORT(x, ...) if (0) mc_abort(x, ##__VA_ARGS__)
#define MC_DEBUG(x, ...) if (0) mc_debug(x, ##__VA_ARGS__)
#define MC_ERROR(x, ...) if (0) mc_error(x, ##__VA_ARGS__)
#define MC_INFO(x, ...) if (0) mc_info(x, ##__VA_ARGS__)
#endif
#endif #endif

View File

@@ -27,7 +27,7 @@
char* strndup(const char* str, size_t n); char* strndup(const char* str, size_t n);
#endif /* !HAVE_STRNDUP */ #endif /* !HAVE_STRNDUP */
/* #define REFCOUNT_DEBUG 1 */ #define REFCOUNT_DEBUG 1
char *json_number_chars = "0123456789.+-e"; char *json_number_chars = "0123456789.+-e";
char *json_hex_chars = "0123456789abcdef"; char *json_hex_chars = "0123456789abcdef";
@@ -56,22 +56,24 @@ static struct lh_table *json_object_table;
static void json_object_init() __attribute__ ((constructor)); static void json_object_init() __attribute__ ((constructor));
static void json_object_init() { static void json_object_init() {
mc_debug("json_object_init: creating object table\n"); MC_DEBUG("json_object_init: creating object table\n");
json_object_table = lh_kptr_table_new(128, "json_object_table", NULL); json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
} }
static void json_object_fini() __attribute__ ((destructor)); static void json_object_fini() __attribute__ ((destructor));
static void json_object_fini() { static void json_object_fini() {
struct lh_entry *ent; struct lh_entry *ent;
if(mc_get_debug() && json_object_table->count) { if(MC_GET_DEBUG()) {
mc_debug("json_object_fini: %d referenced objects at exit\n", if (json_object_table->count) {
json_object_table->count); MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
lh_foreach(json_object_table, ent) { json_object_table->count);
struct json_object* obj = (struct json_object*)ent->v; lh_foreach(json_object_table, ent) {
mc_debug("\t%s:%p\n", json_type_name[obj->o_type], obj); struct json_object* obj = (struct json_object*)ent->v;
MC_DEBUG("\t%s:%p\n", json_type_name[obj->o_type], obj);
}
} }
} }
mc_debug("json_object_fini: freeing object table\n"); MC_DEBUG("json_object_fini: freeing object table\n");
lh_table_free(json_object_table); lh_table_free(json_object_table);
} }
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
@@ -147,7 +149,7 @@ extern void json_object_put(struct json_object *this)
static void json_object_generic_delete(struct json_object* this) static void json_object_generic_delete(struct json_object* this)
{ {
#ifdef REFCOUNT_DEBUG #ifdef REFCOUNT_DEBUG
mc_debug("json_object_delete_%s: %p\n", MC_DEBUG("json_object_delete_%s: %p\n",
json_type_name[this->o_type], this); json_type_name[this->o_type], this);
lh_table_delete(json_object_table, this); lh_table_delete(json_object_table, this);
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
@@ -164,7 +166,7 @@ static struct json_object* json_object_new(enum json_type o_type)
this->_delete = &json_object_generic_delete; this->_delete = &json_object_generic_delete;
#ifdef REFCOUNT_DEBUG #ifdef REFCOUNT_DEBUG
lh_table_insert(json_object_table, this, this); lh_table_insert(json_object_table, this, this);
mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this); MC_DEBUG("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
return this; return this;
} }

View File

@@ -265,7 +265,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case json_tokener_state_comment_eol: case json_tokener_state_comment_eol:
if(c == '\n') { if(c == '\n') {
mc_debug("json_tokener_comment: %s\n", tok->pb->buf); MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
state = json_tokener_state_eatws; state = json_tokener_state_eatws;
} else { } else {
printbuf_memappend(tok->pb, &c, 1); printbuf_memappend(tok->pb, &c, 1);
@@ -275,7 +275,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case json_tokener_state_comment_end: case json_tokener_state_comment_end:
printbuf_memappend(tok->pb, &c, 1); printbuf_memappend(tok->pb, &c, 1);
if(c == '/') { if(c == '/') {
mc_debug("json_tokener_comment: %s\n", tok->pb->buf); MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
state = json_tokener_state_eatws; state = json_tokener_state_eatws;
} else { } else {
state = json_tokener_state_comment; state = json_tokener_state_comment;
@@ -510,7 +510,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
out: out:
if(tok->err == json_tokener_success) return json_object_get(current); if(tok->err == json_tokener_success) return json_object_get(current);
mc_debug("json_tokener_parse_ex: error %s at offset %d\n", MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n",
json_tokener_errors[tok->err], tok->char_offset); json_tokener_errors[tok->err], tok->char_offset);
return NULL; return NULL;
} }

View File

@@ -59,12 +59,12 @@ struct json_object* json_object_from_file(char *filename)
int fd, ret; int fd, ret;
if((fd = open(filename, O_RDONLY)) < 0) { if((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n", MC_ERROR("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno)); filename, strerror(errno));
return error_ptr(-1); return error_ptr(-1);
} }
if(!(pb = printbuf_new())) { if(!(pb = printbuf_new())) {
mc_error("json_object_from_file: printbuf_new failed\n"); MC_ERROR("json_object_from_file: printbuf_new failed\n");
return error_ptr(-1); return error_ptr(-1);
} }
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) { while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
@@ -72,7 +72,7 @@ struct json_object* json_object_from_file(char *filename)
} }
close(fd); close(fd);
if(ret < 0) { if(ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n", MC_ABORT("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno)); filename, strerror(errno));
printbuf_free(pb); printbuf_free(pb);
return error_ptr(-1); return error_ptr(-1);
@@ -89,12 +89,12 @@ int json_object_to_file(char *filename, struct json_object *obj)
unsigned int wpos, wsize; unsigned int wpos, wsize;
if(!obj) { if(!obj) {
mc_error("json_object_to_file: object is null\n"); MC_ERROR("json_object_to_file: object is null\n");
return -1; return -1;
} }
if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n", MC_ERROR("json_object_to_file: error opening file %s: %s\n",
filename, strerror(errno)); filename, strerror(errno));
return -1; return -1;
} }
@@ -107,7 +107,7 @@ int json_object_to_file(char *filename, struct json_object *obj)
while(wpos < wsize) { while(wpos < wsize) {
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) { if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
close(fd); close(fd);
mc_error("json_object_to_file: error writing file %s: %s\n", MC_ERROR("json_object_to_file: error writing file %s: %s\n",
filename, strerror(errno)); filename, strerror(errno));
return -1; return -1;
} }

View File

@@ -46,7 +46,7 @@ int printbuf_memappend(struct printbuf *p, char *buf, int size)
if(p->size - p->bpos <= size) { if(p->size - p->bpos <= size) {
int new_size = max(p->size * 2, p->bpos + size + 8); int new_size = max(p->size * 2, p->bpos + size + 8);
#ifdef PRINTBUF_DEBUG #ifdef PRINTBUF_DEBUG
mc_debug("printbuf_memappend: realloc " MC_DEBUG("printbuf_memappend: realloc "
"bpos=%d wrsize=%d old_size=%d new_size=%d\n", "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
p->bpos, size, p->size, new_size); p->bpos, size, p->size, new_size);
#endif /* PRINTBUF_DEBUG */ #endif /* PRINTBUF_DEBUG */

View File

@@ -11,7 +11,7 @@ int main(int argc, char **argv)
struct json_object *new_obj; struct json_object *new_obj;
int i; int i;
mc_set_debug(1); MC_SET_DEBUG(1);
my_string = json_object_new_string("\t"); my_string = json_object_new_string("\t");
printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string=%s\n", json_object_get_string(my_string));
@@ -51,7 +51,7 @@ int main(int argc, char **argv)
json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("bang"));
json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_add(my_object, "baz", json_object_new_string("fark"));
json_object_object_del(my_object, "baz"); json_object_object_del(my_object, "baz");
json_object_object_add(my_object, "arr", my_array); /*json_object_object_add(my_object, "arr", my_array);*/
printf("my_object=\n"); printf("my_object=\n");
json_object_object_foreach(my_object, key, val) { json_object_object_foreach(my_object, key, val) {
printf("\t%s: %s\n", key, json_object_to_json_string(val)); printf("\t%s: %s\n", key, json_object_to_json_string(val));

View File

@@ -9,7 +9,7 @@ int main(int argc, char **argv)
{ {
struct json_object *new_obj; struct json_object *new_obj;
mc_set_debug(1); MC_SET_DEBUG(1);
new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }"); new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));