[Spice-commits] 2 commits - server/red-channel-client.c server/red-channel.c server/red-channel.h server/red-qxl.c server/reds.c server/spicevmc.c server/utils.c server/utils.h

Frediano Ziglio fziglio at kemper.freedesktop.org
Mon Oct 23 19:31:42 UTC 2017


 server/red-channel-client.c |   34 ++++++++--------------------
 server/red-channel.c        |   19 +++++++++------
 server/red-channel.h        |   23 +++++++++++++++++++
 server/red-qxl.c            |   10 +++-----
 server/reds.c               |   33 +++++++++------------------
 server/spicevmc.c           |    6 +---
 server/utils.c              |   53 ++++++++++++++++++++++++++++++++++++++++++++
 server/utils.h              |    3 ++
 8 files changed, 118 insertions(+), 63 deletions(-)

New commits:
commit bf61342e9b4b26f64c80964e359b8dabe97a62ea
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Mon Oct 23 18:31:10 2017 +0200

    channel: Introduce logging helpers
    
    This commit adds red_channel_{debug,warning,printerr}() helpers which
    will prepend the log message with "channel-name:id (%p)". It also
    changes various locations which were doing this manually.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/red-channel-client.c b/server/red-channel-client.c
index 54a6223b..821dd351 100644
--- a/server/red-channel-client.c
+++ b/server/red-channel-client.c
@@ -181,10 +181,7 @@ static bool red_channel_client_config_socket(RedChannelClient *rcc);
 #define spice_channel_client_error(rcc, format, ...)                                     \
     do {                                                                                 \
         RedChannel *_ch = red_channel_client_get_channel(rcc);                           \
-        uint32_t _type, _id;                                                             \
-        g_object_get(_ch, "channel-type", &_type, "id", &_id, NULL);                     \
-        spice_warning("rcc %p type %u id %u: " format, rcc,                              \
-                    type, id, ## __VA_ARGS__);                                           \
+        red_channel_warning(_ch, format, ## __VA_ARGS__);                                \
         red_channel_client_shutdown(rcc);                                                \
     } while (0)
 
@@ -778,14 +775,10 @@ static void red_channel_client_connectivity_timer(void *opaque)
         core->timer_start(core, rcc->priv->connectivity_monitor.timer,
                           rcc->priv->connectivity_monitor.timeout);
     } else {
-        uint32_t type, id;
-        g_object_get(rcc->priv->channel,
-                     "channel-type", &type,
-                     "id", &id,
-                     NULL);
         monitor->state = CONNECTIVITY_STATE_DISCONNECTED;
-        spice_warning("rcc %p on channel %d:%d has been unresponsive for more than %u ms, disconnecting",
-                      rcc, type, id, monitor->timeout);
+        red_channel_warning(rcc->priv->channel,
+                            "rcc %p has been unresponsive for more than %u ms, disconnecting",
+                            rcc, monitor->timeout);
         red_channel_client_disconnect(rcc);
     }
 }
