Skip to content

Commit

Permalink
Improved diagnostic output for glCheck and alCheck macros
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Bromeon authored and eXpl0it3r committed Aug 24, 2015
1 parent 0bcd7d3 commit e5f98a6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
23 changes: 13 additions & 10 deletions src/SFML/Audio/ALCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,70 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/ALCheck.hpp>
#include <SFML/Audio/AudioDevice.hpp>
#include <SFML/System/Err.hpp>
#include <string>


namespace sf
{
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
ALenum errorCode = alGetError();

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
switch (errorCode)
{
case AL_INVALID_NAME:
{
error = "AL_INVALID_NAME";
description = "an unacceptable name has been specified";
description = "A bad name (ID) has been specified.";
break;
}

case 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;
}

case AL_INVALID_VALUE:
{
error = "AL_INVALID_VALUE";
description = "a numeric argument is out of range";
description = "A numeric argument is out of range.";
break;
}

case 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;
}

case 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;
}
}

// Log the error
err() << "An internal OpenAL call failed in "
<< file.substr(file.find_last_of("\\/") + 1) << " (" << line << ") : "
<< error << ", " << description
<< fileString.substr(fileString.find_last_of("\\/") + 1) << "(" << line << ")."
<< "\nExpression:\n " << expression
<< "\nError description:\n " << error << "\n " << description << "\n"
<< std::endl;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/SFML/Audio/ALCheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <iostream>
#include <string>
#ifdef SFML_SYSTEM_IOS
#include <OpenAl/al.h>
#include <OpenAl/alc.h>
Expand All @@ -45,7 +43,7 @@ namespace sf
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

Expand All @@ -56,7 +54,7 @@ namespace priv
#else

// Else, we don't add any overhead
#define alCheck(Func) (Func)
#define alCheck(expr) (expr)

#endif

Expand All @@ -66,9 +64,10 @@ namespace priv
///
/// \param file 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

Expand Down
28 changes: 15 additions & 13 deletions src/SFML/Graphics/GLCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,81 +27,83 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/GLCheck.hpp>
#include <SFML/System/Err.hpp>
#include <string>


namespace sf
{
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
GLenum errorCode = glGetError();

if (errorCode != GL_NO_ERROR)
{
std::string fileString(file);
std::string error = "unknown error";
std::string description = "no description";
std::string fileString = file;
std::string error = "Unknown error";
std::string description = "No description";

// Decode the error code
switch (errorCode)
{
case 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;
}

case GL_INVALID_VALUE:
{
error = "GL_INVALID_VALUE";
description = "a numeric argument is out of range";
description = "A numeric argument is out of range.";
break;
}

case 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;
}

case GL_STACK_OVERFLOW:
{
error = "GL_STACK_OVERFLOW";
description = "this command would cause a stack overflow";
description = "This command would cause a stack overflow.";
break;
}

case GL_STACK_UNDERFLOW:
{
error = "GL_STACK_UNDERFLOW";
description = "this command would cause a stack underflow";
description = "This command would cause a stack underflow.";
break;
}

case 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;
}

case GLEXT_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;
}
}

// Log the error
err() << "An internal OpenGL call failed in "
<< fileString.substr(fileString.find_last_of("\\/") + 1) << " (" << line << ") : "
<< error << ", " << description
<< fileString.substr(fileString.find_last_of("\\/") + 1) << "(" << line << ")."
<< "\nExpression:\n " << expression
<< "\nError description:\n " << error << "\n " << description << "\n"
<< std::endl;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/SFML/Graphics/GLCheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Graphics/GLExtensions.hpp>
#include <string>


namespace sf
{
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

Expand All @@ -49,7 +48,7 @@ namespace priv
#else

// Else, we don't add any overhead
#define glCheck(call) (call)
#define glCheck(expr) (expr)

#endif

Expand All @@ -58,9 +57,10 @@ namespace priv
///
/// \param file 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

Expand Down

0 comments on commit e5f98a6

Please sign in to comment.