Signed-off-by: K Shiva <shiva_kr(a)riseup.net>
---
tools/virsh-network.c | 78 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 77 insertions(+), 1 deletion(-)
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 42b7dba761..74712e29be 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1206,7 +1206,8 @@ typedef struct virshNetEventData virshNetEventData;
VIR_ENUM_DECL(virshNetworkEventId);
VIR_ENUM_IMPL(virshNetworkEventId,
VIR_NETWORK_EVENT_ID_LAST,
- "lifecycle");
+ "lifecycle",
+ "metadata-change");
static void
vshEventLifecyclePrint(virConnectPtr conn G_GNUC_UNUSED,
@@ -1239,9 +1240,84 @@ vshEventLifecyclePrint(virConnectPtr conn G_GNUC_UNUSED,
vshEventDone(data->ctl);
}
+static void G_GNUC_PRINTF(2, 3)
+virshEventPrintf(virshNetEventData *data,
+ const char *fmt,
+ ...)
+{
+ va_list ap;
+
+ if (!data->loop && data->count)
+ return;
+
+ if (data->timestamp) {
+ char timestamp[VIR_TIME_STRING_BUFLEN] = "";
+
+ ignore_value(virTimeStringNowRaw(timestamp));
+ vshPrint(data->ctl, "%s: ", timestamp);
+ }
+
+ va_start(ap, fmt);
+ vshPrintVa(data->ctl, fmt, ap);
+ va_end(ap);
+
+ (data->count)++;
+ if (!data->loop)
+ vshEventDone(data->ctl);
+}
+
+/**
+ * virshEventPrint:
+ *
+ * @data: opaque data passed to all event callbacks
+ * @buf: string buffer describing the event
+ *
+ * Print the event description found in @buf and update virshNetEventData.
+ *
+ * This function resets @buf and frees all memory consumed by its content.
+ */
+static void
+virshEventPrint(virshNetEventData *data,
+ virBuffer *buf)
+{
+ g_autofree char *msg = NULL;
+
+ if (!(msg = virBufferContentAndReset(buf)))
+ return;
+
+ virshEventPrintf(data, "%s", msg);
+}
+
+#define UNKNOWNSTR(str) (str ? str : N_("unsupported value"))
+
+VIR_ENUM_DECL(virshNetworkEventMetadataChangeType);
+VIR_ENUM_IMPL(virshNetworkEventMetadataChangeType,
+ VIR_NETWORK_METADATA_LAST,
+ N_("description"),
+ N_("title"),
+ N_("element"));
+
+static void
+vshEventMetadataChangePrint(virConnectPtr conn G_GNUC_UNUSED,
+ virNetworkPtr net,
+ int type,
+ const char *nsuri,
+ void *opaque)
+{
+ g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+
+ virBufferAsprintf(&buf, _("event 'metadata-change' for network
'%1$s': type %2$s, uri %3$s\n"),
+ virNetworkGetName(net),
+ UNKNOWNSTR(virshNetworkEventMetadataChangeTypeTypeToString(type)),
+ NULLSTR(nsuri));
+ virshEventPrint(opaque, &buf);
+}
+
virshNetworkEventCallback virshNetworkEventCallbacks[] = {
{ "lifecycle",
VIR_NETWORK_EVENT_CALLBACK(vshEventLifecyclePrint), },
+ { "metadata-change",
+ VIR_NETWORK_EVENT_CALLBACK(vshEventMetadataChangePrint), },
};
G_STATIC_ASSERT(VIR_NETWORK_EVENT_ID_LAST == G_N_ELEMENTS(virshNetworkEventCallbacks));
--
2.41.0