@@ -1419,11 +1412,9 @@ static void red_channel_client_handle_migrate_data(RedChannelClient *rcc,
 {
     RedChannel *channel = red_channel_client_get_channel(rcc);
     RedChannelClass *klass = RED_CHANNEL_GET_CLASS(channel);
-    uint32_t type, id;
 
-    g_object_get(channel, "channel-type", &type, "id", &id, NULL);
-    spice_debug("channel type %d id %d rcc %p size %u",
-                type, id, rcc, size);
+    red_channel_debug(channel, "rcc %p size %u", rcc, size);
+
     if (!klass->handle_migrate_data) {
         return;
     }
@@ -1709,14 +1700,11 @@ void red_channel_client_disconnect(RedChannelClient *rcc)
 {
     RedChannel *channel = rcc->priv->channel;
     SpiceCoreInterfaceInternal *core = red_channel_get_core_interface(channel);
-    uint32_t type, id;
 
     if (!red_channel_client_is_connected(rcc)) {
         return;
     }
-    g_object_get(channel, "channel-type", &type, "id", &id, NULL);
-    spice_printerr("rcc=%p (channel=%p type=%d id=%d)", rcc, channel,
-                   type, id);
+    red_channel_printerr(channel, "rcc=%p", rcc);
     red_channel_client_pipe_clear(rcc);
     if (rcc->priv->stream->watch) {
         core->watch_remove(core, rcc->priv->stream->watch);
@@ -1881,19 +1869,17 @@ void red_channel_client_pipe_remove_and_release_pos(RedChannelClient *rcc,
 gboolean red_channel_client_set_migration_seamless(RedChannelClient *rcc)
 {
     gboolean ret = FALSE;
-    uint32_t type, id, flags;
+    uint32_t flags;
 
     g_object_get(rcc->priv->channel,
-                 "channel-type", &type,
-                 "id", &id,
                  "migration-flags", &flags,
                  NULL);
     if (flags & SPICE_MIGRATE_NEED_DATA_TRANSFER) {
         rcc->priv->wait_migrate_data = TRUE;
         ret = TRUE;
     }
-    spice_debug("channel type %d id %d rcc %p wait data %d", type, id, rcc,
-                rcc->priv->wait_migrate_data);
+    red_channel_debug(rcc->priv->channel, "rcc %p wait data %d", rcc,
+                      rcc->priv->wait_migrate_data);
 
     return ret;
 }
diff --git a/server/red-channel.c b/server/red-channel.c
index b3e98abc..ad45fb52 100644
--- a/server/red-channel.c
+++ b/server/red-channel.c
@@ -191,8 +191,8 @@ static void
 red_channel_constructed(GObject *object)
 {
     RedChannel *self = RED_CHANNEL(object);
-    spice_debug("%p: channel type %d id %d thread_id 0x%lx", self,
-                self->priv->type, self->priv->id, self->priv->thread_id);
+
+    red_channel_debug(self, "thread_id 0x%lx", self->priv->thread_id);
 
     RedChannelClass *klass = RED_CHANNEL_GET_CLASS(self);
 
@@ -463,12 +463,10 @@ void red_channel_remove_client(RedChannel *channel, RedChannelClient *rcc)
     g_return_if_fail(channel == red_channel_client_get_channel(rcc));
 
     if (!pthread_equal(pthread_self(), channel->priv->thread_id)) {
-        spice_warning("channel type %d id %d - "
-                      "channel->thread_id (0x%lx) != pthread_self (0x%lx)."
-                      "If one of the threads is != io-thread && != vcpu-thread, "
-                      "this might be a BUG",
-                      channel->priv->type, channel->priv->id,
-                      channel->priv->thread_id, pthread_self());
+        red_channel_warning(channel, "channel->thread_id (0x%lx) != pthread_self (0x%lx)."
+                            "If one of the threads is != io-thread && != vcpu-thread, "
+                            "this might be a BUG",
+                            channel->priv->thread_id, pthread_self());
     }
     spice_return_if_fail(channel);
     link = g_list_find(channel->priv->clients, rcc);
diff --git a/server/red-channel.h b/server/red-channel.h
index 70282197..9e64e8b1 100644
--- a/server/red-channel.h
+++ b/server/red-channel.h
@@ -236,6 +236,27 @@ void red_channel_disconnect_client(RedChannel *channel, RedChannelClient *rcc);
 
 #define CHANNEL_BLOCKED_SLEEP_DURATION 10000 //micro
 
+#define red_channel_log_generic(log_cb, channel, format, ...)                            \
+    do {                                                                                 \
+        uint32_t id_;                                                                    \
+        RedChannel *channel_ = (channel);                                                \
+        g_object_get(channel_, "id", &id_, NULL);                                        \
+        log_cb("%s:%u (%p): " format, red_channel_get_name(channel_),                    \
+                        id_, channel_, ## __VA_ARGS__);                                  \
+    } while (0)
+
+#define red_channel_printerr(channel, format, ...)                                       \
+        red_channel_log_generic(spice_printerr, channel, format, ## __VA_ARGS__);
+
+#define red_channel_warning(channel, format, ...)                                        \
+        red_channel_log_generic(g_warning, channel, format, ## __VA_ARGS__);
+
+#define red_channel_message(channel, format, ...)                                        \
+        red_channel_log_generic(g_message, channel, format, ## __VA_ARGS__);
+
+#define red_channel_debug(channel, format, ...)                                          \
+        red_channel_log_generic(g_debug, channel, format, ## __VA_ARGS__);
+
 G_END_DECLS
 
 #endif /* RED_CHANNEL_H_ */
diff --git a/server/red-qxl.c b/server/red-qxl.c
index ad33b1b4..730b9f3d 100644
--- a/server/red-qxl.c
+++ b/server/red-qxl.c
@@ -114,11 +114,10 @@ static void red_qxl_display_migrate(RedChannelClient *rcc)
     RedWorkerMessageDisplayMigrate payload;
     Dispatcher *dispatcher;
     RedChannel *channel = red_channel_client_get_channel(rcc);
-    uint32_t type, id;
 
-    g_object_get(channel, "channel-type", &type, "id", &id, NULL);
+    red_channel_printerr(channel, "");
+
     dispatcher = (Dispatcher *)g_object_get_data(G_OBJECT(channel), "dispatcher");
-    spice_printerr("channel type %u id %u", type, id);
     payload.rcc = g_object_ref(rcc);
     dispatcher_send_message(dispatcher,
                             RED_WORKER_MESSAGE_DISPLAY_MIGRATE,
@@ -164,11 +163,10 @@ static void red_qxl_cursor_migrate(RedChannelClient *rcc)
     RedWorkerMessageCursorMigrate payload;
     Dispatcher *dispatcher;
     RedChannel *channel = red_channel_client_get_channel(rcc);
-    uint32_t type, id;
 
-    g_object_get(channel, "channel-type", &type, "id", &id, NULL);
+    red_channel_printerr(channel, "");
+
     dispatcher = (Dispatcher *)g_object_get_data(G_OBJECT(channel), "dispatcher");
-    spice_printerr("channel type %u id %u", type, id);
     payload.rcc = g_object_ref(rcc);
     dispatcher_send_message(dispatcher,
                             RED_WORKER_MESSAGE_CURSOR_MIGRATE,
diff --git a/server/spicevmc.c b/server/spicevmc.c
index 42ed2eb3..6a6e6202 100644
--- a/server/spicevmc.c
+++ b/server/spicevmc.c
@@ -804,15 +804,13 @@ static void spicevmc_connect(RedChannel *channel, RedClient *client,
     RedVmcChannel *vmc_channel;
     SpiceCharDeviceInstance *sin;
     SpiceCharDeviceInterface *sif;
-    uint32_t type, id;
 
     vmc_channel = RED_VMC_CHANNEL(channel);
     sin = vmc_channel->chardev_sin;
-    g_object_get(channel, "channel-type", &type, "id", &id, NULL);
 
     if (vmc_channel->rcc) {
-        spice_printerr("channel client %d:%d (%p) already connected, refusing second connection",
-                       type, id, vmc_channel->rcc);
+        red_channel_printerr(channel, "channel client (%p) already connected, refusing second connection",
+                             vmc_channel->rcc);
         // TODO: notify client in advance about the in use channel using
         // SPICE_MSG_MAIN_CHANNEL_IN_USE (for example)
         red_stream_free(stream);
commit 699a94ca8fed76849180c42156430b33f82b247d
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Mon Oct 23 18:31:09 2017 +0200

    utils: Introduce helpers to map channel types to names
    
    spice_server_set_channel_security() is already mostly doing that. We can
    make its code more generic, and introduce a red_channel_get_name()
    method. This method will then be used to make debug messages more
    readable by showing the actual channel name rather than its type as
    an int.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/red-channel.c b/server/red-channel.c
index 27cf9ac1..b3e98abc 100644
--- a/server/red-channel.c
+++ b/server/red-channel.c
@@ -452,6 +452,11 @@ int red_channel_is_connected(RedChannel *channel)
     return channel && channel->priv->clients;
 }
 
+const char *red_channel_get_name(RedChannel *channel)
+{
+    return red_channel_type_to_str(channel->priv->type);
+}
+
 void red_channel_remove_client(RedChannel *channel, RedChannelClient *rcc)
 {
     GList *link;
diff --git a/server/red-channel.h b/server/red-channel.h
index 573ddfd3..70282197 100644
--- a/server/red-channel.h
+++ b/server/red-channel.h
@@ -128,6 +128,8 @@ struct RedChannelClass
 
 GType red_channel_get_type(void) G_GNUC_CONST;
 
+const char *red_channel_get_name(RedChannel *channel);
+
 void red_channel_add_client(RedChannel *channel, RedChannelClient *rcc);
 void red_channel_remove_client(RedChannel *channel, RedChannelClient *rcc);
 
diff --git a/server/reds.c b/server/reds.c
index 816d0e69..0d0af05f 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3983,32 +3983,23 @@ SPICE_GNUC_VISIBLE int spice_server_set_zlib_glz_compression(SpiceServer *s, spi
 
 SPICE_GNUC_VISIBLE int spice_server_set_channel_security(SpiceServer *s, const char *channel, int security)
 {
-    static const char *const names[] = {
-        [ SPICE_CHANNEL_MAIN     ] = "main",
-        [ SPICE_CHANNEL_DISPLAY  ] = "display",
-        [ SPICE_CHANNEL_INPUTS   ] = "inputs",
-        [ SPICE_CHANNEL_CURSOR   ] = "cursor",
-        [ SPICE_CHANNEL_PLAYBACK ] = "playback",
-        [ SPICE_CHANNEL_RECORD   ] = "record",
-#ifdef USE_SMARTCARD
-        [ SPICE_CHANNEL_SMARTCARD] = "smartcard",
-#endif
-        [ SPICE_CHANNEL_USBREDIR ] = "usbredir",
-        [ SPICE_CHANNEL_WEBDAV ] = "webdav",
-    };
-    int i;
-
+    int type;
     if (channel == NULL) {
         s->config->default_channel_security = security;
         return 0;
     }
-    for (i = 0; i < SPICE_N_ELEMENTS(names); i++) {
-        if (names[i] && strcmp(names[i], channel) == 0) {
-            reds_set_one_channel_security(s, i, security);
-            return 0;
-        }
+    type = red_channel_name_to_type(channel);
+#ifndef USE_SMARTCARD
+    if (type == SPICE_CHANNEL_SMARTCARD) {
+        type = -1;
     }
-    return -1;
+#endif
+    if (type == -1) {
+        return -1;
+    }
+
+    reds_set_one_channel_security(s, type, security);
+    return 0;
 }
 
 /* very obsolete and old function, retain only for ABI */
diff --git a/server/utils.c b/server/utils.c
index 66df86ff..e25b7891 100644
--- a/server/utils.c
+++ b/server/utils.c
@@ -19,6 +19,7 @@
 #endif
 
 #include <glib.h>
+#include <spice/enums.h>
 #include "utils.h"
 
 int rgb32_data_has_alpha(int width, int height, size_t stride,
@@ -48,3 +49,55 @@ int rgb32_data_has_alpha(int width, int height, size_t stride,
     *all_set_out = has_alpha;
     return has_alpha;
 }
+
+/* These names are used to parse command line options, don't change them */
+static const char *const channel_names[] = {
+    [ SPICE_CHANNEL_MAIN     ] = "main",
+    [ SPICE_CHANNEL_DISPLAY  ] = "display",
+    [ SPICE_CHANNEL_INPUTS   ] = "inputs",
+    [ SPICE_CHANNEL_CURSOR   ] = "cursor",
+    [ SPICE_CHANNEL_PLAYBACK ] = "playback",
+    [ SPICE_CHANNEL_RECORD   ] = "record",
+    [ SPICE_CHANNEL_SMARTCARD] = "smartcard",
+    [ SPICE_CHANNEL_USBREDIR ] = "usbredir",
+    [ SPICE_CHANNEL_WEBDAV   ] = "webdav",
+};
+
+/**
+ * red_channel_type_to_str:
+ *
+ * This function returns a human-readable name from a SPICE_CHANNEL_* type.
+ * It must be called with a valid channel type.
+ *
+ * Returns: NULL if the channel type is invalid, the channel name otherwise.
+ */
+const char *red_channel_type_to_str(int type)
+{
+    g_return_val_if_fail(type >= 0, NULL);
+    g_return_val_if_fail(type < G_N_ELEMENTS(channel_names), NULL);
+    g_return_val_if_fail(channel_names[type] != NULL, NULL);
+
+    return channel_names[type];
+}
+
+/**
+ * red_channel_name_to_type:
+ *
+ * Converts a channel name to a SPICE_CHANNEL_* type. These names are currently
+ * used in our public API (see spice_server_set_channel_security()).
+ *
+ * Returns: -1 if @name was not a known channel name, the channel
+ * type otherwise.
+ *
+ */
+int red_channel_name_to_type(const char *name)
+{
+    int i;
+
+    for (i = 0; i < G_N_ELEMENTS(channel_names); i++) {
+        if (g_strcmp0(channel_names[i], name) == 0) {
+            return i;
+        }
+    }
+    return -1;
+}
diff --git a/server/utils.h b/server/utils.h
index ec2db2c9..3f735754 100644
--- a/server/utils.h
+++ b/server/utils.h
@@ -74,4 +74,7 @@ static inline red_time_t spice_get_monotonic_time_ms(void)
 int rgb32_data_has_alpha(int width, int height, size_t stride,
                          uint8_t *data, int *all_set_out);
 
+const char *red_channel_type_to_str(int type);
+int red_channel_name_to_type(const char *name);
+
 #endif /* UTILS_H_ */


More information about the Spice-commits mailing list