jsonrpc: Add logging for messages sent and received, at DBG level.
authorBen Pfaff <blp@nicira.com>
Tue, 17 Nov 2009 00:55:35 +0000 (16:55 -0800)
committerBen Pfaff <blp@nicira.com>
Wed, 18 Nov 2009 00:02:46 +0000 (16:02 -0800)
This made it much easier to see problems while developing some
ovsdb-server features.

lib/dynamic-string.c
lib/dynamic-string.h
lib/jsonrpc.c

index 531e53b..527dee8 100644 (file)
@@ -115,6 +115,13 @@ ds_put_cstr(struct ds *ds, const char *s)
     memcpy(ds_put_uninit(ds, s_len), s, s_len);
 }
 
+void
+ds_put_and_free_cstr(struct ds *ds, char *s)
+{
+    ds_put_cstr(ds, s);
+    free(s);
+}
+
 void
 ds_put_format(struct ds *ds, const char *format, ...)
 {
index 01b93c4..136cedc 100644 (file)
@@ -44,6 +44,7 @@ void ds_put_utf8(struct ds *, int uc);
 void ds_put_char_multiple(struct ds *, char, size_t n);
 void ds_put_buffer(struct ds *, const char *, size_t n);
 void ds_put_cstr(struct ds *, const char *);
+void ds_put_and_free_cstr(struct ds *, char *);
 void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
 void ds_put_format_valist(struct ds *, const char *, va_list)
     PRINTF_FORMAT(2, 0);
index d298f0d..09c3d45 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 
 #include "byteq.h"
+#include "dynamic-string.h"
 #include "json.h"
 #include "list.h"
 #include "ofpbuf.h"
@@ -133,6 +134,37 @@ jsonrpc_get_name(const struct jsonrpc *rpc)
     return rpc->name;
 }
 
+static void
+jsonrpc_log_msg(const struct jsonrpc *rpc, const char *title,
+                const struct jsonrpc_msg *msg)
+{
+    if (VLOG_IS_DBG_ENABLED()) {
+        struct ds s = DS_EMPTY_INITIALIZER;
+        if (msg->method) {
+            ds_put_format(&s, ", method=\"%s\"", msg->method);
+        }
+        if (msg->params) {
+            ds_put_cstr(&s, ", params=");
+            ds_put_and_free_cstr(&s, json_to_string(msg->params, 0));
+        }
+        if (msg->result) {
+            ds_put_cstr(&s, ", result=");
+            ds_put_and_free_cstr(&s, json_to_string(msg->result, 0));
+        }
+        if (msg->error) {
+            ds_put_cstr(&s, ", error=");
+            ds_put_and_free_cstr(&s, json_to_string(msg->error, 0));
+        }
+        if (msg->id) {
+            ds_put_cstr(&s, ", id=");
+            ds_put_and_free_cstr(&s, json_to_string(msg->id, 0));
+        }
+        VLOG_DBG("%s: %s %s%s", rpc->name, title,
+                 jsonrpc_msg_type_to_string(msg->type), ds_cstr(&s));
+        ds_destroy(&s);
+    }
+}
+
 int
 jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
 {
@@ -146,6 +178,8 @@ jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
         return rpc->status;
     }
 
+    jsonrpc_log_msg(rpc, "send", msg);
+
     json = jsonrpc_msg_to_json(msg);
     s = json_to_string(json, 0);
     length = strlen(s);
@@ -313,6 +347,7 @@ jsonrpc_received(struct jsonrpc *rpc)
         return;
     }
 
+    jsonrpc_log_msg(rpc, "received", msg);
     rpc->received = msg;
 }