nsDebug.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either of the GNU General Public License Version 2 or later (the "GPL"),
  26. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsDebug_h___
  38. #define nsDebug_h___
  39. #ifndef nscore_h___
  40. #include "nscore.h"
  41. #endif
  42. #ifndef nsError_h__
  43. #include "nsError.h"
  44. #endif
  45. #ifdef DEBUG
  46. #define NS_DEBUG
  47. #endif
  48. /**
  49. * Namespace for debugging methods. Note that your code must use the
  50. * macros defined later in this file so that the debug code can be
  51. * conditionally compiled out.
  52. */
  53. PR_BEGIN_EXTERN_C
  54. /**
  55. * Log a warning message to the debug log.
  56. */
  57. NS_COM_GLUE void NS_FASTCALL
  58. NSGlue_Warning(const char *aMessage, const char *aFile, PRIntn aLine);
  59. /**
  60. * Abort the executing program. This works on all architectures.
  61. */
  62. NS_COM_GLUE void NS_FASTCALL
  63. NSGlue_Abort(const char *aFile, PRIntn aLine);
  64. /**
  65. * Break the executing program into the debugger.
  66. */
  67. NS_COM_GLUE void NS_FASTCALL
  68. NSGlue_Break(const char* aFile, PRIntn aLine);
  69. /**
  70. * Log an assertion message to the debug log
  71. */
  72. NS_COM_GLUE void NS_FASTCALL
  73. NSGlue_Assertion(const char* aStr, const char* aExpr,
  74. const char* aFile, PRIntn aLine);
  75. PR_END_EXTERN_C
  76. #ifdef DEBUG
  77. /**
  78. * Abort the execution of the program if the expression evaluates to
  79. * false.
  80. *
  81. * There is no status value returned from the macro.
  82. *
  83. * Note that the non-debug version of this macro does <b>not</b>
  84. * evaluate the expression argument. Hence side effect statements
  85. * as arguments to the macro will yield improper execution in a
  86. * non-debug build. For example:
  87. *
  88. * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero");
  89. *
  90. * Note also that the non-debug version of this macro does <b>not</b>
  91. * evaluate the message argument.
  92. */
  93. #define NS_ABORT_IF_FALSE(_expr, _msg) \
  94. PR_BEGIN_MACRO \
  95. if (!(_expr)) { \
  96. NSGlue_Assertion(_msg, #_expr, __FILE__, __LINE__); \
  97. } \
  98. PR_END_MACRO
  99. /**
  100. * Warn if a given condition is false.
  101. *
  102. * Program execution continues past the usage of this macro.
  103. *
  104. * Note also that the non-debug version of this macro does <b>not</b>
  105. * evaluate the message argument.
  106. */
  107. #define NS_WARN_IF_FALSE(_expr,_msg) \
  108. PR_BEGIN_MACRO \
  109. if (!(_expr)) { \
  110. NSGlue_Assertion(_msg, #_expr, __FILE__, __LINE__); \
  111. } \
  112. PR_END_MACRO
  113. /**
  114. * Test a precondition for truth. If the expression is not true then
  115. * trigger a program failure.
  116. */
  117. #define NS_PRECONDITION(expr, str) \
  118. PR_BEGIN_MACRO \
  119. if (!(expr)) { \
  120. NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \
  121. } \
  122. PR_END_MACRO
  123. /**
  124. * Test an assertion for truth. If the expression is not true then
  125. * trigger a program failure.
  126. */
  127. #define NS_ASSERTION(expr, str) \
  128. PR_BEGIN_MACRO \
  129. if (!(expr)) { \
  130. NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \
  131. } \
  132. PR_END_MACRO
  133. /**
  134. * Test a post-condition for truth. If the expression is not true then
  135. * trigger a program failure.
  136. */
  137. #define NS_POSTCONDITION(expr, str) \
  138. PR_BEGIN_MACRO \
  139. if (!(expr)) { \
  140. NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \
  141. } \
  142. PR_END_MACRO
  143. /**
  144. * This macros triggers a program failure if executed. It indicates that
  145. * an attempt was made to execute some unimplemented functionality.
  146. */
  147. #define NS_NOTYETIMPLEMENTED(str) \
  148. NSGlue_Assertion(str, "NotYetImplemented", __FILE__, __LINE__)
  149. /**
  150. * This macros triggers a program failure if executed. It indicates that
  151. * an attempt was made to execute some unimplemented functionality.
  152. */
  153. #define NS_NOTREACHED(str) \
  154. NSGlue_Assertion(str, "Not Reached", __FILE__, __LINE__)
  155. /**
  156. * Log an error message.
  157. */
  158. #define NS_ERROR(str) \
  159. NSGlue_Assertion(str, "Error", __FILE__, __LINE__)
  160. /**
  161. * Log a warning message.
  162. */
  163. #define NS_WARNING(str) \
  164. NSGlue_Warning(str, __FILE__, __LINE__)
  165. /**
  166. * Trigger an abort
  167. */
  168. #define NS_ABORT() \
  169. NSGlue_Abort(__FILE__, __LINE__)
  170. /**
  171. * Cause a break
  172. */
  173. #define NS_BREAK() \
  174. NSGlue_Break(__FILE__, __LINE__)
  175. #else /* NS_DEBUG */
  176. /**
  177. * The non-debug version of these macros do not evaluate the
  178. * expression or the message arguments to the macro.
  179. */
  180. #define NS_ABORT_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  181. #define NS_WARN_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  182. #define NS_PRECONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  183. #define NS_ASSERTION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  184. #define NS_POSTCONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  185. #define NS_NOTYETIMPLEMENTED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  186. #define NS_NOTREACHED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  187. #define NS_ERROR(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  188. #define NS_WARNING(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  189. #define NS_ABORT() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  190. #define NS_BREAK() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  191. #endif /* ! NS_DEBUG */
  192. /* Macros for checking the trueness of an expression passed in within an
  193. * interface implementation. These need to be compiled regardless of the */
  194. /* NS_DEBUG flag
  195. ******************************************************************************/
  196. #define NS_ENSURE_TRUE(x, ret) \
  197. PR_BEGIN_MACRO \
  198. if (NS_UNLIKELY(!(x))) { \
  199. NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \
  200. return ret; \
  201. } \
  202. PR_END_MACRO
  203. #define NS_ENSURE_FALSE(x, ret) \
  204. NS_ENSURE_TRUE(!(x), ret)
  205. /******************************************************************************
  206. ** Macros for checking results
  207. ******************************************************************************/
  208. #define NS_ENSURE_SUCCESS(res, ret) \
  209. NS_ENSURE_TRUE(NS_SUCCEEDED(res), ret)
  210. /******************************************************************************
  211. ** Macros for checking state and arguments upon entering interface boundaries
  212. ******************************************************************************/
  213. #define NS_ENSURE_ARG(arg) \
  214. NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)
  215. #define NS_ENSURE_ARG_POINTER(arg) \
  216. NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER)
  217. #define NS_ENSURE_ARG_MIN(arg, min) \
  218. NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG)
  219. #define NS_ENSURE_ARG_MAX(arg, max) \
  220. NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG)
  221. #define NS_ENSURE_ARG_RANGE(arg, min, max) \
  222. NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG)
  223. #define NS_ENSURE_STATE(state) \
  224. NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED)
  225. #define NS_ENSURE_NO_AGGREGATION(outer) \
  226. NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION)
  227. #define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \
  228. NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG)
  229. /*****************************************************************************/
  230. #ifdef XPCOM_GLUE
  231. #define NS_CheckThreadSafe
  232. #else
  233. #define NS_CheckThreadSafe(owningThread, msg) \
  234. NS_ASSERTION(owningThread == PR_GetCurrentThread(), msg)
  235. #endif
  236. #endif /* nsDebug_h___ */