mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-05 16:33:50 +00:00
virsh: Refactor event printing
To reduce code duplication. Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
parent
4e1f37f812
commit
7cbb7f059a
@ -9162,9 +9162,13 @@ struct virshQemuEventData {
|
|||||||
typedef struct virshQemuEventData virshQemuEventData;
|
typedef struct virshQemuEventData virshQemuEventData;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
virshEventPrint(virConnectPtr conn ATTRIBUTE_UNUSED, virDomainPtr dom,
|
virshEventQemuPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
||||||
const char *event, long long seconds, unsigned int micros,
|
virDomainPtr dom,
|
||||||
const char *details, void *opaque)
|
const char *event,
|
||||||
|
long long seconds,
|
||||||
|
unsigned int micros,
|
||||||
|
const char *details,
|
||||||
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshQemuEventData *data = opaque;
|
virshQemuEventData *data = opaque;
|
||||||
virJSONValuePtr pretty = NULL;
|
virJSONValuePtr pretty = NULL;
|
||||||
@ -9261,7 +9265,7 @@ cmdQemuMonitorEvent(vshControl *ctl, const vshCmd *cmd)
|
|||||||
|
|
||||||
if ((eventId = virConnectDomainQemuMonitorEventRegister(priv->conn, dom,
|
if ((eventId = virConnectDomainQemuMonitorEventRegister(priv->conn, dom,
|
||||||
event,
|
event,
|
||||||
virshEventPrint,
|
virshEventQemuPrint,
|
||||||
&data, NULL,
|
&data, NULL,
|
||||||
flags)) < 0)
|
flags)) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -12138,20 +12142,49 @@ struct virshDomEventData {
|
|||||||
};
|
};
|
||||||
typedef struct virshDomEventData virshDomEventData;
|
typedef struct virshDomEventData virshDomEventData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virshEventPrint:
|
||||||
|
*
|
||||||
|
* @data: opaque data passed to all event callbacks
|
||||||
|
* @buf: string buffer describing the event
|
||||||
|
*
|
||||||
|
* Print the event description found in @buf and update virshDomEventData.
|
||||||
|
*
|
||||||
|
* This function resets @buf and frees all memory consumed by its content.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
virshEventPrint(virshDomEventData *data,
|
||||||
|
virBufferPtr buf)
|
||||||
|
{
|
||||||
|
char *msg;
|
||||||
|
|
||||||
|
if (!(msg = virBufferContentAndReset(buf)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!data->loop && *data->count)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
vshPrint(data->ctl, "%s", msg);
|
||||||
|
|
||||||
|
(*data->count)++;
|
||||||
|
if (!data->loop)
|
||||||
|
vshEventDone(data->ctl);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(msg);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
virshEventGenericPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
virshEventGenericPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
||||||
virDomainPtr dom,
|
virDomainPtr dom,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event '%s' for domain %s\n"),
|
||||||
return;
|
((virshDomEventData *) opaque)->cb->name,
|
||||||
vshPrint(data->ctl, _("event '%s' for domain %s\n"),
|
virDomainGetName(dom));
|
||||||
data->cb->name, virDomainGetName(dom));
|
virshEventPrint(opaque, &buf);
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12161,16 +12194,13 @@ virshEventLifecyclePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int detail,
|
int detail,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'lifecycle' for domain %s: %s %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl, _("event 'lifecycle' for domain %s: %s %s\n"),
|
virshDomainEventToString(event),
|
||||||
virDomainGetName(dom), virshDomainEventToString(event),
|
virshDomainEventDetailToString(event, detail));
|
||||||
virshDomainEventDetailToString(event, detail));
|
virshEventPrint(opaque, &buf);
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12179,15 +12209,12 @@ virshEventRTCChangePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
long long utcoffset,
|
long long utcoffset,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'rtc-change' for domain %s: %lld\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl, _("event 'rtc-change' for domain %s: %lld\n"),
|
utcoffset);
|
||||||
virDomainGetName(dom), utcoffset);
|
virshEventPrint(opaque, &buf);
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12196,15 +12223,12 @@ virshEventWatchdogPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int action,
|
int action,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'watchdog' for domain %s: %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl, _("event 'watchdog' for domain %s: %s\n"),
|
virshDomainEventWatchdogToString(action));
|
||||||
virDomainGetName(dom), virshDomainEventWatchdogToString(action));
|
virshEventPrint(opaque, &buf);
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12215,16 +12239,14 @@ virshEventIOErrorPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int action,
|
int action,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'io-error' for domain %s: %s (%s) %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl, _("event 'io-error' for domain %s: %s (%s) %s\n"),
|
srcPath,
|
||||||
virDomainGetName(dom), srcPath, devAlias,
|
devAlias,
|
||||||
virshDomainEventIOErrorToString(action));
|
virshDomainEventIOErrorToString(action));
|
||||||
(*data->count)++;
|
virshEventPrint(opaque, &buf);
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12237,26 +12259,26 @@ virshEventGraphicsPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
const virDomainEventGraphicsSubject *subject,
|
const virDomainEventGraphicsSubject *subject,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'graphics' for domain %s: "
|
||||||
return;
|
"%s local[%s %s %s] remote[%s %s %s] %s\n"),
|
||||||
vshPrint(data->ctl, _("event 'graphics' for domain %s: "
|
virDomainGetName(dom),
|
||||||
"%s local[%s %s %s] remote[%s %s %s] %s"),
|
virshGraphicsPhaseToString(phase),
|
||||||
virDomainGetName(dom), virshGraphicsPhaseToString(phase),
|
virshGraphicsAddressToString(local->family),
|
||||||
virshGraphicsAddressToString(local->family),
|
local->node,
|
||||||
local->node, local->service,
|
local->service,
|
||||||
virshGraphicsAddressToString(remote->family),
|
virshGraphicsAddressToString(remote->family),
|
||||||
remote->node, remote->service,
|
remote->node,
|
||||||
authScheme);
|
remote->service,
|
||||||
for (i = 0; i < subject->nidentity; i++)
|
authScheme);
|
||||||
vshPrint(data->ctl, " %s=%s", subject->identities[i].type,
|
for (i = 0; i < subject->nidentity; i++) {
|
||||||
subject->identities[i].name);
|
virBufferAsprintf(&buf, "\t%s=%s\n",
|
||||||
vshPrint(data->ctl, "\n");
|
subject->identities[i].type,
|
||||||
(*data->count)++;
|
subject->identities[i].name);
|
||||||
if (!data->loop)
|
}
|
||||||
vshEventDone(data->ctl);
|
virshEventPrint(opaque, &buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12268,17 +12290,16 @@ virshEventIOErrorReasonPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
const char *reason,
|
const char *reason,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'io-error-reason' for domain %s: "
|
||||||
return;
|
"%s (%s) %s due to %s\n"),
|
||||||
vshPrint(data->ctl, _("event 'io-error-reason' for domain %s: "
|
virDomainGetName(dom),
|
||||||
"%s (%s) %s due to %s\n"),
|
srcPath,
|
||||||
virDomainGetName(dom), srcPath, devAlias,
|
devAlias,
|
||||||
virshDomainEventIOErrorToString(action), reason);
|
virshDomainEventIOErrorToString(action),
|
||||||
(*data->count)++;
|
reason);
|
||||||
if (!data->loop)
|
virshEventPrint(opaque, &buf);
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12289,17 +12310,15 @@ virshEventBlockJobPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int status,
|
int status,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event '%s' for domain %s: %s for %s %s\n"),
|
||||||
return;
|
((virshDomEventData *) opaque)->cb->name,
|
||||||
vshPrint(data->ctl, _("event '%s' for domain %s: %s for %s %s\n"),
|
virDomainGetName(dom),
|
||||||
data->cb->name, virDomainGetName(dom),
|
virshDomainBlockJobToString(type),
|
||||||
virshDomainBlockJobToString(type),
|
disk,
|
||||||
disk, virshDomainBlockJobStatusToString(status));
|
virshDomainBlockJobStatusToString(status));
|
||||||
(*data->count)++;
|
virshEventPrint(opaque, &buf);
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12311,17 +12330,16 @@ virshEventDiskChangePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int reason,
|
int reason,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'disk-change' for domain %s disk %s: "
|
||||||
return;
|
"%s -> %s: %s\n"),
|
||||||
vshPrint(data->ctl,
|
virDomainGetName(dom),
|
||||||
_("event 'disk-change' for domain %s disk %s: %s -> %s: %s\n"),
|
alias,
|
||||||
virDomainGetName(dom), alias, NULLSTR(oldSrc), NULLSTR(newSrc),
|
NULLSTR(oldSrc),
|
||||||
virshDomainEventDiskChangeToString(reason));
|
NULLSTR(newSrc),
|
||||||
(*data->count)++;
|
virshDomainEventDiskChangeToString(reason));
|
||||||
if (!data->loop)
|
virshEventPrint(opaque, &buf);
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12331,17 +12349,13 @@ virshEventTrayChangePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int reason,
|
int reason,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'tray-change' for domain %s disk %s: %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl,
|
alias,
|
||||||
_("event 'tray-change' for domain %s disk %s: %s\n"),
|
virshDomainEventTrayChangeToString(reason));
|
||||||
virDomainGetName(dom), alias,
|
virshEventPrint(opaque, &buf);
|
||||||
virshDomainEventTrayChangeToString(reason));
|
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12361,16 +12375,12 @@ virshEventBalloonChangePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
unsigned long long actual,
|
unsigned long long actual,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'balloon-change' for domain %s: %lluKiB\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl,
|
actual);
|
||||||
_("event 'balloon-change' for domain %s: %lluKiB\n"),
|
virshEventPrint(opaque, &buf);
|
||||||
virDomainGetName(dom), actual);
|
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12379,16 +12389,12 @@ virshEventDeviceRemovedPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
const char *alias,
|
const char *alias,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'device-removed' for domain %s: %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl,
|
alias);
|
||||||
_("event 'device-removed' for domain %s: %s\n"),
|
virshEventPrint(opaque, &buf);
|
||||||
virDomainGetName(dom), alias);
|
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12397,16 +12403,12 @@ virshEventDeviceAddedPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
const char *alias,
|
const char *alias,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'device-added' for domain %s: %s\n"),
|
||||||
return;
|
virDomainGetName(dom),
|
||||||
vshPrint(data->ctl,
|
alias);
|
||||||
_("event 'device-added' for domain %s: %s\n"),
|
virshEventPrint(opaque, &buf);
|
||||||
virDomainGetName(dom), alias);
|
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -12416,28 +12418,20 @@ virshEventTunablePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int nparams,
|
int nparams,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
size_t i;
|
size_t i;
|
||||||
char *value = NULL;
|
char *value;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
|
||||||
return;
|
|
||||||
|
|
||||||
vshPrint(data->ctl,
|
|
||||||
_("event 'tunable' for domain %s:\n"),
|
|
||||||
virDomainGetName(dom));
|
|
||||||
|
|
||||||
|
virBufferAsprintf(&buf, _("event 'tunable' for domain %s:\n"),
|
||||||
|
virDomainGetName(dom));
|
||||||
for (i = 0; i < nparams; i++) {
|
for (i = 0; i < nparams; i++) {
|
||||||
value = virTypedParameterToString(¶ms[i]);
|
value = virTypedParameterToString(¶ms[i]);
|
||||||
if (value) {
|
if (value) {
|
||||||
vshPrint(data->ctl, _("\t%s: %s\n"), params[i].field, value);
|
virBufferAsprintf(&buf, "\t%s: %s\n", params[i].field, value);
|
||||||
VIR_FREE(value);
|
VIR_FREE(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
virshEventPrint(opaque, &buf);
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_ENUM_DECL(virshEventAgentLifecycleState)
|
VIR_ENUM_DECL(virshEventAgentLifecycleState)
|
||||||
@ -12462,19 +12456,14 @@ virshEventAgentLifecyclePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
int reason,
|
int reason,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
virshDomEventData *data = opaque;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!data->loop && *data->count)
|
virBufferAsprintf(&buf, _("event 'agent-lifecycle' for domain %s: state: "
|
||||||
return;
|
"'%s' reason: '%s'\n"),
|
||||||
vshPrint(data->ctl,
|
virDomainGetName(dom),
|
||||||
_("event 'agent-lifecycle' for domain %s: state: '%s' reason: '%s'\n"),
|
UNKNOWNSTR(virshEventAgentLifecycleStateTypeToString(state)),
|
||||||
virDomainGetName(dom),
|
UNKNOWNSTR(virshEventAgentLifecycleReasonTypeToString(reason)));
|
||||||
UNKNOWNSTR(virshEventAgentLifecycleStateTypeToString(state)),
|
virshEventPrint(opaque, &buf);
|
||||||
UNKNOWNSTR(virshEventAgentLifecycleReasonTypeToString(reason)));
|
|
||||||
|
|
||||||
(*data->count)++;
|
|
||||||
if (!data->loop)
|
|
||||||
vshEventDone(data->ctl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static vshEventCallback vshEventCallbacks[] = {
|
static vshEventCallback vshEventCallbacks[] = {
|
||||||
|
Loading…
Reference in New Issue
Block a user