ovsdb-idl: Make ovsdb_idl_txn_add_comment() take a printf() format string.
authorBen Pfaff <blp@nicira.com>
Mon, 8 Mar 2010 22:18:44 +0000 (14:18 -0800)
committerBen Pfaff <blp@nicira.com>
Mon, 8 Mar 2010 22:18:44 +0000 (14:18 -0800)
All of the callers were calling xasprintf() and then passing the result
to ovsdb_idl_txn_add_comment(), so this slightly simplifies the callers.

lib/ovsdb-idl.c
lib/ovsdb-idl.h
utilities/ovs-vsctl.c
vswitchd/ovs-brcompatd.c

index ba58895..42c53b8 100644 (file)
@@ -914,13 +914,22 @@ ovsdb_idl_txn_create(struct ovsdb_idl *idl)
     return txn;
 }
 
+/* Appends 's', which is treated as a printf()-type format string, to the
+ * comments that will be passed to the OVSDB server when 'txn' is committed.
+ * (The comment will be committed to the OVSDB log, which "ovsdb-tool
+ * show-log" can print in a relatively human-readable form.) */
 void
-ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s)
+ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
 {
+    va_list args;
+
     if (txn->comment.length) {
         ds_put_char(&txn->comment, '\n');
     }
-    ds_put_cstr(&txn->comment, s);
+
+    va_start(args, s);
+    ds_put_format_valist(&txn->comment, s, args);
+    va_end(args);
 }
 
 void
index 4c64585..2aaaa71 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <stdbool.h>
 #include <stdint.h>
+#include "compiler.h"
 
 struct json;
 struct ovsdb_datum;
@@ -58,7 +59,8 @@ enum ovsdb_idl_txn_status {
 const char *ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status);
 
 struct ovsdb_idl_txn *ovsdb_idl_txn_create(struct ovsdb_idl *);
-void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *);
+void ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *, const char *, ...)
+    PRINTF_FORMAT (2, 3);
 void ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *);
 void ovsdb_idl_txn_increment(struct ovsdb_idl_txn *, const char *table,
                              const char *column, const struct json *where);
index 4efb1d0..4ca737e 100644 (file)
@@ -2353,7 +2353,6 @@ do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
     enum ovsdb_idl_txn_status status;
     struct vsctl_command *c;
     int64_t next_cfg = 0;
-    char *comment;
     char *error;
 
     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
@@ -2361,9 +2360,7 @@ do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
         ovsdb_idl_txn_set_dry_run(txn);
     }
 
-    comment = xasprintf("ovs-vsctl: %s", args);
-    ovsdb_idl_txn_add_comment(txn, comment);
-    free(comment);
+    ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
 
     ovs = ovsrec_open_vswitch_first(idl);
     if (!ovs) {
index 6b65947..b364b0b 100644 (file)
@@ -463,7 +463,6 @@ add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
     struct ovsrec_port *port;
     struct ovsrec_interface *iface;
     struct ovsdb_idl_txn *txn;
-    char *comment;
 
     if (find_bridge(ovs, br_name)) {
         VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
@@ -492,9 +491,7 @@ add_bridge(struct ovsdb_idl *idl, const struct ovsrec_open_vswitch *ovs,
 
     txn = ovsdb_idl_txn_create(idl);
 
-    comment = xasprintf("ovs-brcompatd: addbr %s", br_name);
-    ovsdb_idl_txn_add_comment(txn, comment);
-    free(comment);
+    ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
 
     iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
     ovsrec_interface_set_name(iface, br_name);
@@ -582,7 +579,6 @@ del_bridge(struct ovsdb_idl *idl,
     struct ovsrec_bridge *br = find_bridge(ovs, br_name);
     struct ovsrec_bridge **bridges;
     struct ovsdb_idl_txn *txn;
-    char *comment;
     size_t i, n;
 
     if (!br) {
@@ -592,9 +588,7 @@ del_bridge(struct ovsdb_idl *idl,
 
     txn = ovsdb_idl_txn_create(idl);
 
-    comment = xasprintf("ovs-brcompatd: delbr %s", br_name);
-    ovsdb_idl_txn_add_comment(txn, comment);
-    free(comment);
+    ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: delbr %s", br_name);
 
     del_port(br, br_name);
 
@@ -738,19 +732,17 @@ handle_port_cmd(struct ovsdb_idl *idl,
         } else {
             do {
                 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
-                char *comment;
 
                 if (add) {
-                    comment = xasprintf("ovs-brcompatd: add-if %s", port_name);
+                    ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: add-if %s",
+                                              port_name);
                     add_port(ovs, br, port_name);
                 } else {
-                    comment = xasprintf("ovs-brcompatd: del-if %s", port_name);
+                    ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: del-if %s",
+                                              port_name);
                     del_port(br, port_name);
                 }
 
-                ovsdb_idl_txn_add_comment(txn, comment);
-                free(comment);
-
                 error = commit_txn(txn, true);
                 VLOG_INFO_RL(&rl, "%s %s %s: %s",
                              cmd_name, br_name, port_name, strerror(error));
@@ -1200,7 +1192,6 @@ rtnl_recv_update(struct ovsdb_idl *idl,
                 /* Network device is really gone. */
                 struct ovsdb_idl_txn *txn;
                 struct ovsrec_bridge *br;
-                char *comment;
 
                 VLOG_INFO("network device %s destroyed, "
                           "removing from bridge %s", port_name, br_name);
@@ -1214,11 +1205,9 @@ rtnl_recv_update(struct ovsdb_idl *idl,
                 }
 
                 txn = ovsdb_idl_txn_create(idl);
-
-                comment = xasprintf("ovs-brcompatd: destroy port %s",
-                        port_name);
-                ovsdb_idl_txn_add_comment(txn, comment);
-                free(comment);
+                ovsdb_idl_txn_add_comment(txn,
+                                          "ovs-brcompatd: destroy port %s",
+                                          port_name);
 
                 del_port(br, port_name);
                 commit_txn(txn, false);