Make the error printed on check_error() slightly friendlier: Include the enum if...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 26 Jul 2016 13:30:56 +0000 (15:30 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 26 Jul 2016 13:30:56 +0000 (15:30 +0200)
defs.h
util.cpp
util.h

diff --git a/defs.h b/defs.h
index ad1826c..155ef9b 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -5,8 +5,10 @@
 
 #ifdef __GNUC__
 #define MUST_CHECK_RESULT __attribute__((warn_unused_result))
+#define DOES_NOT_RETURN __attribute__((noreturn))
 #else
 #define MUST_CHECK_RESULT 
+#define DOES_NOT_RETURN
 #endif
 
 #endif  // !defined(_MOVIT_DEFS_H)
index 3141f8d..3bbc869 100644 (file)
--- a/util.cpp
+++ b/util.cpp
@@ -354,4 +354,39 @@ void *get_gl_context_identifier()
 #endif
 }
 
+void abort_gl_error(GLenum err, const char *filename, int line)
+{
+       const char *err_text = "unknown";
+
+       // All errors listed in the glGetError(3G) man page.
+       switch (err) {
+       case GL_NO_ERROR:
+               err_text = "GL_NO_ERROR";  // Should not happen.
+               break;
+       case GL_INVALID_ENUM:
+               err_text = "GL_INVALID_ENUM";
+               break;
+       case GL_INVALID_VALUE:
+               err_text = "GL_INVALID_VALUE";
+               break;
+       case GL_INVALID_OPERATION:
+               err_text = "GL_INVALID_OPERATION";
+               break;
+       case GL_INVALID_FRAMEBUFFER_OPERATION:
+               err_text = "GL_INVALID_FRAMEBUFFER_OPERATION";
+               break;
+       case GL_OUT_OF_MEMORY:
+               err_text = "GL_OUT_OF_MEMORY";
+               break;
+       case GL_STACK_UNDERFLOW:
+               err_text = "GL_STACK_UNDERFLOW";
+               break;
+       case GL_STACK_OVERFLOW:
+               err_text = "GL_STACK_OVERFLOW";
+               break;
+       }
+       fprintf(stderr, "GL error 0x%x (%s) at %s:%d\n", err, err_text, filename, line);
+       abort();
+}
+
 }  // namespace movit
diff --git a/util.h b/util.h
index f5b5dd3..45fe6ba 100644 (file)
--- a/util.h
+++ b/util.h
@@ -8,6 +8,7 @@
 #include <stdlib.h>
 #include <Eigen/Core>
 #include <string>
+#include "defs.h"
 
 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
 
@@ -93,12 +94,15 @@ unsigned next_power_of_two(unsigned v);
 // back into anything you intend to pass into OpenGL.
 void *get_gl_context_identifier();
 
+// Used in the check_error() macro, below.
+void abort_gl_error(GLenum err, const char *filename, int line) DOES_NOT_RETURN;
+
 }  // namespace movit
 
 #ifdef NDEBUG
 #define check_error()
 #else
-#define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at %s:%d\n", err, __FILE__, __LINE__); abort(); } }
+#define check_error() { GLenum err = glGetError(); if (err != GL_NO_ERROR) { movit::abort_gl_error(err, __FILE__, __LINE__); } }
 #endif
 
 // CHECK() is like assert(), but retains any side effects no matter the compilation mode.