* Add macros to enable compiling out debug code
authorMichael Clark <michael@metaparadigm.com>
Thu, 25 Oct 2007 02:26:00 +0000 (02:26 +0000)
committerMichael Clark <michael@metaparadigm.com>
Thu, 25 Oct 2007 02:26:00 +0000 (02:26 +0000)
  Geoffrey Young, geoff at modperlcookbook dot org

git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@18 327403b1-1117-474d-bef2-5cb71233fd97

ChangeLog
config.h.in
debug.h
json_object.c
json_tokener.c
json_util.c
printbuf.c
test1.c
test2.c

index 1523c6c..7121199 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+0.8
+  * Add macros to enable compiling out debug code
+    Geoffrey Young, geoff at modperlcookbook dot org
+
 0.7
   * Add escaping of backslash to json output
   * Add escaping of foward slash on tokenizing and output
index 83ab797..44e10aa 100644 (file)
 /* Define to rpl_realloc if the replacement function should be used. */
 #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
diff --git a/debug.h b/debug.h
index 1db69ef..8c183e1 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -21,4 +21,22 @@ extern void mc_debug(const char *msg, ...);
 extern void mc_error(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
index d6c56dc..a629f61 100644 (file)
@@ -27,7 +27,7 @@
   char* strndup(const char* str, size_t n);
 #endif /* !HAVE_STRNDUP */
 
-/* #define REFCOUNT_DEBUG 1 */
+#define REFCOUNT_DEBUG 1
 
 char *json_number_chars = "0123456789.+-e";
 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() {
-  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);
 }
 
 static void json_object_fini() __attribute__ ((destructor));
 static void json_object_fini() {
   struct lh_entry *ent;
-  if(mc_get_debug() && json_object_table->count) {
-    mc_debug("json_object_fini: %d referenced objects at exit\n",
-            json_object_table->count);
-    lh_foreach(json_object_table, ent) {
-      struct json_object* obj = (struct json_object*)ent->v;
-      mc_debug("\t%s:%p\n", json_type_name[obj->o_type], obj);
+  if(MC_GET_DEBUG()) {
+    if (json_object_table->count) {
+      MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
+              json_object_table->count);
+      lh_foreach(json_object_table, ent) {
+        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);
 }
 #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)
 {
 #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);
   lh_table_delete(json_object_table, this);
 #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;
 #ifdef REFCOUNT_DEBUG
   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 */
   return this;
 }
index ada0f9b..ba329fe 100644 (file)
@@ -265,7 +265,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
 
     case json_tokener_state_comment_eol:
       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;
       } else {
        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:
       printbuf_memappend(tok->pb, &c, 1);
       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;
       } else {
        state = json_tokener_state_comment;
@@ -510,7 +510,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
 
  out:
   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);
   return NULL;
 }
index e20be24..903a694 100644 (file)
@@ -59,12 +59,12 @@ struct json_object* json_object_from_file(char *filename)
   int fd, ret;
 
   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));
     return error_ptr(-1);
   }
   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);
   }
   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);
   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));
     printbuf_free(pb);
     return error_ptr(-1);
@@ -89,12 +89,12 @@ int json_object_to_file(char *filename, struct json_object *obj)
   unsigned int wpos, wsize;
 
   if(!obj) {
-    mc_error("json_object_to_file: object is null\n");
+    MC_ERROR("json_object_to_file: object is null\n");
     return -1;
   }
 
   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));
     return -1;
   }
@@ -107,7 +107,7 @@ int json_object_to_file(char *filename, struct json_object *obj)
   while(wpos < wsize) {
     if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
       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));
       return -1;
     }
index 79bdaf5..0ee45e0 100644 (file)
@@ -46,7 +46,7 @@ int printbuf_memappend(struct printbuf *p, char *buf, int size)
   if(p->size - p->bpos <= size) {
     int new_size = max(p->size * 2, p->bpos + size + 8);
 #ifdef PRINTBUF_DEBUG
-    mc_debug("printbuf_memappend: realloc "
+    MC_DEBUG("printbuf_memappend: realloc "
             "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
             p->bpos, size, p->size, new_size);
 #endif /* PRINTBUF_DEBUG */
diff --git a/test1.c b/test1.c
index a1c1c35..f894fac 100644 (file)
--- a/test1.c
+++ b/test1.c
@@ -11,7 +11,7 @@ int main(int argc, char **argv)
   struct json_object *new_obj;
   int i;
 
-  mc_set_debug(1);
+  MC_SET_DEBUG(1);
 
   my_string = json_object_new_string("\t");
   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("fark"));
   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");
   json_object_object_foreach(my_object, key, val) {
     printf("\t%s: %s\n", key, json_object_to_json_string(val));
diff --git a/test2.c b/test2.c
index b7bdf62..afbd386 100644 (file)
--- a/test2.c
+++ b/test2.c
@@ -9,7 +9,7 @@ int main(int argc, char **argv)
 {
   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\"] } ] } } }");
   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));