From 738b88aabacafb8a9ffb28a66c31015afbbae652 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Mon, 27 Apr 2020 11:49:55 +0300
Subject: [PATCH] debug: randomize object printout colors

This way we can more easily distinguish different objects when
looking at the log, although it's not perfect
---
 lib/wp/debug.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/wp/debug.c b/lib/wp/debug.c
index 6f9bd899..33bfe1e5 100644
--- a/lib/wp/debug.c
+++ b/lib/wp/debug.c
@@ -48,7 +48,13 @@ struct common_fields
 /* our palette */
 #define DOMAIN_COLOR    COLOR_MAGENTA
 #define LOCATION_COLOR  COLOR_BLUE
-#define OBJECT_COLOR    COLOR_YELLOW
+
+/* available colors for object printouts (the <Object:0xfoobar>) */
+static const gchar *object_colors[] = {
+  COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_MAGENTA, COLOR_CYAN,
+  COLOR_BRIGHT_RED, COLOR_BRIGHT_GREEN, COLOR_BRIGHT_YELLOW,
+  COLOR_BRIGHT_MAGENTA, COLOR_BRIGHT_CYAN
+};
 
 /*
  * priority numbers are based on GLib's gmessages.c
@@ -173,8 +179,14 @@ write_debug_message (FILE *s, struct common_fields *cf)
 static inline gchar *
 format_message (struct common_fields *cf)
 {
+  const gchar *object_color = "";
+  if (use_color) {
+    guint h = g_direct_hash (cf->object) % G_N_ELEMENTS (object_colors);
+    object_color = object_colors[h];
+  }
+
   return g_strdup_printf ("%s<%s%s%p>%s %s",
-      use_color ? OBJECT_COLOR : "",
+      object_color,
       cf->object_type != 0 ? g_type_name (cf->object_type) : "",
       cf->object_type != 0 ? ":" : "",
       cf->object,
-- 
GitLab