[libvirt] [dbus PATCH v2 4/5] events: Introduce virtDBusEventsDomainEventToString function

Pavel Hrdina phrdina at redhat.com
Sat Mar 31 14:26:45 UTC 2018


On Thu, Mar 29, 2018 at 05:41:20PM +0200, Katerina Koukiou wrote:
> This function replaces the manual translation of Event ENUMS.
> 
> Note: VIR_ENUM_SENTINELS is needed for VIR_DOMAIN_EVENT_LAST.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou at redhat.com>
> ---
>  src/events.c | 55 +++++++++++++++++++++----------------------------------
>  src/events.h |  3 +++
>  src/util.h   |  1 +
>  3 files changed, 25 insertions(+), 34 deletions(-)
> 
> diff --git a/src/events.c b/src/events.c
> index dada55f..0d6d5fc 100644
> --- a/src/events.c
> +++ b/src/events.c
> @@ -4,6 +4,26 @@
>  
>  #include <libvirt/libvirt.h>
>  
> +VIRT_DBUS_ENUM_DECL(virtDBusEventsDomainEvent)
> +VIRT_DBUS_ENUM_IMPL(virtDBusEventsDomainEvent,
> +                    VIR_DOMAIN_EVENT_LAST,
> +                    "Defined",
> +                    "Undefined",
> +                    "Started",
> +                    "Suspended",
> +                    "Resumed",
> +                    "Stopped",
> +                    "Shutdown",
> +                    "PMSuspended",
> +                    "Crashed")

For this patch it should be with the "Domain" prefix, the next patch
should rename it.

> +
> +const gchar *
> +virtDBusEventsDomainEventToString(gint event)
> +{
> +    const gchar *str = virtDBusEventsDomainEventTypeToString(event);
> +    return str ? str : "unknown";
> +}
> +
>  static gint
>  virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                virDomainPtr domain,
> @@ -12,42 +32,9 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                gpointer opaque)
>  {
>      virtDBusConnect *connect = opaque;
> -    const gchar *signal = NULL;
>      const gchar *name;
>      g_autofree gchar *path = NULL;
>  
> -    switch (event) {
> -    case VIR_DOMAIN_EVENT_DEFINED:
> -        signal = "DomainDefined";
> -        break;
> -    case VIR_DOMAIN_EVENT_UNDEFINED:
> -        signal = "DomainUndefined";
> -        break;
> -    case VIR_DOMAIN_EVENT_STARTED:
> -        signal = "DomainStarted";
> -        break;
> -    case VIR_DOMAIN_EVENT_SUSPENDED:
> -        signal = "DomainSuspended";
> -        break;
> -    case VIR_DOMAIN_EVENT_RESUMED:
> -        signal = "DomainResumed";
> -        break;
> -    case VIR_DOMAIN_EVENT_STOPPED:
> -        signal = "DomainStopped";
> -        break;
> -    case VIR_DOMAIN_EVENT_SHUTDOWN:
> -        signal = "DomainShutdown";
> -        break;
> -    case VIR_DOMAIN_EVENT_PMSUSPENDED:
> -        signal = "DomainPMSuspended";
> -        break;
> -    case VIR_DOMAIN_EVENT_CRASHED:
> -        signal = "DomainCrashed";
> -        break;
> -    default:
> -        return 0;
> -    }
> -
>      name = virDomainGetName(domain);
>      path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath);
>  
> @@ -55,7 +42,7 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                    NULL,
>                                    connect->connectPath,
>                                    VIRT_DBUS_CONNECT_INTERFACE,
> -                                  signal,
> +                                  virtDBusEventsDomainEventToString(event),
>                                    g_variant_new("(so)", name, path),
>                                    NULL);
>  
> diff --git a/src/events.h b/src/events.h
> index c234c55..7eb5daa 100644
> --- a/src/events.h
> +++ b/src/events.h
> @@ -4,3 +4,6 @@
>  
>  void
>  virtDBusEventsRegister(virtDBusConnect *connect);
> +
> +const gchar *
> +virtDBusEventsDomainEventToString(gint event) G_GNUC_PURE;

There is no need to export this function in header file, it's used only
in events.c as a static function.

> diff --git a/src/util.h b/src/util.h
> index 61e01c9..203cd62 100644
> --- a/src/util.h
> +++ b/src/util.h
> @@ -2,6 +2,7 @@
>  
>  #include "gdbus.h"
>  
> +#define VIR_ENUM_SENTINELS

Unrelated change and not needed, you can remove it.

Pavel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180331/1955e9b2/attachment-0001.sig>


More information about the libvir-list mailing list