darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 1 | /* |
| 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 |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 33 | #define KWQ_ASSERTIONS_DISABLED_DEFAULT 1 |
| 34 | #else |
| 35 | #define KWQ_ASSERTIONS_DISABLED_DEFAULT 0 |
| 36 | #endif |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 37 | |
| 38 | #ifndef ASSERT_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 39 | #define ASSERT_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #ifndef ASSERT_ARG_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 43 | #define ASSERT_ARG_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #ifndef FATAL_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 47 | #define FATAL_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | #ifndef ERROR_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 51 | #define ERROR_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #ifndef LOG_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 55 | #define LOG_DISABLED KWQ_ASSERTIONS_DISABLED_DEFAULT |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | // These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. |
| 59 | |
| 60 | #ifdef __cplusplus |
| 61 | extern "C" { |
| 62 | #endif |
| 63 | |
| 64 | typedef struct { |
| 65 | unsigned mask; |
| 66 | const char *defaultName; |
| 67 | enum { KWQLogChannelUninitialized, KWQLogChannelOff, KWQLogChannelOn } state; |
| 68 | } KWQLogChannel; |
| 69 | |
| 70 | void KWQReportAssertionFailure(const char *file, int line, const char *function, const char *assertion); |
| 71 | void KWQReportAssertionFailureWithMessage(const char *file, int line, const char *function, const char *assertion, const char *format, ...); |
| 72 | void KWQReportArgumentAssertionFailure(const char *file, int line, const char *function, const char *argName, const char *assertion); |
| 73 | void KWQReportFatalError(const char *file, int line, const char *function, const char *format, ...) ; |
| 74 | void KWQReportError(const char *file, int line, const char *function, const char *format, ...); |
| 75 | void 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) |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 86 | #define ASSERT_WITH_MESSAGE(assertion, formatAndArgs...) ((void)0) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 87 | #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 | } \ |
| 96 | while (0) |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 97 | #define ASSERT_WITH_MESSAGE(assertion, formatAndArgs...) do \ |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 98 | if (!(assertion)) { \ |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 99 | KWQReportAssertionFailureWithMessage(__FILE__, __LINE__, __PRETTY_FUNCTION__, #assertion, formatAndArgs); \ |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 100 | raise(SIGQUIT); \ |
| 101 | } \ |
| 102 | while (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 | } \ |
| 123 | while (0) |
| 124 | |
| 125 | #endif |
| 126 | |
| 127 | // FATAL |
| 128 | |
| 129 | #if FATAL_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 130 | #define FATAL(formatAndArgs...) ((void)0) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 131 | #else |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 132 | #define FATAL(formatAndArgs...) do { \ |
| 133 | KWQReportFatalError(__FILE__, __LINE__, __PRETTY_FUNCTION__, formatAndArgs); \ |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 134 | raise(SIGQUIT); \ |
| 135 | } while (0) |
| 136 | #endif |
| 137 | |
| 138 | // ERROR |
| 139 | |
| 140 | #if ERROR_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 141 | #define ERROR(formatAndArgs...) ((void)0) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 142 | #else |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 143 | #define ERROR(formatAndArgs...) KWQReportError(__FILE__, __LINE__, __PRETTY_FUNCTION__, formatAndArgs) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | // LOG |
| 147 | |
| 148 | #if LOG_DISABLED |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 149 | #define LOG(channel, formatAndArgs...) ((void)0) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 150 | #else |
darin | fc91b7c | 2002-09-11 23:23:14 +0000 | [diff] [blame^] | 151 | #define LOG(channel, formatAndArgs...) KWQLog(__FILE__, __LINE__, __PRETTY_FUNCTION__, &JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), formatAndArgs) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 152 | #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 |