PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/nsDebug.h

http://firefox-mac-pdf.googlecode.com/
C Header | 280 lines | 123 code | 45 blank | 112 comment | 11 complexity | f3ae305b089b4d66621ccc9257d4a0b1 MD5 | raw file
  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. #include "nsXPCOM.h"
  46. #ifdef DEBUG
  47. #define NS_DEBUG
  48. #include "prprf.h"
  49. #endif
  50. #ifdef DEBUG
  51. /**
  52. * Abort the execution of the program if the expression evaluates to
  53. * false.
  54. *
  55. * There is no status value returned from the macro.
  56. *
  57. * Note that the non-debug version of this macro does <b>not</b>
  58. * evaluate the expression argument. Hence side effect statements
  59. * as arguments to the macro will yield improper execution in a
  60. * non-debug build. For example:
  61. *
  62. * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero");
  63. *
  64. * Note also that the non-debug version of this macro does <b>not</b>
  65. * evaluate the message argument.
  66. */
  67. #define NS_ABORT_IF_FALSE(_expr, _msg) \
  68. PR_BEGIN_MACRO \
  69. if (!(_expr)) { \
  70. NS_DebugBreak(NS_DEBUG_ASSERTION, _msg, #_expr, __FILE__, __LINE__); \
  71. } \
  72. PR_END_MACRO
  73. /**
  74. * Warn if a given condition is false.
  75. *
  76. * Program execution continues past the usage of this macro.
  77. *
  78. * Note also that the non-debug version of this macro does <b>not</b>
  79. * evaluate the message argument.
  80. */
  81. #define NS_WARN_IF_FALSE(_expr,_msg) \
  82. PR_BEGIN_MACRO \
  83. if (!(_expr)) { \
  84. NS_DebugBreak(NS_DEBUG_WARNING, _msg, #_expr, __FILE__, __LINE__); \
  85. } \
  86. PR_END_MACRO
  87. /**
  88. * Test a precondition for truth. If the expression is not true then
  89. * trigger a program failure.
  90. */
  91. #define NS_PRECONDITION(expr, str) \
  92. PR_BEGIN_MACRO \
  93. if (!(expr)) { \
  94. NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
  95. } \
  96. PR_END_MACRO
  97. /**
  98. * Test an assertion for truth. If the expression is not true then
  99. * trigger a program failure.
  100. */
  101. #define NS_ASSERTION(expr, str) \
  102. PR_BEGIN_MACRO \
  103. if (!(expr)) { \
  104. NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
  105. } \
  106. PR_END_MACRO
  107. /**
  108. * Test a post-condition for truth. If the expression is not true then
  109. * trigger a program failure.
  110. */
  111. #define NS_POSTCONDITION(expr, str) \
  112. PR_BEGIN_MACRO \
  113. if (!(expr)) { \
  114. NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \
  115. } \
  116. PR_END_MACRO
  117. /**
  118. * This macros triggers a program failure if executed. It indicates that
  119. * an attempt was made to execute some unimplemented functionality.
  120. */
  121. #define NS_NOTYETIMPLEMENTED(str) \
  122. NS_DebugBreak(NS_DEBUG_ASSERTION, str, "NotYetImplemented", __FILE__, __LINE__)
  123. /**
  124. * This macros triggers a program failure if executed. It indicates that
  125. * an attempt was made to execute some unimplemented functionality.
  126. */
  127. #define NS_NOTREACHED(str) \
  128. NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Not Reached", __FILE__, __LINE__)
  129. /**
  130. * Log an error message.
  131. */
  132. #define NS_ERROR(str) \
  133. NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Error", __FILE__, __LINE__)
  134. /**
  135. * Log a warning message.
  136. */
  137. #define NS_WARNING(str) \
  138. NS_DebugBreak(NS_DEBUG_WARNING, str, nsnull, __FILE__, __LINE__)
  139. /**
  140. * Trigger an abort
  141. */
  142. #define NS_ABORT() \
  143. NS_DebugBreak(NS_DEBUG_ABORT, nsnull, nsnull, __FILE__, __LINE__)
  144. /**
  145. * Cause a break
  146. */
  147. #define NS_BREAK() \
  148. NS_DebugBreak(NS_DEBUG_BREAK, nsnull, nsnull, __FILE__, __LINE__)
  149. #else /* NS_DEBUG */
  150. /**
  151. * The non-debug version of these macros do not evaluate the
  152. * expression or the message arguments to the macro.
  153. */
  154. #define NS_ABORT_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  155. #define NS_WARN_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  156. #define NS_PRECONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  157. #define NS_ASSERTION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  158. #define NS_POSTCONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  159. #define NS_NOTYETIMPLEMENTED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  160. #define NS_NOTREACHED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  161. #define NS_ERROR(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  162. #define NS_WARNING(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  163. #define NS_ABORT() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  164. #define NS_BREAK() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
  165. #endif /* ! NS_DEBUG */
  166. /* Macros for checking the trueness of an expression passed in within an
  167. * interface implementation. These need to be compiled regardless of the */
  168. /* NS_DEBUG flag
  169. ******************************************************************************/
  170. #define NS_ENSURE_TRUE(x, ret) \
  171. PR_BEGIN_MACRO \
  172. if (NS_UNLIKELY(!(x))) { \
  173. NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \
  174. return ret; \
  175. } \
  176. PR_END_MACRO
  177. #define NS_ENSURE_FALSE(x, ret) \
  178. NS_ENSURE_TRUE(!(x), ret)
  179. /******************************************************************************
  180. ** Macros for checking results
  181. ******************************************************************************/
  182. #if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR)
  183. #define NS_ENSURE_SUCCESS_BODY(res, ret) \
  184. char *msg = PR_smprintf("NS_ENSURE_SUCCESS(%s, %s) failed with " \
  185. "result 0x%X", #res, #ret, __rv); \
  186. NS_WARNING(msg); \
  187. PR_smprintf_free(msg);
  188. #else
  189. #define NS_ENSURE_SUCCESS_BODY(res, ret) \
  190. NS_WARNING("NS_ENSURE_SUCCESS(" #res ", " #ret ") failed");
  191. #endif
  192. #define NS_ENSURE_SUCCESS(res, ret) \
  193. PR_BEGIN_MACRO \
  194. nsresult __rv = res; /* Don't evaluate |res| more than once */ \
  195. if (NS_FAILED(__rv)) { \
  196. NS_ENSURE_SUCCESS_BODY(res, ret) \
  197. return ret; \
  198. } \
  199. PR_END_MACRO
  200. /******************************************************************************
  201. ** Macros for checking state and arguments upon entering interface boundaries
  202. ******************************************************************************/
  203. #define NS_ENSURE_ARG(arg) \
  204. NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)
  205. #define NS_ENSURE_ARG_POINTER(arg) \
  206. NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER)
  207. #define NS_ENSURE_ARG_MIN(arg, min) \
  208. NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG)
  209. #define NS_ENSURE_ARG_MAX(arg, max) \
  210. NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG)
  211. #define NS_ENSURE_ARG_RANGE(arg, min, max) \
  212. NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG)
  213. #define NS_ENSURE_STATE(state) \
  214. NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED)
  215. #define NS_ENSURE_NO_AGGREGATION(outer) \
  216. NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION)
  217. #define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \
  218. NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG)
  219. /*****************************************************************************/
  220. #ifdef XPCOM_GLUE
  221. #define NS_CheckThreadSafe
  222. #else
  223. #define NS_CheckThreadSafe(owningThread, msg) \
  224. NS_ASSERTION(owningThread == PR_GetCurrentThread(), msg)
  225. #endif
  226. /* When compiling the XPCOM Glue on Windows, we pretend that it's going to
  227. * be linked with a static CRT (-MT) even when it's not. This means that we
  228. * cannot link to data exports from the CRT, only function exports. So,
  229. * instead of referencing "stderr" directly, use fdopen.
  230. */
  231. PR_BEGIN_EXTERN_C
  232. NS_COM_GLUE void
  233. printf_stderr(const char *fmt, ...);
  234. PR_END_EXTERN_C
  235. #endif /* nsDebug_h___ */