Improved diagnostic output for glCheck and alCheck macros

Changes:
* In addition to source file and line, the expression itself is output
* For better readability, the log is split across multiple lines
* alCheck() doesn't unnecessarily construct std::string when there is no error
* Unused #include directives are removed
This commit is contained in:
Jan Haller 2015-06-25 23:51:38 +02:00 committed by Lukas Dürrenberger
parent b7d984cdac
commit 64ddb10f3d
4 changed files with 36 additions and 32 deletions

View File

@ -26,8 +26,8 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Audio/ALCheck.hpp> #include <SFML/Audio/ALCheck.hpp>
#include <SFML/Audio/AudioDevice.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <string>
namespace sf namespace sf
@ -35,14 +35,16 @@ namespace sf
namespace priv namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void alCheckError(const std::string& file, unsigned int line) void alCheckError(const char* file, unsigned int line, const char* expression)
{ {
// Get the last error // Get the last error
ALenum errorCode = alGetError(); ALenum errorCode = alGetError();
if (errorCode != AL_NO_ERROR) if (errorCode != AL_NO_ERROR)
{ {
std::string error, description; std::string fileString = file;
std::string error = "Unknown error";
std::string description = "No description";
// Decode the error code // Decode the error code
switch (errorCode) switch (errorCode)
@ -50,43 +52,44 @@ void alCheckError(const std::string& file, unsigned int line)
case AL_INVALID_NAME: case AL_INVALID_NAME:
{ {
error = "AL_INVALID_NAME"; error = "AL_INVALID_NAME";
description = "an unacceptable name has been specified"; description = "A bad name (ID) has been specified.";
break; break;
} }
case AL_INVALID_ENUM: case AL_INVALID_ENUM:
{ {
error = "AL_INVALID_ENUM"; error = "AL_INVALID_ENUM";
description = "an unacceptable value has been specified for an enumerated argument"; description = "An unacceptable value has been specified for an enumerated argument.";
break; break;
} }
case AL_INVALID_VALUE: case AL_INVALID_VALUE:
{ {
error = "AL_INVALID_VALUE"; error = "AL_INVALID_VALUE";
description = "a numeric argument is out of range"; description = "A numeric argument is out of range.";
break; break;
} }
case AL_INVALID_OPERATION: case AL_INVALID_OPERATION:
{ {
error = "AL_INVALID_OPERATION"; error = "AL_INVALID_OPERATION";
description = "the specified operation is not allowed in the current state"; description = "The specified operation is not allowed in the current state.";
break; break;
} }
case AL_OUT_OF_MEMORY: case AL_OUT_OF_MEMORY:
{ {
error = "AL_OUT_OF_MEMORY"; error = "AL_OUT_OF_MEMORY";
description = "there is not enough memory left to execute the command"; description = "There is not enough memory left to execute the command.";
break; break;
} }
} }
// Log the error // Log the error
err() << "An internal OpenAL call failed in " err() << "An internal OpenAL call failed in "
<< file.substr(file.find_last_of("\\/") + 1) << " (" << line << ") : " << fileString.substr(fileString.find_last_of("\\/") + 1) << "(" << line << ")."
<< error << ", " << description << "\nExpression:\n " << expression
<< "\nError description:\n " << error << "\n " << description << "\n"
<< std::endl; << std::endl;
} }
} }

View File

@ -29,8 +29,6 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Config.hpp> #include <SFML/Config.hpp>
#include <iostream>
#include <string>
#ifdef SFML_SYSTEM_IOS #ifdef SFML_SYSTEM_IOS
#include <OpenAl/al.h> #include <OpenAl/al.h>
#include <OpenAl/alc.h> #include <OpenAl/alc.h>
@ -45,7 +43,7 @@ namespace sf
namespace priv namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Let's define a macro to quickly check every OpenAL API calls /// Let's define a macro to quickly check every OpenAL API call
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#ifdef SFML_DEBUG #ifdef SFML_DEBUG
@ -56,7 +54,7 @@ namespace priv
#else #else
// Else, we don't add any overhead // Else, we don't add any overhead
#define alCheck(Func) (Func) #define alCheck(expr) (expr)
#endif #endif
@ -66,9 +64,10 @@ namespace priv
/// ///
/// \param file Source file where the call is located /// \param file Source file where the call is located
/// \param line Line number of the source file where the call is located /// \param line Line number of the source file where the call is located
/// \param expression The evaluated expression as a string
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void alCheckError(const std::string& file, unsigned int line); void alCheckError(const char* file, unsigned int line, const char* expression);
} // namespace priv } // namespace priv

View File

