Common.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GL/glu.h>
  4. #include "Common.h"
  5. #include "App.h"
  6. #include "Util.h"
  7. // for the colors and formating see http://www.dreamincode.net/forums/showtopic75171.htm
  8. static const char* terminalColors [MT_NUM + 1] = {
  9. "\033[1;31;6m", // error
  10. "\033[1;33;6m", // warning
  11. "\033[1;31;6m", // fatal
  12. "\033[1;31;6m", // dbg error
  13. "\033[1;32;6m", // info
  14. "\033[0;;m" // default color
  15. };
  16. //======================================================================================================================
  17. // msgPrefix =
  18. //======================================================================================================================
  19. ostream& msgPrefix(MsgType msgType, const char* file, int line, const char* func)
  20. {
  21. if(app == NULL)
  22. ::exit(1);
  23. // select c stream
  24. ostream* cs;
  25. switch(msgType)
  26. {
  27. case MT_ERROR:
  28. case MT_WARNING:
  29. case MT_FATAL:
  30. case MT_DEBUG_ERR:
  31. cs = &cerr;
  32. break;
  33. case MT_INFO:
  34. cs = &cout;
  35. break;
  36. default:
  37. break;
  38. }
  39. // print terminal color
  40. if(app->isTerminalColoringEnabled())
  41. {
  42. (*cs) << terminalColors[msgType];
  43. }
  44. // print message info
  45. switch(msgType)
  46. {
  47. case MT_ERROR:
  48. (*cs) << "Error";
  49. break;
  50. case MT_WARNING:
  51. (*cs) << "Warning";
  52. break;
  53. case MT_FATAL:
  54. (*cs) << "Fatal";
  55. break;
  56. case MT_DEBUG_ERR:
  57. (*cs) << "Debug Err";
  58. break;
  59. case MT_INFO:
  60. (*cs) << "Info";
  61. break;
  62. default:
  63. break;
  64. }
  65. // print caller info
  66. (*cs) << " (" << Util::cutPath(file) << ":" << line << " " << Util::getFunctionFromPrettyFunction(func) << "): ";
  67. return (*cs);
  68. }
  69. //======================================================================================================================
  70. // msgSuffix =
  71. //======================================================================================================================
  72. ostream& msgSuffix(ostream& cs)
  73. {
  74. if(app == NULL)
  75. ::exit(1);
  76. if(app->isTerminalColoringEnabled())
  77. cs << terminalColors[MT_NUM];
  78. cs << endl;
  79. return cs;
  80. }
  81. //======================================================================================================================
  82. // msgSuffixFatal =
  83. //======================================================================================================================
  84. ostream& msgSuffixFatal(ostream& cs)
  85. {
  86. msgSuffix(cs);
  87. ::exit(1);
  88. return cs;
  89. }
  90. //======================================================================================================================
  91. // msgGlError =
  92. //======================================================================================================================
  93. bool msgGlError(const char* file, int line, const char* func)
  94. {
  95. GLenum errId = glGetError();
  96. if(errId == GL_NO_ERROR) return true;
  97. msgPrefix(MT_ERROR, file, line, func) << "OpenGL Err: " << gluErrorString(errId) << msgSuffix;
  98. return false;
  99. }