gallium: Add newline to eof.
[mesa.git] / src / gallium / auxiliary / util / p_debug.c
index cc036dabf82fe8c0473637d0bc5a8daa0fd9c0b8..4ec17466624a7f3a7add760063552472be5f214e 100644 (file)
  **************************************************************************/
 
 
+#include "pipe/p_config.h" 
+
 #include <stdarg.h>
 
-#ifdef WIN32
+#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
 #include <windows.h>
 #include <winddi.h>
 #else
 #include "pipe/p_compiler.h" 
 #include "pipe/p_util.h" 
 #include "pipe/p_debug.h" 
+#include "pipe/p_format.h" 
+#include "util/u_string.h" 
 
 
-#ifdef WIN32
+#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
 static INLINE void 
-rpl_EngDebugPrint(const char *format, ...)
+_EngDebugPrint(const char *format, ...)
 {
    va_list ap;
    va_start(ap, format);
    EngDebugPrint("", (PCHAR)format, ap);
    va_end(ap);
 }
-
-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
 
 
 void _debug_vprintf(const char *format, va_list ap)
 {
-#ifdef WIN32
+#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
 #ifndef WINCE
    /* EngDebugPrint does not handle float point arguments, so we need to use
-    * our own vsnprintf implementation */
-   char buf[512 + 1];
-   rpl_vsnprintf(buf, sizeof(buf), format, ap);
-   rpl_EngDebugPrint("%s", buf);
+    * our own vsnprintf implementation. It is also very slow, so buffer until
+    * we find a newline. */
+   static char buf[512 + 1] = {'\0'};
+   size_t len = strlen(buf);
+   int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
+   if(ret > (int)(sizeof(buf) - len - 1) || strchr(buf + len, '\n')) {
+      _EngDebugPrint("%s", buf);
+      buf[0] = '\0';
+   }
 #else
    /* TODO: Implement debug print for WINCE */
 #endif
@@ -100,7 +104,7 @@ void _debug_break(void)
    __asm("int3");
 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
    _asm {int 3};
-#elif defined(WIN32) && !defined(WINCE)
+#elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
    EngDebugBreak();
 #else
    abort();
@@ -108,7 +112,7 @@ void _debug_break(void)
 }
 
 
