From a2aa7ab8eabe91298040781d9f3e4ccbb40c7606 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
Date: Fri, 9 Dec 2011 18:37:49 +0100
Subject: [PATCH spice-gtk] controller: log traffic

https://bugs.freedesktop.org/show_bug.cgi?id=42707
---
 gtk/controller/Makefile.am     |    1 +
 gtk/controller/controller.vala |    1 +
 gtk/controller/hexdump.vala    |   67 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
 create mode 100644 gtk/controller/hexdump.vala

diff --git a/gtk/controller/Makefile.am b/gtk/controller/Makefile.am
index 2d5c5af..438c4d0 100644
--- a/gtk/controller/Makefile.am
+++ b/gtk/controller/Makefile.am
@@ -28,6 +28,7 @@ BUILT_SOURCES = controller.vala.stamp
 libspice_controller_la_VALASOURCES =		\
 	menu.vala				\
 	controller.vala				\
+	hexdump.vala				\
 	$(NULL)
 libspice_controller_la_SOURCES =		\
 	custom.h				\
diff --git a/gtk/controller/controller.vala b/gtk/controller/controller.vala
index e33278f..27ba0e7 100644
--- a/gtk/controller/controller.vala
+++ b/gtk/controller/controller.vala
@@ -208,6 +208,7 @@ public class Controller: Object {
 				if (warn_if (read != msg.size - sizeof(SpiceProtocol.Controller.Msg)))
 					break;
 			}
+			debug ("ctrl msg id=%u size=%u\n".printf (msg.id, msg.size) + hexdump (t[0:msg.size]));
 
 			handle_message (msg);
 		}
diff --git a/gtk/controller/hexdump.vala b/gtk/controller/hexdump.vala
new file mode 100644
index 0000000..b3849c6
--- /dev/null
+++ b/gtk/controller/hexdump.vala
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2009-2011 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
+ *
+ * hexdump() is copied from Cornucopia libfsobasics/fsobasics/stringhandling.vala
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ */
+using GLib;
+
+namespace SpiceCtrl {
+
+    public string hexdump (uint8[] array, int linelength = 16, string prefix = "", uchar unknownCharacter = '?')
+    {
+        if (array.length < 1)
+            return "";
+
+		string result = "";
+
+		int BYTES_PER_LINE = linelength;
+
+		var hexline = new StringBuilder (prefix);
+		var ascline = new StringBuilder ();
+		uchar b;
+		int i;
+
+		for (i = 0; i < array.length; ++i) {
+			b = array[i];
+			hexline.append_printf ("%02X ", b);
+			if (31 < b && b < 128)
+				ascline.append_printf ("%c", b);
+			else
+				ascline.append_printf (".");
+
+			if (i % BYTES_PER_LINE + 1 == BYTES_PER_LINE) {
+				hexline.append (ascline.str);
+				result += hexline.str;
+				result += "\n";
+				hexline = new StringBuilder (prefix);
+				ascline = new StringBuilder ();
+			}
+		}
+
+		if (i % BYTES_PER_LINE != BYTES_PER_LINE) {
+			while (hexline.len < 3 * BYTES_PER_LINE)
+				hexline.append_c (' ');
+
+			hexline.append (ascline.str);
+			result += hexline.str;
+			result += "\n";
+		}
+
+		return result.strip ();
+	}
+}
-- 
1.7.7.3