blob: 187c55a1f7038e67ec4c4302340c14889bd1ac21 [file] [log] [blame]
darin7e7b97b2002-09-09 06:11:13 +00001/*
2 * Copyright (C) 2002 Apple Computer, 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// Note, this file uses many GCC extensions, but it should be compatible with
27// C, Objective C, C++, and Objective C++.
28
29// For non-debug builds, everything is disabled by default.
30// Defining any of the symbols explicitly prevents this from having any effect.
31
32#ifdef NDEBUG
darinfc91b7c2002-09-11 23:23:14 +000033#define KWQ_ASSERTIONS_DISABLED_DEFAULT 1
34#else
35#define KWQ_ASSERTIONS_DISABLED_DEFAULT 0
36#endif
darin7e7b97b2002-09-09 06:11:13 +000037
38#ifndef ASSERT_DISABLED
darinfc91b7c2002-09-11 23:23:14 +000039#define ASSERT_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000040#endif
41
42#ifndef ASSERT_ARG_DISABLED
darinfc91b7c2002-09-11 23:23:14 +000043#define ASSERT_ARG_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000044#endif
45
46#ifndef FATAL_DISABLED
darinfc91b7c2002-09-11 23:23:14 +000047#define FATAL_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000048#endif
49
50#ifndef ERROR_DISABLED
darinfc91b7c2002-09-11 23:23:14 +000051#define ERROR_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000052#endif
53
54#ifndef LOG_DISABLED
darinfc91b7c2002-09-11 23:23:14 +000055#define LOG_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT
darin7e7b97b2002-09-09 06:11:13 +000056#endif
57
58// These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled.
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64typedef struct {
65 unsigned mask;
66 const char *defaultName;
67 enum { KWQLogChannelUninitialized, KWQLogChannelOff, KWQLogChannelOn } state;
68} KWQLogChannel;
69
70void KWQReportAssertionFailure(const char *file, int line, const char *function, const char *assertion);
71void KWQReportAssertionFailureWithMessage(const char *file, int line, const char *function, const char *assertion, const char *format, ...);
72void KWQReportArgumentAssertionFailure(const char *file, int line, const char *function, const char *argName, const char *assertion);
73void KWQReportFatalError(const char *file, int line, const char *function, const char *format, ...) ;
74void KWQReportError(const char *file, int line, const char *function, const char *format, ...);
75void KWQLog(const char *file, int line, const char *function, KWQLogChannel *channel, const char *format, ...);
76
77#ifdef __cplusplus
78}
79#endif
80
81// ASSERT, ASSERT_WITH_MESSAGE, ASSERT_NOT_REACHED
82
83#if ASSERT_DISABLED
84
85#define ASSERT(assertion) ((void)0)
darinfc91b7c2002-09-11 23:23:14 +000086#define ASSERT_WITH_MESSAGE(assertion, formatAndArgs...) ((void)0)
darin7e7b97b2002-09-09 06:11:13 +000087#define ASSERT_NOT_REACHED() ((void)0)
88
89#else
90
91#define ASSERT(assertion) do \
92 if (!(assertion)) { \
93 KWQReportAssertionFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__, #assertion); \
94 raise(SIGQUIT); \
95 } \
96while (0)
darinfc91b7c2002-09-11 23:23:14 +000097#define ASSERT_WITH_MESSAGE(assertion, formatAndArgs...) do \
darin7e7b97b2002-09-09 06:11:13 +000098 if (!(assertion)) { \
darinfc91b7c2002-09-11 23:23:14 +000099 KWQReportAssertionFailureWithMessage(__FILE__, __LINE__, __PRETTY_FUNCTION__, #assertion, formatAndArgs); \
darin7e7b97b2002-09-09 06:11:13 +0000100 raise(SIGQUIT); \
101 } \
102while (0)
103#define ASSERT_NOT_REACHED() do { \
104 KWQReportAssertionFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__, 0); \
105 raise(SIGQUIT); \
106} while (0)
107
108#endif
109
110// ASSERT_ARG
111
112#if ASSERT_ARG_DISABLED
113
114#define ASSERT_ARG(argName, assertion) ((void)0)
115
116#else
117
118#define ASSERT_ARG(argName, assertion) do \
119 if (!(assertion)) { \
120 KWQReportArgumentAssertionFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__, #argName, #assertion); \
121 raise(SIGQUIT); \
122 } \
123while (0)
124
125#endif
126
127// FATAL
128
129#if FATAL_DISABLED
darinfc91b7c2002-09-11 23:23:14 +0000130#define FATAL(formatAndArgs...) ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000131#else
darinfc91b7c2002-09-11 23:23:14 +0000132#define FATAL(formatAndArgs...) do { \
133 KWQReportFatalError(__FILE__, __LINE__, __PRETTY_FUNCTION__, formatAndArgs); \
darin7e7b97b2002-09-09 06:11:13 +0000134 raise(SIGQUIT); \
135} while (0)
136#endif
137
138// ERROR
139
140#if ERROR_DISABLED
darinfc91b7c2002-09-11 23:23:14 +0000141#define ERROR(formatAndArgs...) ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000142#else
darinfc91b7c2002-09-11 23:23:14 +0000143#define ERROR(formatAndArgs...) KWQReportError(__FILE__, __LINE__, __PRETTY_FUNCTION__, formatAndArgs)
darin7e7b97b2002-09-09 06:11:13 +0000144#endif
145
146// LOG
147
148#if LOG_DISABLED
darinfc91b7c2002-09-11 23:23:14 +0000149#define LOG(channel, formatAndArgs...) ((void)0)
darin7e7b97b2002-09-09 06:11:13 +0000150#else
darinfc91b7c2002-09-11 23:23:14 +0000151#define LOG(channel, formatAndArgs...) KWQLog(__FILE__, __LINE__, __PRETTY_FUNCTION__, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), formatAndArgs)
darin7e7b97b2002-09-09 06:11:13 +0000152#define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
153#define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
154#endif