Change Tizen Log macros
[framework/web/webkit-efl.git] / Source / WTF / wtf / Assertions.h
1 /*
2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #ifndef WTF_Assertions_h
27 #define WTF_Assertions_h
28
29 /*
30    no namespaces because this file has to be includable from C and Objective-C
31
32    Note, this file uses many GCC extensions, but it should be compatible with
33    C, Objective C, C++, and Objective C++.
34
35    For non-debug builds, everything is disabled by default.
36    Defining any of the symbols explicitly prevents this from having any effect.
37    
38    MSVC7 note: variadic macro support was added in MSVC8, so for now we disable
39    those macros in MSVC7. For more info, see the MSDN document on variadic 
40    macros here:
41    
42    http://msdn2.microsoft.com/en-us/library/ms177415(VS.80).aspx
43 */
44
45 #include <wtf/Platform.h>
46
47 #include <stddef.h>
48
49 #if !COMPILER(MSVC)
50 #include <inttypes.h>
51 #endif
52
53 #ifdef NDEBUG
54 /* Disable ASSERT* macros in release mode. */
55 #define ASSERTIONS_DISABLED_DEFAULT 1
56 #else
57 #define ASSERTIONS_DISABLED_DEFAULT 0
58 #endif
59
60 #if COMPILER(MSVC7_OR_LOWER)
61 #define HAVE_VARIADIC_MACRO 0
62 #else
63 #define HAVE_VARIADIC_MACRO 1
64 #endif
65
66 #ifndef BACKTRACE_DISABLED
67 #define BACKTRACE_DISABLED ASSERTIONS_DISABLED_DEFAULT
68 #endif
69
70 #ifndef ASSERT_DISABLED
71 #define ASSERT_DISABLED ASSERTIONS_DISABLED_DEFAULT
72 #endif
73
74 #ifndef ASSERT_MSG_DISABLED
75 #if HAVE(VARIADIC_MACRO)
76 #define ASSERT_MSG_DISABLED ASSERTIONS_DISABLED_DEFAULT
77 #else
78 #define ASSERT_MSG_DISABLED 1
79 #endif
80 #endif
81
82 #ifndef ASSERT_ARG_DISABLED
83 #define ASSERT_ARG_DISABLED ASSERTIONS_DISABLED_DEFAULT
84 #endif
85
86 #ifndef FATAL_DISABLED
87 #if HAVE(VARIADIC_MACRO)
88 #define FATAL_DISABLED ASSERTIONS_DISABLED_DEFAULT
89 #else
90 #define FATAL_DISABLED 1
91 #endif
92 #endif
93
94 #ifndef ERROR_DISABLED
95 #if HAVE(VARIADIC_MACRO)
96 #define ERROR_DISABLED ASSERTIONS_DISABLED_DEFAULT
97 #else
98 #define ERROR_DISABLED 1
99 #endif
100 #endif
101
102 #ifndef LOG_DISABLED
103 #if HAVE(VARIADIC_MACRO)
104 #define LOG_DISABLED ASSERTIONS_DISABLED_DEFAULT
105 #else
106 #define LOG_DISABLED 1
107 #endif
108 #endif
109
110 #if COMPILER(GCC)
111 #define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
112 #else
113 #define WTF_PRETTY_FUNCTION __FUNCTION__
114 #endif
115
116 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
117    emits a warning when %@ is used in the format string.  Until <rdar://problem/5195437> is resolved we can't include
118    the attribute when being used from Objective-C code in case it decides to use %@. */
119 #if COMPILER(GCC) && !defined(__OBJC__)
120 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
121 #else
122 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) 
123 #endif
124
125 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
126
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130
131 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
132
133 typedef struct {
134     unsigned mask;
135     const char *defaultName;
136     WTFLogChannelState state;
137 } WTFLogChannel;
138
139 WTF_EXPORT_PRIVATE void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
140 WTF_EXPORT_PRIVATE void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
141 WTF_EXPORT_PRIVATE void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
142 WTF_EXPORT_PRIVATE void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
143 WTF_EXPORT_PRIVATE void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
144 WTF_EXPORT_PRIVATE void WTFLog(WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
145 WTF_EXPORT_PRIVATE void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
146 WTF_EXPORT_PRIVATE void WTFLogAlways(const char* format, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
147
148 WTF_EXPORT_PRIVATE void WTFGetBacktrace(void** stack, int* size);
149 WTF_EXPORT_PRIVATE void WTFReportBacktrace();
150 WTF_EXPORT_PRIVATE void WTFPrintBacktrace(void** stack, int size);
151
152 typedef void (*WTFCrashHookFunction)();
153 WTF_EXPORT_PRIVATE void WTFSetCrashHook(WTFCrashHookFunction);
154 WTF_EXPORT_PRIVATE void WTFInvokeCrashHook();
155
156 #if ENABLE(TIZEN_DLOG_SUPPORT)
157 typedef enum {
158     TIZEN_DLOG_PRIORITY_ERROR,
159     TIZEN_DLOG_PRIORITY_INFO
160 } TizenDlogPriority;
161
162 WTF_EXPORT_PRIVATE void TizenLog(TizenDlogPriority priority, bool secureLog, int line, const char* function, const char* format, ...);
163 #endif
164
165 #ifdef __cplusplus
166 }
167 #endif
168
169 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
170
171    Use CRASH() in response to known, unrecoverable errors like out-of-memory.
172    Macro is enabled in both debug and release mode.
173    To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
174
175    Signals are ignored by the crash reporter on OS X so we must do better.
176 */
177 #ifndef CRASH
178 #if COMPILER(CLANG)
179 #define CRASH() \
180     (WTFReportBacktrace(), \
181      WTFInvokeCrashHook(), \
182      (*(int *)(uintptr_t)0xbbadbeef = 0), \
183      __builtin_trap())
184 #else
185 #define CRASH() \
186     (WTFReportBacktrace(), \
187      WTFInvokeCrashHook(), \
188      (*(int *)(uintptr_t)0xbbadbeef = 0), \
189      ((void(*)())0)() /* More reliable, but doesn't say BBADBEEF */ \
190     )
191 #endif
192 #endif
193
194 #if COMPILER(CLANG)
195 #define NO_RETURN_DUE_TO_CRASH NO_RETURN
196 #else
197 #define NO_RETURN_DUE_TO_CRASH
198 #endif
199
200
201 /* BACKTRACE
202
203   Print a backtrace to the same location as ASSERT messages.
204 */
205
206 #if BACKTRACE_DISABLED
207
208 #define BACKTRACE() ((void)0)
209
210 #else
211
212 #define BACKTRACE() do { \
213     WTFReportBacktrace(); \
214 } while(false)
215
216 #endif
217
218 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
219
220   These macros are compiled out of release builds.
221   Expressions inside them are evaluated in debug builds only.
222 */
223
224 #if OS(WINCE) && !PLATFORM(TORCHMOBILE)
225 /* FIXME: We include this here only to avoid a conflict with the ASSERT macro. */
226 #include <windows.h>
227 #undef min
228 #undef max
229 #undef ERROR
230 #endif
231
232 #if OS(WINDOWS)
233 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
234 #undef ASSERT
235 #endif
236
237 #if ASSERT_DISABLED
238
239 #define ASSERT(assertion) ((void)0)
240 #define ASSERT_AT(assertion, file, line, function) ((void)0)
241 #define ASSERT_NOT_REACHED() ((void)0)
242 #define NO_RETURN_DUE_TO_ASSERT
243
244 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
245 template<typename T>
246 inline void assertUnused(T& x) { (void)x; }
247 #define ASSERT_UNUSED(variable, assertion) (assertUnused(variable))
248 #else
249 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
250 #endif
251
252 #else
253
254 #define ASSERT(assertion) \
255     (!(assertion) ? \
256         (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
257          CRASH()) : \
258         (void)0)
259
260 #define ASSERT_AT(assertion, file, line, function) \
261     (!(assertion) ? \
262         (WTFReportAssertionFailure(file, line, function, #assertion), \
263          CRASH()) :                                                   \
264         (void)0)
265
266 #define ASSERT_NOT_REACHED() do { \
267     WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
268     CRASH(); \
269 } while (0)
270
271 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
272
273 #define NO_RETURN_DUE_TO_ASSERT NO_RETURN_DUE_TO_CRASH
274
275 #endif
276
277 /* ASSERT_WITH_MESSAGE */
278
279 #if COMPILER(MSVC7_OR_LOWER)
280 #define ASSERT_WITH_MESSAGE(assertion) ((void)0)
281 #elif ASSERT_MSG_DISABLED
282 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
283 #else
284 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
285     if (!(assertion)) { \
286         WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
287         CRASH(); \
288     } \
289 while (0)
290 #endif
291
292 /* ASSERT_WITH_MESSAGE_UNUSED */
293
294 #if COMPILER(MSVC7_OR_LOWER)
295 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion) ((void)0)
296 #elif ASSERT_MSG_DISABLED
297 #if COMPILER(INTEL) && !OS(WINDOWS) || COMPILER(RVCT)
298 template<typename T>
299 inline void assertWithMessageUnused(T& x) { (void)x; }
300 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) (assertWithMessageUnused(variable))
301 #else
302 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
303 #endif
304 #else
305 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
306     if (!(assertion)) { \
307         WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
308         CRASH(); \
309     } \
310 while (0)
311 #endif
312                         
313                         
314 /* ASSERT_ARG */
315
316 #if ASSERT_ARG_DISABLED
317
318 #define ASSERT_ARG(argName, assertion) ((void)0)
319
320 #else
321
322 #define ASSERT_ARG(argName, assertion) do \
323     if (!(assertion)) { \
324         WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
325         CRASH(); \
326     } \
327 while (0)
328
329 #endif
330
331 /* COMPILE_ASSERT */
332 #ifndef COMPILE_ASSERT
333 #if COMPILER_SUPPORTS(C_STATIC_ASSERT)
334 #define COMPILE_ASSERT(exp, name) _Static_assert((exp), #name)
335 #else
336 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
337 #endif
338 #endif
339
340 /* FATAL */
341
342 #if COMPILER(MSVC7_OR_LOWER)
343 #define FATAL() ((void)0)
344 #elif FATAL_DISABLED
345 #define FATAL(...) ((void)0)
346 #else
347 #define FATAL(...) do { \
348     WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
349     CRASH(); \
350 } while (0)
351 #endif
352
353 /* LOG_ERROR */
354
355 #if COMPILER(MSVC7_OR_LOWER)
356 #define LOG_ERROR() ((void)0)
357 #elif ERROR_DISABLED
358 #define LOG_ERROR(...) ((void)0)
359 #else
360 #define LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
361 #endif
362
363 /* LOG */
364
365 #if COMPILER(MSVC7_OR_LOWER)
366 #define LOG() ((void)0)
367 #elif LOG_DISABLED
368 #define LOG(channel, ...) ((void)0)
369 #else
370 #if ENABLE(TIZEN_DLOG_SUPPORT)
371 // Use below macros to print out WebKit debug log.
372 #define TIZEN_LOGE(...) TizenLog(TIZEN_DLOG_PRIORITY_ERROR, false, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
373 #define TIZEN_LOGI(...) TizenLog(TIZEN_DLOG_PRIORITY_INFO, false, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
374
375 // Use below macros to print out secure log.
376 #define TIZEN_SECURE_LOGE(...) TizenLog(TIZEN_DLOG_PRIORITY_ERROR, true, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
377 #define TIZEN_SECURE_LOGI(...) TizenLog(TIZEN_DLOG_PRIORITY_INFO, true, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__);
378
379 #define LOG(channel, ...) LOG_VERBOSE(channel, __VA_ARGS__)
380 #else
381 #define LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
382 #endif
383 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
384 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
385 #endif
386
387 /* LOG_VERBOSE */
388
389 #if COMPILER(MSVC7_OR_LOWER)
390 #define LOG_VERBOSE(channel) ((void)0)
391 #elif LOG_DISABLED
392 #define LOG_VERBOSE(channel, ...) ((void)0)
393 #else
394 #define LOG_VERBOSE(channel, ...) WTFLogVerbose(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
395 #endif
396
397 /* UNREACHABLE_FOR_PLATFORM */
398
399 #if COMPILER(CLANG)
400 // This would be a macro except that its use of #pragma works best around
401 // a function. Hence it uses macro naming convention.
402 #pragma clang diagnostic push
403 #pragma clang diagnostic ignored "-Wmissing-noreturn"
404 static inline void UNREACHABLE_FOR_PLATFORM()
405 {
406     ASSERT_NOT_REACHED();
407 }
408 #pragma clang diagnostic pop
409 #else
410 #define UNREACHABLE_FOR_PLATFORM() ASSERT_NOT_REACHED()
411 #endif
412
413 #endif /* WTF_Assertions_h */