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