OSDN Git Service

gallium: Add generic enum and flags dumping utility functions.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Wed, 19 Mar 2008 16:41:07 +0000 (16:41 +0000)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Wed, 19 Mar 2008 16:42:19 +0000 (16:42 +0000)
src/gallium/auxiliary/util/p_debug.c
src/gallium/include/pipe/p_debug.h

index bb84e80..bd3a022 100644 (file)
@@ -52,6 +52,9 @@ rpl_EngDebugPrint(const char *format, ...)
 }
 
 int rpl_vsnprintf(char *, size_t, const char *, va_list);
+int rpl_snprintf(char *str, size_t size, const char *format, ...);
+#define vsnprintf rpl_vsnprintf
+#define snprintf rpl_snprintf
 #endif
 
 
@@ -181,3 +184,59 @@ void debug_mask_vprintf(uint32_t uuid, uint32_t what, const char *format, va_lis
    if(mask & what)
       debug_vprintf(format, ap);
 }
+
+
+const char *
+debug_dump_enum(const struct debug_named_value *names, 
+                unsigned long value)
+{
+   static char rest[256];
+   
+   while(names->name) {
+      if(names->value == value)
+        return names->name;
+      ++names;
+   }
+
+   snprintf(rest, sizeof(rest), "0x%08x", value);
+   return rest;
+}
+
+
+const char *
+debug_dump_flags(const struct debug_named_value *names, 
+                 unsigned long value)
+{
+   static char output[4096];
+   static char rest[256];
+   int first = 1;
+
+   output[0] = '\0';
+
+   while(names->name) {
+      if((names->value & value) == names->value) {
+        if (!first)
+           strncat(output, "|", sizeof(output));
+        else
+           first = 0;
+        strncat(output, names->name, sizeof(output));
+        value &= ~names->value;
+      }
+      ++names;
+   }
+   
+   if (value) {
+      if (!first)
+        strncat(output, "|", sizeof(output));
+      else
+        first = 0;
+      
+      snprintf(rest, sizeof(rest), "0x%08x", value);
+      strncat(output, rest, sizeof(output));
+   }
+   
+   if(first)
+      return "0";
+   
+   return output;
+}
index f3dfa06..e924c1e 100644 (file)
@@ -168,6 +168,55 @@ void debug_mask_vprintf(uint32_t uuid,
 #endif
 
 
+/**
+ * Used by debug_dump_enum and debug_dump_flags to describe symbols.
+ */
+struct debug_named_value
+{
+   const char *name;
+   unsigned long value;
+};
+
+
+/**
+ * Some C pre-processor magic to simplify creating named values.
+ * 
+ * Example:
+ * @code
+ * static const debug_named_value my_names[] = {
+ *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
+ *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
+ *    DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
+ *    DEBUG_NAMED_VALUE_END
+ * };
+ * 
+ *    ...
+ *    debug_printf("%s = %s\n", 
+ *                 name,
+ *                 debug_dump_enum(my_names, my_value));
+ *    ...
+ * @endcode
+ */
+#define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol} 
+#define DEBUG_NAMED_VALUE_END {NULL, 0} 
+
+
+/**
+ * Convert a enum value to a string.
+ */
+const char *
+debug_dump_enum(const struct debug_named_value *names, 
+                unsigned long value);
+
+
+/**
+ * Convert binary flags value to a string.
+ */
+const char *
+debug_dump_flags(const struct debug_named_value *names, 
+                 unsigned long value);
+
+
 #ifdef __cplusplus
 }
 #endif