/soc/2015/jgeboski/facebook: 3ee1b27dacc4: facebook: added debug...

James Geboski jgeboski at gmail.com
Sat Jun 27 00:56:23 EDT 2015


Changeset: 3ee1b27dacc44931489bfdb71aa507ac29364aa8
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-06-27 00:56 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/3ee1b27dacc4

Description:

facebook: added debugging utilities and messages

diffstat:

 libpurple/protocols/facebook/api.c  |   15 +++-
 libpurple/protocols/facebook/mqtt.c |   17 ++++
 libpurple/protocols/facebook/util.c |  144 ++++++++++++++++++++++++++++++++++++
 libpurple/protocols/facebook/util.h |   46 +++++++++++
 4 files changed, 221 insertions(+), 1 deletions(-)

diffs (truncated from 323 to 300 lines):

diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c
--- a/libpurple/protocols/facebook/api.c
+++ b/libpurple/protocols/facebook/api.c
@@ -312,6 +312,9 @@ fb_api_json_chk(FbApi *api, gconstpointe
 	g_return_val_if_fail(FB_IS_API(api), FALSE);
 	priv = api->priv;
 
+	fb_util_debug(FB_UTIL_DEBUG_INFO, "Parsing JSON: %.*s",
+	              (gint) size, (const gchar *) data);
+
 	root = fb_json_node_new(data, size, &err);
 	FB_API_ERROR_CHK(api, err, return FALSE);
 
@@ -777,6 +780,8 @@ fb_api_cb_publish_p(FbApi *api, const GB
 
 		mptr = fb_api_presence_dup(&pres);
 		press = g_slist_prepend(press, mptr);
+		fb_util_debug_info("Presence: %" FB_ID_FORMAT " (%d)",
+		                   i64, i32 != 0);
 
 		/* Skip the last active timestamp field */
 		if (!fb_thrift_read_field(thft, &type, NULL)) {
@@ -837,6 +842,10 @@ fb_api_cb_mqtt_publish(FbMqtt *mqtt, con
 		bytes = (GByteArray*) pload;
 	}
 
+	fb_util_debug_hexdump(FB_UTIL_DEBUG_INFO, bytes,
+	                      "Reading message (topic: %s)",
+			      topic);
+
 	if (g_ascii_strcasecmp(topic, "/orca_typing_notifications") == 0) {
 		fb_api_cb_publish_tn(api, bytes);
 	} else if (g_ascii_strcasecmp(topic, "/t_ms") == 0) {
@@ -1120,8 +1129,12 @@ fb_api_publish(FbApi *api, const gchar *
 
 	bytes = g_byte_array_new_take((guint8*) msg, strlen(msg));
 	cytes = fb_util_zcompress(bytes);
+
+	fb_util_debug_hexdump(FB_UTIL_DEBUG_INFO, bytes,
+	                      "Writing message (topic: %s)",
+			      topic);
+
 	fb_mqtt_publish(priv->mqtt, topic, cytes);
-
 	g_byte_array_free(cytes, TRUE);
 	g_byte_array_free(bytes, TRUE);
 }
diff --git a/libpurple/protocols/facebook/mqtt.c b/libpurple/protocols/facebook/mqtt.c
--- a/libpurple/protocols/facebook/mqtt.c
+++ b/libpurple/protocols/facebook/mqtt.c
@@ -30,6 +30,7 @@
 
 #include "marshal.h"
 #include "mqtt.h"
+#include "util.h"
 
 struct _FbMqttPrivate
 {
@@ -211,6 +212,10 @@ fb_mqtt_close(FbMqtt *mqtt)
 		priv->gsc = NULL;
 	}
 
+	if (priv->wbuf->len > 0) {
+		fb_util_debug_warning("Closing with unwritten data");
+	}
+
 	priv->connected = FALSE;
 	g_byte_array_set_size(priv->rbuf, 0);
 	g_byte_array_set_size(priv->wbuf, 0);
@@ -383,6 +388,10 @@ fb_mqtt_read(FbMqtt *mqtt, FbMqttMessage
 	priv = mqtt->priv;
 	mriv = msg->priv;
 
+	fb_util_debug_hexdump(FB_UTIL_DEBUG_INFO, mriv->bytes,
+	                      "Reading %d (flags: 0x%0X)",
+			      mriv->type, mriv->flags);
+
 	switch (mriv->type) {
 	case FB_MQTT_MESSAGE_TYPE_CONNACK:
 		if (!fb_mqtt_message_read_byte(msg, NULL) ||
@@ -493,10 +502,14 @@ void
 fb_mqtt_write(FbMqtt *mqtt, FbMqttMessage *msg)
 {
 	const GByteArray *bytes;
+	FbMqttMessagePrivate *mriv;
 	FbMqttPrivate *priv;
 
 	g_return_if_fail(FB_IS_MQTT(mqtt));
+	g_return_if_fail(FB_IS_MQTT_MESSAGE(msg));
 	priv = mqtt->priv;
+	mriv = msg->priv;
+
 	bytes = fb_mqtt_message_bytes(msg);
 
 	if (G_UNLIKELY(bytes == NULL)) {
@@ -505,6 +518,10 @@ fb_mqtt_write(FbMqtt *mqtt, FbMqttMessag
 		return;
 	}
 
+	fb_util_debug_hexdump(FB_UTIL_DEBUG_INFO, mriv->bytes,
+	                      "Writing %d (flags: 0x%0X)",
+		              mriv->type, mriv->flags);
+
 	g_byte_array_append(priv->wbuf, bytes->data, bytes->len);
 	fb_mqtt_cb_write(mqtt, priv->gsc->fd, PURPLE_INPUT_WRITE);
 
diff --git a/libpurple/protocols/facebook/util.c b/libpurple/protocols/facebook/util.c
--- a/libpurple/protocols/facebook/util.c
+++ b/libpurple/protocols/facebook/util.c
@@ -21,11 +21,155 @@
 
 #include "internal.h"
 
+#include <stdarg.h>
 #include <string.h>
 #include <zlib.h>
 
 #include "util.h"
 
+void
+fb_util_debug(PurpleDebugLevel level, const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(level, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_vdebug(PurpleDebugLevel level, const gchar *format, va_list ap)
+{
+	gboolean unsafe;
+	gboolean verbose;
+	gchar *str;
+
+	g_return_if_fail(format != NULL);
+
+	unsafe = (level & FB_UTIL_DEBUG_FLAG_UNSAFE) != 0;
+	verbose = (level & FB_UTIL_DEBUG_FLAG_VERBOSE) != 0;
+
+	if ((unsafe && !purple_debug_is_unsafe()) ||
+	    (verbose && !purple_debug_is_verbose()))
+	{
+		return;
+	}
+
+	str = g_strdup_vprintf(format, ap);
+	purple_debug(level, "facebook", "%s", str);
+	g_free(str);
+}
+
+void
+fb_util_debug_misc(const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(PURPLE_DEBUG_MISC, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_debug_info(const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(PURPLE_DEBUG_INFO, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_debug_warning(const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(PURPLE_DEBUG_WARNING, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_debug_error(const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(PURPLE_DEBUG_ERROR, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_debug_fatal(const gchar *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	fb_util_vdebug(PURPLE_DEBUG_FATAL, format, ap);
+	va_end(ap);
+}
+
+void
+fb_util_debug_hexdump(PurpleDebugLevel level, const GByteArray *bytes,
+                      const gchar *format, ...)
+{
+	gchar c;
+	guint i;
+	guint j;
+	GString *gstr;
+	va_list ap;
+
+	static const gchar *indent = "  ";
+
+	g_return_if_fail(bytes != NULL);
+
+	if (format != NULL) {
+		va_start(ap, format);
+		fb_util_vdebug(level, format, ap);
+		va_end(ap);
+	}
+
+	gstr = g_string_sized_new(80);
+
+	for (i = 0; i < bytes->len; i += 16) {
+		g_string_append_printf(gstr, "%s%08x  ", indent, i);
+
+		for (j = 0; j < 16; j++) {
+			if ((i + j) < bytes->len) {
+				g_string_append_printf(gstr, "%02x ",
+				                       bytes->data[i + j]);
+			} else {
+				g_string_append(gstr, "   ");
+			}
+
+			if (j == 7) {
+				g_string_append_c(gstr, ' ');
+			}
+		}
+
+		g_string_append(gstr, " |");
+
+		for (j = 0; (j < 16) && ((i + j) < bytes->len); j++) {
+			c = bytes->data[i + j];
+
+			if (!g_ascii_isprint(c) || g_ascii_isspace(c)) {
+				c = '.';
+			}
+
+			g_string_append_c(gstr, c);
+		}
+
+		g_string_append_c(gstr, '|');
+		fb_util_debug(level, "%s", gstr->str);
+		g_string_erase(gstr, 0, -1);
+	}
+
+	g_string_append_printf(gstr, "%s%08x", indent, i);
+	fb_util_debug(level, "%s", gstr->str);
+	g_string_free(gstr, TRUE);
+}
+
 gchar *
 fb_util_locale_str(void)
 {
diff --git a/libpurple/protocols/facebook/util.h b/libpurple/protocols/facebook/util.h
--- a/libpurple/protocols/facebook/util.h
+++ b/libpurple/protocols/facebook/util.h
@@ -25,8 +25,54 @@
 #include "connection.h"
 #include "glibcompat.h"
 
+#define FB_UTIL_DEBUG_INFO (        \
+		PURPLE_DEBUG_INFO |         \
+		FB_UTIL_DEBUG_FLAG_UNSAFE | \
+		FB_UTIL_DEBUG_FLAG_VERBOSE  \
+	)
+
+typedef enum _FbUtilDebugFlags FbUtilDebugFlags;
+
 typedef void (*FbUtilRequestBuddyFunc) (GSList *buddies, gpointer data);
 
+enum _FbUtilDebugFlags
+{
+	FB_UTIL_DEBUG_FLAG_UNSAFE  = 1 << 25,
+	FB_UTIL_DEBUG_FLAG_VERBOSE = 1 << 26
+};
+
+void
+fb_util_debug(PurpleDebugLevel level, const gchar *format, ...)
+              G_GNUC_PRINTF(2, 3);
+
+void
+fb_util_vdebug(PurpleDebugLevel level, const gchar *format, va_list ap);
+
+void
+fb_util_debug_misc(const gchar *format, ...)
+                   G_GNUC_PRINTF(1, 2);
+
+void



More information about the Commits mailing list