PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/error.h

http://github.com/sofian/qualia
C Header | 131 lines | 54 code | 26 blank | 51 comment | 0 complexity | 1757a7ca2f2d4a3fe598888bbafdf304 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * error.h
  3. *
  4. * Error/warning/notification routines/macros.
  5. *
  6. * This file is part of Qualia https://github.com/sofian/qualia
  7. *
  8. * (c) 2011 Sofian Audry -- info(@)sofianaudry(.)com
  9. * This file was originally part of Drone (http://drone.ws)
  10. * (c) 2004 Mathieu Guindon, Julien Keable, Jean-Sebastien Senecal
  11. * Error messages code adapted from Torch
  12. * (c) 2004 Ronan Collobert (collober@idiap.ch)
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #ifndef ERROR_INCLUDED
  28. #define ERROR_INCLUDED
  29. #include <stdarg.h>
  30. // # Macro definitions #####################################################
  31. // Debug levels.
  32. //! No debug.
  33. #define DEBUG_LEVEL_NODEBUG -1
  34. //! Only catch errors.
  35. #define DEBUG_LEVEL_ERROR 0
  36. //! Catch both errors and warnings.
  37. #define DEBUG_LEVEL_WARNING 1
  38. //! Catch errors, warnings and notices.
  39. #define DEBUG_LEVEL_NOTICE 2
  40. // Default debug level.
  41. #ifndef DEBUG_LEVEL
  42. #define DEBUG_LEVEL DEBUG_LEVEL_ERROR
  43. #endif
  44. // Quick condition checkers.
  45. #define DEBUG_ERROR DEBUG_LEVEL >= DEBUG_LEVEL_ERROR
  46. #define DEBUG_WARNING DEBUG_LEVEL >= DEBUG_LEVEL_WARNING
  47. #define DEBUG_NOTICE DEBUG_LEVEL >= DEBUG_LEVEL_NOTICE
  48. // Redefine __STRING just to make sure.
  49. #ifndef __STRING
  50. #define __STRING(x) #x
  51. #endif
  52. // This macro is absolutely NOT SAFE! NEVER USE IT ALONE!!!
  53. // Use ASSERT_ERROR, ASSERT_WARNING and ASSERT_NOTICE instead, below.
  54. #define __TRIGGER_ASSERT(expr, func) \
  55. ((expr) ? static_cast<void>(0) : func("fail: " __STRING(expr)) );
  56. #define __DUMMY_ASSERT (static_cast<void>(0));
  57. //#define dummymsg static_cast<void>(0);
  58. // # Message functions #####################################################
  59. // Dummy methods (used for empty macros, see up there).
  60. inline void dummymsg(const char* , ...) {}
  61. inline void assertdummymsg(bool, const char* , ...) {}
  62. //! Prints a message.
  63. void message(const char* msg, ...);
  64. //! Like printf.
  65. void print(const char* msg, ...);
  66. //! Prints an error message. The program will exit.
  67. void errormsg(const char* msg, ...);
  68. //! Prints a warning message. The program will not.
  69. void warningmsg(const char* msg, ...);
  70. //! Prints a notice, usually intended for deep information at the programmer's intent.
  71. void noticemsg(const char* msg, ...);
  72. // # Assertions and messages functions #####################################
  73. //! Error messages/assertion.
  74. #if DEBUG_ERROR
  75. // XXX This was conflicting...
  76. //#define ERROR errormsg
  77. #define error errormsg
  78. void ASSERT_ERROR_MESSAGE(bool expr, const char* msg, ...);
  79. #define ASSERT_ERROR(expr) __TRIGGER_ASSERT(expr, errormsg)
  80. #else
  81. #define error dummymsg
  82. #define ASSERT_ERROR(expr) __DUMMY_ASSERT
  83. #define ASSERT_ERROR_MESSAGE assertdummymsg
  84. #endif
  85. //! Warning messages/assertion.
  86. #if DEBUG_WARNING
  87. #define WARNING warningmsg
  88. void ASSERT_WARNING_MESSAGE(bool expr, const char* msg, ...);
  89. #define ASSERT_WARNING(expr) __TRIGGER_ASSERT(expr, warningmsg)
  90. #else
  91. #define WARNING dummymsg
  92. #define ASSERT_WARNING(expr) __DUMMY_ASSERT
  93. #define ASSERT_WARNING_MESSAGE assertdummymsg
  94. #endif
  95. //! Notice messages/assertion.
  96. #if DEBUG_NOTICE
  97. #define NOTICE noticemsg
  98. void ASSERT_NOTICE_MESSAGE(bool expr, const char* msg, ...);
  99. #define ASSERT_NOTICE(expr) __TRIGGER_ASSERT(expr, noticemsg)
  100. #else
  101. #define NOTICE dummymsg
  102. #define ASSERT_NOTICE(expr) __DUMMY_ASSERT
  103. #define ASSERT_NOTICE_MESSAGE assertdummymsg
  104. #endif
  105. #endif