mesa: fix formatting of messages printed using _mesa_log
[mesa.git] / src / mesa / main / errors.c
index 9932b4a5a89d3cb7104a2040b901a8858ae7d85e..02517c1c4078decd75428b936b6e2594b1ac8e0e 100644 (file)
 #include <stdio.h>
 #include "errors.h"
 #include "enums.h"
-#include "imports.h"
+
 #include "context.h"
 #include "debug_output.h"
-#include "dispatch.h"
-#include "hash.h"
-#include "mtypes.h"
-#include "version.h"
-#include "util/hash_table.h"
-#include "util/simple_list.h"
 
 
 static FILE *LogFile = NULL;
@@ -65,7 +59,7 @@ output_if_debug(const char *prefixString, const char *outputString,
          LogFile = fopen(logFile, "w");
       if (!LogFile)
          LogFile = stderr;
-#ifdef DEBUG
+#ifndef NDEBUG
       /* in debug builds, print messages unless MESA_DEBUG="silent" */
       if (MESA_DEBUG_FLAGS & DEBUG_SILENT)
          debug = 0;
@@ -88,11 +82,14 @@ output_if_debug(const char *prefixString, const char *outputString,
       fflush(LogFile);
 
 #if defined(_WIN32)
-      /* stderr from windows applications without console is not usually 
-       * visible, so communicate with the debugger instead */ 
+      /* stderr from windows applications without console is not usually
+       * visible, so communicate with the debugger instead */
       {
          char buf[4096];
-         _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
+         if (prefixString)
+            snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
+         else
+            snprintf(buf, sizeof(buf), "%s%s", outputString, newline ? "\n" : "");
          OutputDebugStringA(buf);
       }
 #endif
@@ -122,7 +119,7 @@ flush_delayed_errors( struct gl_context *ctx )
    char s[MAX_DEBUG_MESSAGE_LENGTH];
 
    if (ctx->ErrorDebugCount) {
-      _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors", 
+      snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
                      ctx->ErrorDebugCount,
                      _mesa_enum_to_string(ctx->ErrorValue));
 
@@ -145,10 +142,10 @@ _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
 {
    char str[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
-   va_start( args, fmtString );  
-   (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
+   va_start( args, fmtString );
+   (void) vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
    va_end( args );
-   
+
    if (ctx)
       flush_delayed_errors( ctx );
 
@@ -175,11 +172,11 @@ _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
    if (numCalls < 50) {
       numCalls++;
 
-      va_start( args, fmtString );  
-      _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
+      va_start( args, fmtString );
+      vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
       va_end( args );
-      fprintf(stderr, "Mesa %s implementation error: %s\n",
-              PACKAGE_VERSION, str);
+      fprintf(stderr, "Mesa " PACKAGE_VERSION " implementation error: %s\n",
+              str);
       fprintf(stderr, "Please report at " PACKAGE_BUGREPORT "\n");
    }
 }
@@ -195,7 +192,7 @@ should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
    if (debug == -1) {
       const char *debugEnv = getenv("MESA_DEBUG");
 
-#ifdef DEBUG
+#ifndef NDEBUG
       if (debugEnv && strstr(debugEnv, "silent"))
          debug = GL_FALSE;
       else
@@ -223,37 +220,67 @@ should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
 
 
 void
-_mesa_gl_vdebug(struct gl_context *ctx,
-                GLuint *id,
-                enum mesa_debug_source source,
-                enum mesa_debug_type type,
-                enum mesa_debug_severity severity,
-                const char *fmtString,
-                va_list args)
+_mesa_gl_vdebugf(struct gl_context *ctx,
+                 GLuint *id,
+                 enum mesa_debug_source source,
+                 enum mesa_debug_type type,
+                 enum mesa_debug_severity severity,
+                 const char *fmtString,
+                 va_list args)
 {
    char s[MAX_DEBUG_MESSAGE_LENGTH];
    int len;
 
    _mesa_debug_get_id(id);
 
-   len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   len = vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   if (len >= MAX_DEBUG_MESSAGE_LENGTH)
+      /* message was truncated */
+      len = MAX_DEBUG_MESSAGE_LENGTH - 1;
 
    _mesa_log_msg(ctx, source, type, *id, severity, len, s);
 }
 
 
 void
+_mesa_gl_debugf(struct gl_context *ctx,
+                GLuint *id,
+                enum mesa_debug_source source,
+                enum mesa_debug_type type,
+                enum mesa_debug_severity severity,
+                const char *fmtString, ...)
+{
+   va_list args;
+   va_start(args, fmtString);
+   _mesa_gl_vdebugf(ctx, id, source, type, severity, fmtString, args);
+   va_end(args);
+}
+
+size_t
 _mesa_gl_debug(struct gl_context *ctx,
                GLuint *id,
                enum mesa_debug_source source,
                enum mesa_debug_type type,
                enum mesa_debug_severity severity,
-               const char *fmtString, ...)
+               const char *msg)
 {
-   va_list args;
-   va_start(args, fmtString);
-   _mesa_gl_vdebug(ctx, id, source, type, severity, fmtString, args);
-   va_end(args);
+   _mesa_debug_get_id(id);
+
+   size_t len = strnlen(msg, MAX_DEBUG_MESSAGE_LENGTH);
+   if (len < MAX_DEBUG_MESSAGE_LENGTH) {
+      _mesa_log_msg(ctx, source, type, *id, severity, len, msg);
+      return len;
+   }
+
+   /* limit the message to fit within KHR_debug buffers */
+   char s[MAX_DEBUG_MESSAGE_LENGTH];
+   strncpy(s, msg, MAX_DEBUG_MESSAGE_LENGTH);
+   s[MAX_DEBUG_MESSAGE_LENGTH - 1] = '\0';
+   len = MAX_DEBUG_MESSAGE_LENGTH - 1;
+   _mesa_log_msg(ctx, source, type, *id, severity, len, s);
+
+   /* report the number of characters that were logged */
+   return len;
 }
 
 
@@ -264,7 +291,7 @@ _mesa_gl_debug(struct gl_context *ctx,
  * If debugging is enabled (either at compile-time via the DEBUG macro, or
  * run-time via the MESA_DEBUG environment variable), report the error with
  * _mesa_debug().
- * 
+ *
  * \param ctx the GL context.
  * \param error the error value.
  * \param fmtString printf() style format string, followed by optional args
@@ -282,7 +309,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
 
    do_output = should_output(ctx, error, fmtString);
 
-   mtx_lock(&ctx->DebugMutex);
+   simple_mtx_lock(&ctx->DebugMutex);
    if (ctx->Debug) {
       do_log = _mesa_debug_is_message_enabled(ctx->Debug,
                                               MESA_DEBUG_SOURCE_API,
@@ -293,7 +320,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
    else {
       do_log = GL_FALSE;
    }
-   mtx_unlock(&ctx->DebugMutex);
+   simple_mtx_unlock(&ctx->DebugMutex);
 
    if (do_output || do_log) {
       char s[MAX_DEBUG_MESSAGE_LENGTH], s2[MAX_DEBUG_MESSAGE_LENGTH];
@@ -301,7 +328,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
       va_list args;
 
       va_start(args, fmtString);
-      len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+      len = vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
       va_end(args);
 
       if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
@@ -312,7 +339,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
          return;
       }
 
-      len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
+      len = snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
                            _mesa_enum_to_string(error), s);
       if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
          /* Same as above. */
@@ -333,7 +360,8 @@ _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
    }
 
    /* Set the GL context error state for glGetError. */
-   _mesa_record_error(ctx, error);
+   if (ctx->ErrorValue == GL_NO_ERROR)
+      ctx->ErrorValue = error;
 }
 
 void
@@ -346,18 +374,18 @@ _mesa_error_no_memory(const char *caller)
 /**
  * Report debug information.  Print error message to stderr via fprintf().
  * No-op if DEBUG mode not enabled.
- * 
+ *
  * \param ctx GL context.
  * \param fmtString printf()-style format string, followed by optional args.
  */
 void
 _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
 {
-#ifdef DEBUG
+#ifndef NDEBUG
    char s[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
    va_start(args, fmtString);
-   _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
    va_end(args);
    output_if_debug("Mesa", s, GL_FALSE);
 #endif /* DEBUG */
@@ -372,9 +400,9 @@ _mesa_log(const char *fmtString, ...)
    char s[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
    va_start(args, fmtString);
-   _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
    va_end(args);
-   output_if_debug("", s, GL_FALSE);
+   output_if_debug(NULL, s, GL_FALSE);
 }