mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 1 | // -*- mode: c++; c-basic-offset: 4 -*- |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 2 | /* |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
mjs | b64c50a | 2005-10-03 21:13:12 +0000 | [diff] [blame] | 27 | #include "config.h" |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 28 | #include "Assertions.h" |
darin | fb4d937 | 2006-04-22 05:31:57 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <string.h> |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 32 | |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 33 | #if PLATFORM(MAC) |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 34 | #include <CoreFoundation/CFString.h> |
| 35 | #endif |
| 36 | |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 37 | #if PLATFORM(WIN) |
| 38 | #define WINVER 0x0500 |
| 39 | #define _WIN32_WINNT 0x0500 |
| 40 | #include <windows.h> |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 41 | #include <crtdbg.h> |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 44 | extern "C" { |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 45 | |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 46 | // This is to work around the "you should use a printf format attribute" warning on GCC |
| 47 | // We can't use _attribute__ ((format (printf, 2, 3))) since we allow %@ |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 48 | static int (* vfprintf_no_warning)(FILE *, const char*, va_list) = vfprintf; |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 49 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 50 | static void vprintf_stderr_common(const char* format, va_list args) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 51 | { |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 52 | #if PLATFORM(MAC) |
tomernic | dbff46d | 2006-05-01 20:49:48 +0000 | [diff] [blame] | 53 | if (strstr(format, "%@")) { |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 54 | CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); |
| 55 | CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args); |
| 56 | |
| 57 | int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 58 | char* buffer = (char*)malloc(length + 1); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 59 | |
| 60 | CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8); |
| 61 | |
| 62 | fputs(buffer, stderr); |
| 63 | |
| 64 | free(buffer); |
| 65 | CFRelease(str); |
| 66 | CFRelease(cfFormat); |
| 67 | } else |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 68 | #elif PLATFORM(WIN) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 69 | if (IsDebuggerPresent()) { |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 70 | size_t size = 1024; |
| 71 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 72 | do { |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 73 | char* buffer = (char*)malloc(size); |
| 74 | |
| 75 | if (buffer == NULL) |
| 76 | break; |
| 77 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 78 | if (_vsnprintf(buffer, size, format, args) != -1) { |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 79 | OutputDebugStringA(buffer); |
| 80 | free(buffer); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | free(buffer); |
| 85 | size *= 2; |
| 86 | } while (size > 1024); |
| 87 | } else |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 88 | #endif |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 89 | vfprintf_no_warning(stderr, format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 92 | static void printf_stderr_common(const char* format, ...) |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 93 | { |
| 94 | va_list args; |
| 95 | va_start(args, format); |
| 96 | vprintf_stderr_common(format, args); |
| 97 | va_end(args); |
| 98 | } |
| 99 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 100 | static void printCallSite(const char* file, int line, const char* function) |
| 101 | { |
| 102 | #if PLATFORM(WIN) |
| 103 | _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function); |
| 104 | #else |
| 105 | printf_stderr_common("(%s:%d %s)\n", file, line, function); |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 110 | { |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 111 | if (assertion) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 112 | printf_stderr_common("ASSERTION FAILED: %s\n", assertion); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 113 | else |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 114 | printf_stderr_common("SHOULD NEVER BE REACHED\n"); |
| 115 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 118 | void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 119 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 120 | printf_stderr_common("ASSERTION FAILED: "); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 121 | va_list args; |
| 122 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 123 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 124 | va_end(args); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 125 | printf_stderr_common("\n%s\n", assertion); |
| 126 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 129 | void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 130 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 131 | printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion); |
| 132 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 135 | void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 136 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 137 | printf_stderr_common("FATAL ERROR: "); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 138 | va_list args; |
| 139 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 140 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 141 | va_end(args); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 142 | printf_stderr_common("\n"); |
| 143 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 146 | void WTFReportError(const char* file, int line, const char* function, const char* format, ...) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 147 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 148 | printf_stderr_common("ERROR: "); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 149 | va_list args; |
| 150 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 151 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 152 | va_end(args); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 153 | printf_stderr_common("\n"); |
| 154 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame^] | 157 | void WTFLog(const char*, int, const char*, WTFLogChannel *channel, const char* format, ...) |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 158 | { |
mjs | bb86351 | 2006-05-09 09:27:55 +0000 | [diff] [blame] | 159 | if (channel->state != WTFLogChannelOn) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 160 | return; |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 161 | |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 162 | va_list args; |
| 163 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 164 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 165 | va_end(args); |
| 166 | if (format[strlen(format) - 1] != '\n') |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 167 | printf_stderr_common("\n"); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 168 | } |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 169 | |
| 170 | } // extern "C" |