/** * \file errors.c * Mesa debugging and error handling functions. */ /* * Mesa 3-D graphics library * Version: 7.1 * * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "errors.h" #include "imports.h" #include "context.h" #include "mtypes.h" #include "version.h" #define MAXSTRING 4000 /* for _mesa_vsnprintf() */ /**********************************************************************/ /** \name Diagnostics */ /*@{*/ static void output_if_debug(const char *prefixString, const char *outputString, GLboolean newline) { static int debug = -1; /* Check the MESA_DEBUG environment variable if it hasn't * been checked yet. We only have to check it once... */ if (debug == -1) { char *env = _mesa_getenv("MESA_DEBUG"); /* In a debug build, we print warning messages *unless* * MESA_DEBUG is 0. In a non-debug build, we don't * print warning messages *unless* MESA_DEBUG is * set *to any value*. */ #ifdef DEBUG debug = (env != NULL && atoi(env) == 0) ? 0 : 1; #else debug = (env != NULL) ? 1 : 0; #endif } /* Now only print the string if we're required to do so. */ if (debug) { fprintf(stderr, "%s: %s", prefixString, outputString); if (newline) fprintf(stderr, "\n"); #if defined(_WIN32) && !defined(_WIN32_WCE) /* 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" : ""); OutputDebugStringA(buf); } #endif } } /** * Return string version of GL error code. */ static const char * error_string( GLenum error ) { switch (error) { case GL_NO_ERROR: return "GL_NO_ERROR"; case GL_INVALID_VALUE: return "GL_INVALID_VALUE"; case GL_INVALID_ENUM: return "GL_INVALID_ENUM"; case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION"; case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW"; case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW"; case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY"; case GL_TABLE_TOO_LARGE: return "GL_TABLE_TOO_LARGE"; case GL_INVALID_FRAMEBUFFER_OPERATION_EXT: return "GL_INVALID_FRAMEBUFFER_OPERATION"; default: return "unknown"; } } /** * When a new type of error is recorded, print a message describing * previous errors which were accumulated. */ static void flush_delayed_errors( struct gl_context *ctx ) { char s[MAXSTRING]; if (ctx->ErrorDebugCount) { _mesa_snprintf(s, MAXSTRING, "%d similar %s errors", ctx->ErrorDebugCount, error_string(ctx->ErrorValue)); output_if_debug("Mesa", s, GL_TRUE); ctx->ErrorDebugCount = 0; } } /** * Report a warning (a recoverable error condition) to stderr if * either DEBUG is defined or the MESA_DEBUG env var is set. * * \param ctx GL context. * \param fmtString printf()-like format string. */ void _mesa_warning( struct gl_context *ctx, const char *fmtString, ... ) { char str[MAXSTRING]; va_list args; va_start( args, fmtString ); (void) _mesa_vsnprintf( str, MAXSTRING, fmtString, args ); va_end( args ); if (ctx) flush_delayed_errors( ctx ); output_if_debug("Mesa warning", str, GL_TRUE); } /** * Report an internal implementation problem. * Prints the message to stderr via fprintf(). * * \param ctx GL context. * \param fmtString problem description string. */ void _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... ) { va_list args; char str[MAXSTRING]; static int numCalls = 0; (void) ctx; if (numCalls < 50) { numCalls++; va_start( args, fmtString ); _mesa_vsnprintf( str, MAXSTRING, fmtString, args ); va_end( args ); fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str); fprintf(stderr, "Please report at bugs.freedesktop.org\n"); } } /** * Record an OpenGL state error. These usually occur when the user * passes invalid parameters to a GL function. * * 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 */ void _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... ) { static GLint debug = -1; /* Check debug environment variable only once: */ if (debug == -1) { const char *debugEnv = _mesa_getenv("MESA_DEBUG"); #ifdef DEBUG if (debugEnv && strstr(debugEnv, "silent")) debug = GL_FALSE; else debug = GL_TRUE; #else if (debugEnv) debug = GL_TRUE; else debug = GL_FALSE; #endif } if (debug) { if (ctx->ErrorValue == error && ctx->ErrorDebugFmtString == fmtString) { ctx->ErrorDebugCount++; } else { char s[MAXSTRING], s2[MAXSTRING]; va_list args; flush_delayed_errors( ctx ); va_start(args, fmtString); _mesa_vsnprintf(s, MAXSTRING, fmtString, args); va_end(args); _mesa_snprintf(s2, MAXSTRING, "%s in %s", error_string(error), s); output_if_debug("Mesa: User error", s2, GL_TRUE); ctx->ErrorDebugFmtString = fmtString; ctx->ErrorDebugCount = 0; } } _mesa_record_error(ctx, error); } /** * 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 char s[MAXSTRING]; va_list args; va_start(args, fmtString); _mesa_vsnprintf(s, MAXSTRING, fmtString, args); va_end(args); output_if_debug("Mesa", s, GL_FALSE); #endif /* DEBUG */ (void) ctx; (void) fmtString; } /*@}*/