[Spice-devel] [spice-gtk v1 1/3] Improve debug log for preferred compression message

Frediano Ziglio fziglio at redhat.com
Wed Dec 20 13:44:14 UTC 2017


> 
> From: Victor Toso <me at victortoso.com>
> 
> To use a string instead of number (enum)
> 
> Signed-off-by: Victor Toso <victortoso at redhat.com>
> ---
>  src/channel-display.c | 26 +++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/src/channel-display.c b/src/channel-display.c
> index 75d2e32..dece3b9 100644
> --- a/src/channel-display.c
> +++ b/src/channel-display.c
> @@ -536,6 +536,29 @@ void
> spice_display_change_preferred_compression(SpiceChannel *channel, gint comp
>      spice_display_channel_change_preferred_compression(channel,
>      compression);
>  }
>  
> +static const gchar *image_compression_types_str[] = {

+const

> +    [ SPICE_IMAGE_COMPRESSION_INVALID ] = "invalid",
> +    [ SPICE_IMAGE_COMPRESSION_OFF ] = "off",
> +    [ SPICE_IMAGE_COMPRESSION_AUTO_GLZ ] = "auto-glz",
> +    [ SPICE_IMAGE_COMPRESSION_AUTO_LZ ] = "auto-lz",
> +    [ SPICE_IMAGE_COMPRESSION_QUIC ] = "quic",
> +    [ SPICE_IMAGE_COMPRESSION_GLZ ] = "glz",
> +    [ SPICE_IMAGE_COMPRESSION_LZ ] = "lz",
> +    [ SPICE_IMAGE_COMPRESSION_LZ4 ] = "lz4",
> +};
> +G_STATIC_ASSERT(G_N_ELEMENTS(image_compression_types_str) <=
> SPICE_IMAGE_COMPRESSION_ENUM_END);
> +
> +static const gchar *preferred_compression_type_to_string(gint type)
> +{
> +    const char *str = NULL;
> +
> +    if (type >= 0 && type < G_N_ELEMENTS(image_compression_types_str)) {
> +        str = image_compression_types_str[type];
> +    }
> +
> +    return str ? str : "unknown";
> +}
> +
>  /**
>   * spice_display_channel_change_preferred_compression:
>   * @channel: a #SpiceDisplayChannel
> @@ -560,7 +583,8 @@ void
> spice_display_channel_change_preferred_compression(SpiceChannel *channel, g
>          return;
>      }
>  
> -    CHANNEL_DEBUG(channel, "changing preferred compression to %d",
> compression);
> +    CHANNEL_DEBUG(channel, "changing preferred compression to %s",
> +                  preferred_compression_type_to_string(compression));
>  
>      pref_comp_msg.image_compression = compression;
>      out = spice_msg_out_new(channel,
>      SPICE_MSGC_DISPLAY_PREFERRED_COMPRESSION);

There's a similar array in spice-session.c (_spice_image_compress_values) and
similar values in spice-option.c (parse_preferred_compression).

To avoid such duplications I usually use a macro like

#define SPICE_ALL_COMPRESSIONS \
    SPICE_COMPRESSION(SPICE_IMAGE_COMPRESSION_OFF, "off) \
    SPICE_COMPRESSION(SPICE_IMAGE_COMPRESSION_INVALID, "invalid") \
...

and use like

static const GEnumValue _spice_image_compress_values[] = {
#define SPICE_COMPRESSION(name, str) { name, #name, str },
    SPICE_ALL_COMPRESSIONS
#undef SPICE_COMPRESSION
  { 0, NULL, NULL }
};

or 

static const gchar *const image_compression_types_str[] = {
#define SPICE_COMPRESSION(name, str) [ name ] = str,
    SPICE_ALL_COMPRESSIONS
#undef SPICE_COMPRESSION
};

In this case you could make _spice_image_compress_values public and
use it. Yes, maybe you don't want to slow down the debug code (surely
not a problem for option parsing).

Frediano


More information about the Spice-devel mailing list