VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/glue/nsDebug.h@ 102210

Last change on this file since 102210 was 102210, checked in by vboxsync, 13 months ago

libs/xpcom/xpcom: Get rid of PR_GetCurrentThread(), bugref:10545

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette