OLD | NEW |
(Empty) | |
| 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 |
| 38 #ifndef nsDebug_h___ |
| 39 #define nsDebug_h___ |
| 40 |
| 41 #ifndef nscore_h___ |
| 42 #include "nscore.h" |
| 43 #endif |
| 44 |
| 45 #ifndef nsError_h__ |
| 46 #include "nsError.h" |
| 47 #endif |
| 48 |
| 49 #ifdef DEBUG |
| 50 #define NS_DEBUG |
| 51 #endif |
| 52 |
| 53 /** |
| 54 * Namespace for debugging methods. Note that your code must use the |
| 55 * macros defined later in this file so that the debug code can be |
| 56 * conditionally compiled out. |
| 57 */ |
| 58 |
| 59 PR_BEGIN_EXTERN_C |
| 60 |
| 61 /** |
| 62 * Log a warning message to the debug log. |
| 63 */ |
| 64 NS_COM_GLUE void NS_FASTCALL |
| 65 NSGlue_Warning(const char *aMessage, const char *aFile, PRIntn aLine); |
| 66 |
| 67 /** |
| 68 * Abort the executing program. This works on all architectures. |
| 69 */ |
| 70 NS_COM_GLUE void NS_FASTCALL |
| 71 NSGlue_Abort(const char *aFile, PRIntn aLine); |
| 72 |
| 73 /** |
| 74 * Break the executing program into the debugger. |
| 75 */ |
| 76 NS_COM_GLUE void NS_FASTCALL |
| 77 NSGlue_Break(const char* aFile, PRIntn aLine); |
| 78 |
| 79 /** |
| 80 * Log an assertion message to the debug log |
| 81 */ |
| 82 NS_COM_GLUE void NS_FASTCALL |
| 83 NSGlue_Assertion(const char* aStr, const char* aExpr, |
| 84 const char* aFile, PRIntn aLine); |
| 85 |
| 86 PR_END_EXTERN_C |
| 87 |
| 88 #ifdef DEBUG |
| 89 |
| 90 /** |
| 91 * Abort the execution of the program if the expression evaluates to |
| 92 * false. |
| 93 * |
| 94 * There is no status value returned from the macro. |
| 95 * |
| 96 * Note that the non-debug version of this macro does <b>not</b> |
| 97 * evaluate the expression argument. Hence side effect statements |
| 98 * as arguments to the macro will yield improper execution in a |
| 99 * non-debug build. For example: |
| 100 * |
| 101 * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero"); |
| 102 * |
| 103 * Note also that the non-debug version of this macro does <b>not</b> |
| 104 * evaluate the message argument. |
| 105 */ |
| 106 #define NS_ABORT_IF_FALSE(_expr, _msg) \ |
| 107 PR_BEGIN_MACRO \ |
| 108 if (!(_expr)) { \ |
| 109 NSGlue_Assertion(_msg, #_expr, __FILE__, __LINE__); \ |
| 110 } \ |
| 111 PR_END_MACRO |
| 112 |
| 113 /** |
| 114 * Warn if a given condition is false. |
| 115 * |
| 116 * Program execution continues past the usage of this macro. |
| 117 * |
| 118 * Note also that the non-debug version of this macro does <b>not</b> |
| 119 * evaluate the message argument. |
| 120 */ |
| 121 #define NS_WARN_IF_FALSE(_expr,_msg) \ |
| 122 PR_BEGIN_MACRO \ |
| 123 if (!(_expr)) { \ |
| 124 NSGlue_Assertion(_msg, #_expr, __FILE__, __LINE__); \ |
| 125 } \ |
| 126 PR_END_MACRO |
| 127 |
| 128 /** |
| 129 * Test a precondition for truth. If the expression is not true then |
| 130 * trigger a program failure. |
| 131 */ |
| 132 #define NS_PRECONDITION(expr, str) \ |
| 133 PR_BEGIN_MACRO \ |
| 134 if (!(expr)) { \ |
| 135 NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \ |
| 136 } \ |
| 137 PR_END_MACRO |
| 138 |
| 139 /** |
| 140 * Test an assertion for truth. If the expression is not true then |
| 141 * trigger a program failure. |
| 142 */ |
| 143 #define NS_ASSERTION(expr, str) \ |
| 144 PR_BEGIN_MACRO \ |
| 145 if (!(expr)) { \ |
| 146 NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \ |
| 147 } \ |
| 148 PR_END_MACRO |
| 149 |
| 150 /** |
| 151 * Test a post-condition for truth. If the expression is not true then |
| 152 * trigger a program failure. |
| 153 */ |
| 154 #define NS_POSTCONDITION(expr, str) \ |
| 155 PR_BEGIN_MACRO \ |
| 156 if (!(expr)) { \ |
| 157 NSGlue_Assertion(str, #expr, __FILE__, __LINE__); \ |
| 158 } \ |
| 159 PR_END_MACRO |
| 160 |
| 161 /** |
| 162 * This macros triggers a program failure if executed. It indicates that |
| 163 * an attempt was made to execute some unimplemented functionality. |
| 164 */ |
| 165 #define NS_NOTYETIMPLEMENTED(str) \ |
| 166 NSGlue_Assertion(str, "NotYetImplemented", __FILE__, __LINE__) |
| 167 |
| 168 /** |
| 169 * This macros triggers a program failure if executed. It indicates that |
| 170 * an attempt was made to execute some unimplemented functionality. |
| 171 */ |
| 172 #define NS_NOTREACHED(str) \ |
| 173 NSGlue_Assertion(str, "Not Reached", __FILE__, __LINE__) |
| 174 |
| 175 /** |
| 176 * Log an error message. |
| 177 */ |
| 178 #define NS_ERROR(str) \ |
| 179 NSGlue_Assertion(str, "Error", __FILE__, __LINE__) |
| 180 |
| 181 /** |
| 182 * Log a warning message. |
| 183 */ |
| 184 #define NS_WARNING(str) \ |
| 185 NSGlue_Warning(str, __FILE__, __LINE__) |
| 186 |
| 187 /** |
| 188 * Trigger an abort |
| 189 */ |
| 190 #define NS_ABORT() \ |
| 191 NSGlue_Abort(__FILE__, __LINE__) |
| 192 |
| 193 /** |
| 194 * Cause a break |
| 195 */ |
| 196 #define NS_BREAK() \ |
| 197 NSGlue_Break(__FILE__, __LINE__) |
| 198 |
| 199 #else /* NS_DEBUG */ |
| 200 |
| 201 /** |
| 202 * The non-debug version of these macros do not evaluate the |
| 203 * expression or the message arguments to the macro. |
| 204 */ |
| 205 #define NS_ABORT_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 206 #define NS_WARN_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 207 #define NS_PRECONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 208 #define NS_ASSERTION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 209 #define NS_POSTCONDITION(expr, str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 210 #define NS_NOTYETIMPLEMENTED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 211 #define NS_NOTREACHED(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 212 #define NS_ERROR(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 213 #define NS_WARNING(str) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 214 #define NS_ABORT() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 215 #define NS_BREAK() PR_BEGIN_MACRO /* nothing */ PR_END_MACRO |
| 216 |
| 217 #endif /* ! NS_DEBUG */ |
| 218 |
| 219 /* Macros for checking the trueness of an expression passed in within an |
| 220 * interface implementation. These need to be compiled regardless of the */ |
| 221 /* NS_DEBUG flag |
| 222 ******************************************************************************/ |
| 223 |
| 224 #define NS_ENSURE_TRUE(x, ret) \ |
| 225 PR_BEGIN_MACRO \ |
| 226 if (NS_UNLIKELY(!(x))) { \ |
| 227 NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \ |
| 228 return ret; \ |
| 229 } \ |
| 230 PR_END_MACRO |
| 231 |
| 232 #define NS_ENSURE_FALSE(x, ret) \ |
| 233 NS_ENSURE_TRUE(!(x), ret) |
| 234 |
| 235 /****************************************************************************** |
| 236 ** Macros for checking results |
| 237 ******************************************************************************/ |
| 238 |
| 239 #define NS_ENSURE_SUCCESS(res, ret) \ |
| 240 NS_ENSURE_TRUE(NS_SUCCEEDED(res), ret) |
| 241 |
| 242 /****************************************************************************** |
| 243 ** Macros for checking state and arguments upon entering interface boundaries |
| 244 ******************************************************************************/ |
| 245 |
| 246 #define NS_ENSURE_ARG(arg) \ |
| 247 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG) |
| 248 |
| 249 #define NS_ENSURE_ARG_POINTER(arg) \ |
| 250 NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER) |
| 251 |
| 252 #define NS_ENSURE_ARG_MIN(arg, min) \ |
| 253 NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG) |
| 254 |
| 255 #define NS_ENSURE_ARG_MAX(arg, max) \ |
| 256 NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG) |
| 257 |
| 258 #define NS_ENSURE_ARG_RANGE(arg, min, max) \ |
| 259 NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG) |
| 260 |
| 261 #define NS_ENSURE_STATE(state) \ |
| 262 NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED) |
| 263 |
| 264 #define NS_ENSURE_NO_AGGREGATION(outer) \ |
| 265 NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION) |
| 266 |
| 267 #define NS_ENSURE_PROPER_AGGREGATION(outer, iid) \ |
| 268 NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALI
D_ARG) |
| 269 |
| 270 /*****************************************************************************/ |
| 271 |
| 272 #ifdef XPCOM_GLUE |
| 273 #define NS_CheckThreadSafe |
| 274 #else |
| 275 #define NS_CheckThreadSafe(owningThread, msg) \ |
| 276 NS_ASSERTION(owningThread == PR_GetCurrentThread(), msg) |
| 277 #endif |
| 278 |
| 279 #endif /* nsDebug_h___ */ |
OLD | NEW |