-#ifdef WIN32
+#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
 static const char *
 find(const char *start, const char *end, char c) 
 {
@@ -149,16 +153,18 @@ const char *
 debug_get_option(const char *name, const char *dfault)
 {
    const char *result;
-#ifdef WIN32
+#ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
    ULONG_PTR iFile = 0;
    const void *pMap = NULL;
    const char *sol, *eol, *sep;
    static char output[1024];
    
+   result = dfault;
+   /* XXX: this creates the file if it does not exists, so it must either be
+    * disabled on release versions, or put in a less conspicuous place.
+    */
    pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
-   if(!pMap)
-      result = dfault;
-   else {
+   if(pMap) {
       sol = (const char *)pMap;
       while(1) {
         /* TODO: handle LF line endings */
@@ -184,10 +190,7 @@ debug_get_option(const char *name, const char *dfault)
       result = dfault;
 #endif
       
-   if(result)
-      debug_printf("%s: %s = %s\n", __FUNCTION__, name, result);
-   else
-      debug_printf("%s: %s = (null)\n", __FUNCTION__, name);
+   debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
    
    return result;
 }
@@ -200,6 +203,8 @@ debug_get_bool_option(const char *name, boolean dfault)
    
    if(str == NULL)
       result = dfault;
+   else if(!strcmp(str, "n"))
+      result = FALSE;
    else if(!strcmp(str, "no"))
       result = FALSE;
    else if(!strcmp(str, "0"))
@@ -251,57 +256,16 @@ debug_get_flags_option(const char *name,
 }
 
 
-#if defined(WIN32)
-ULONG_PTR debug_config_file = 0;
-void *mapped_config_file = 0;
-
-enum {
-       eAssertAbortEn = 0x1,
-};
-
-/* Check for aborts enabled. */
-static unsigned abort_en(void)
-{
-   if (!mapped_config_file)
-   {
-      /* Open an 8 byte file for configuration data. */
-      mapped_config_file = EngMapFile(L"\\??\\c:\\gaDebug.cfg", 8, &debug_config_file);
-   }
-
-   /* A value of "0" (ascii) in the configuration file will clear the
-    * first 8 bits in the test byte. 
-    *
-    * A value of "1" (ascii) in the configuration file will set the
-    * first bit in the test byte. 
-    *
-    * A value of "2" (ascii) in the configuration file will set the
-    * second bit in the test byte. 
-    *
-    * Currently the only interesting values are 0 and 1, which clear
-    * and set abort-on-assert behaviour respectively.
-    */
-   return ((((char *)mapped_config_file)[0]) - 0x30) & eAssertAbortEn;
-}
-#else /* WIN32 */
-static unsigned abort_en(void)
-{
-   return !GETENV("GALLIUM_ABORT_ON_ASSERT");
-}
-#endif
-
 void _debug_assert_fail(const char *expr, 
                         const char *file, 
                         unsigned line, 
                         const char *function) 
 {
    _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
-   if (abort_en())
-   {
+   if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE))
       debug_break();
-   } else
-   {
+   else
       _debug_printf("continuing...\n");
-   }
 }
 
 
@@ -317,7 +281,7 @@ debug_dump_enum(const struct debug_named_value *names,
       ++names;
    }
 
-   snprintf(rest, sizeof(rest), "0x%08lx", value);
+   util_snprintf(rest, sizeof(rest), "0x%08lx", value);
    return rest;
 }
 
@@ -350,7 +314,7 @@ debug_dump_flags(const struct debug_named_value *names,
       else
         first = 0;
       
-      snprintf(rest, sizeof(rest), "0x%08lx", value);
+      util_snprintf(rest, sizeof(rest), "0x%08lx", value);
       strncat(output, rest, sizeof(output));
    }
    
@@ -361,3 +325,101 @@ debug_dump_flags(const struct debug_named_value *names,
 }
 
 
+
+
+
+
+char *pf_sprint_name( char *str, enum pipe_format format )
+{
+   strcpy( str, "PIPE_FORMAT_" );
+   switch (pf_layout( format )) {
+   case PIPE_FORMAT_LAYOUT_RGBAZS:
+      {
+         pipe_format_rgbazs_t rgbazs = (pipe_format_rgbazs_t) format;
+         uint                 i;
+         uint                 scale = 1 << (pf_exp8( rgbazs ) * 3);
+
+         for (i = 0; i < 4; i++) {
+            uint  size = pf_size_xyzw( rgbazs, i );
+
+            if (size == 0) {
+               break;
+            }
+            switch (pf_swizzle_xyzw( rgbazs, i )) {
+            case PIPE_FORMAT_COMP_R:
+               strcat( str, "R" );
+               break;
+            case PIPE_FORMAT_COMP_G:
+               strcat( str, "G" );
+               break;
+            case PIPE_FORMAT_COMP_B:
+               strcat( str, "B" );
+               break;
+            case PIPE_FORMAT_COMP_A:
+               strcat( str, "A" );
+               break;
+            case PIPE_FORMAT_COMP_0:
+               strcat( str, "0" );
+               break;
+            case PIPE_FORMAT_COMP_1:
+               strcat( str, "1" );
+               break;
+            case PIPE_FORMAT_COMP_Z:
+               strcat( str, "Z" );
+               break;
+            case PIPE_FORMAT_COMP_S:
+               strcat( str, "S" );
+               break;
+            }
+            util_snprintf( &str[strlen( str )], 32, "%u", size * scale );
+         }
+         if (i != 0) {
+            strcat( str, "_" );
+         }
+         switch (pf_type( rgbazs )) {
+         case PIPE_FORMAT_TYPE_UNKNOWN:
+            strcat( str, "NONE" );
+            break;
+         case PIPE_FORMAT_TYPE_FLOAT:
+            strcat( str, "FLOAT" );
+            break;
+         case PIPE_FORMAT_TYPE_UNORM:
+            strcat( str, "UNORM" );
+            break;
+         case PIPE_FORMAT_TYPE_SNORM:
+            strcat( str, "SNORM" );
+            break;
+         case PIPE_FORMAT_TYPE_USCALED:
+            strcat( str, "USCALED" );
+            break;
+         case PIPE_FORMAT_TYPE_SSCALED:
+            strcat( str, "SSCALED" );
+            break;
+         }
+      }
+      break;
+   case PIPE_FORMAT_LAYOUT_YCBCR:
+      {
+         pipe_format_ycbcr_t  ycbcr = (pipe_format_ycbcr_t) format;
+
+         strcat( str, "YCBCR" );
+         if (pf_rev( ycbcr )) {
+            strcat( str, "_REV" );
+         }
+      }
+      break;
+   }
+   return str;
+}
+
+
+#ifdef DEBUG
+void debug_print_format(const char *msg, unsigned fmt )
+{
+   char fmtstr[80];
+   pf_sprint_name(fmtstr, (enum pipe_format)fmt);
+
+   debug_printf("%s: %s\n", msg, fmtstr); 
+}
+#endif