@ -27,6 +27,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Graphics/GLCheck.hpp> #include <SFML/Graphics/GLCheck.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <string>
namespace sf namespace sf
@ -34,16 +35,16 @@ namespace sf
namespace priv namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void glCheckError(const char* file, unsigned int line) void glCheckError(const char* file, unsigned int line, const char* expression)
{ {
// Get the last error // Get the last error
GLenum errorCode = glGetError(); GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) if (errorCode != GL_NO_ERROR)
{ {
std::string fileString(file); std::string fileString = file;
std::string error = "unknown error"; std::string error = "Unknown error";
std::string description = "no description"; std::string description = "No description";
// Decode the error code // Decode the error code
switch (errorCode) switch (errorCode)
@ -51,57 +52,58 @@ void glCheckError(const char* file, unsigned int line)
case GL_INVALID_ENUM: case GL_INVALID_ENUM:
{ {
error = "GL_INVALID_ENUM"; error = "GL_INVALID_ENUM";
description = "an unacceptable value has been specified for an enumerated argument"; description = "An unacceptable value has been specified for an enumerated argument.";
break; break;
} }
case GL_INVALID_VALUE: case GL_INVALID_VALUE:
{ {
error = "GL_INVALID_VALUE"; error = "GL_INVALID_VALUE";
description = "a numeric argument is out of range"; description = "A numeric argument is out of range.";
break; break;
} }
case GL_INVALID_OPERATION: case GL_INVALID_OPERATION:
{ {
error = "GL_INVALID_OPERATION"; error = "GL_INVALID_OPERATION";
description = "the specified operation is not allowed in the current state"; description = "The specified operation is not allowed in the current state.";
break; break;
} }
case GL_STACK_OVERFLOW: case GL_STACK_OVERFLOW:
{ {
error = "GL_STACK_OVERFLOW"; error = "GL_STACK_OVERFLOW";
description = "this command would cause a stack overflow"; description = "This command would cause a stack overflow.";
break; break;
} }
case GL_STACK_UNDERFLOW: case GL_STACK_UNDERFLOW:
{ {
error = "GL_STACK_UNDERFLOW"; error = "GL_STACK_UNDERFLOW";
description = "this command would cause a stack underflow"; description = "This command would cause a stack underflow.";
break; break;
} }
case GL_OUT_OF_MEMORY: case GL_OUT_OF_MEMORY:
{ {
error = "GL_OUT_OF_MEMORY"; error = "GL_OUT_OF_MEMORY";
description = "there is not enough memory left to execute the command"; description = "There is not enough memory left to execute the command.";
break; break;
} }
case GLEXT_GL_INVALID_FRAMEBUFFER_OPERATION: case GLEXT_GL_INVALID_FRAMEBUFFER_OPERATION:
{ {
error = "GL_INVALID_FRAMEBUFFER_OPERATION"; error = "GL_INVALID_FRAMEBUFFER_OPERATION";
description = "the object bound to FRAMEBUFFER_BINDING is not \"framebuffer complete\""; description = "The object bound to FRAMEBUFFER_BINDING is not \"framebuffer complete\".";
break; break;
} }
} }
// Log the error // Log the error
err() << "An internal OpenGL call failed in " err() << "An internal OpenGL call failed in "
<< fileString.substr(fileString.find_last_of("\\/") + 1) << " (" << line << ") : " << fileString.substr(fileString.find_last_of("\\/") + 1) << "(" << line << ")."
<< error << ", " << description << "\nExpression:\n " << expression
<< "\nError description:\n " << error << "\n " << description << "\n"
<< std::endl; << std::endl;
} }
} }

View File

@ -30,7 +30,6 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Config.hpp> #include <SFML/Config.hpp>
#include <SFML/Graphics/GLExtensions.hpp> #include <SFML/Graphics/GLExtensions.hpp>
#include <string>
namespace sf namespace sf
@ -38,7 +37,7 @@ namespace sf
namespace priv namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Let's define a macro to quickly check every OpenGL API calls /// Let's define a macro to quickly check every OpenGL API call
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#ifdef SFML_DEBUG #ifdef SFML_DEBUG
@ -49,7 +48,7 @@ namespace priv
#else #else
// Else, we don't add any overhead // Else, we don't add any overhead
#define glCheck(call) (call) #define glCheck(expr) (expr)
#endif #endif
@ -58,9 +57,10 @@ namespace priv
/// ///
/// \param file Source file where the call is located /// \param file Source file where the call is located
/// \param line Line number of the source file where the call is located /// \param line Line number of the source file where the call is located
/// \param expression The evaluated expression as a string
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void glCheckError(const char* file, unsigned int line); void glCheckError(const char* file, unsigned int line, const char* expression);
} // namespace priv } // namespace priv