darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 1 | /* |
weinig | cdc6bc9 | 2007-05-31 00:16:27 +0000 | [diff] [blame] | 2 | * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007-2009 Torch Mobile, Inc. |
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 |
aroben@apple.com | f716e15 | 2008-04-10 16:03:42 +0000 | [diff] [blame] | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 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" |
weinig | cdc6bc9 | 2007-05-31 00:16:27 +0000 | [diff] [blame] | 29 | |
darin | fb4d937 | 2006-04-22 05:31:57 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <string.h> |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 33 | |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 34 | #if PLATFORM(MAC) |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 35 | #include <CoreFoundation/CFString.h> |
| 36 | #endif |
| 37 | |
treat@webkit.org | 068b069 | 2009-06-17 16:39:56 +0000 | [diff] [blame] | 38 | #if COMPILER(MSVC) && !PLATFORM(WINCE) |
ddkilzer | d708423 | 2007-03-05 01:33:33 +0000 | [diff] [blame] | 39 | #ifndef WINVER |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 40 | #define WINVER 0x0500 |
ddkilzer | d708423 | 2007-03-05 01:33:33 +0000 | [diff] [blame] | 41 | #endif |
| 42 | #ifndef _WIN32_WINNT |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 43 | #define _WIN32_WINNT 0x0500 |
ddkilzer | d708423 | 2007-03-05 01:33:33 +0000 | [diff] [blame] | 44 | #endif |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 45 | #include <windows.h> |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 46 | #include <crtdbg.h> |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 49 | #if PLATFORM(WINCE) |
| 50 | #include <winbase.h> |
| 51 | #endif |
| 52 | |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 53 | extern "C" { |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 54 | |
mrowe@apple.com | 85aac95 | 2007-11-22 03:08:41 +0000 | [diff] [blame] | 55 | WTF_ATTRIBUTE_PRINTF(1, 0) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 56 | static void vprintf_stderr_common(const char* format, va_list args) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 57 | { |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 58 | #if PLATFORM(MAC) |
tomernic | dbff46d | 2006-05-01 20:49:48 +0000 | [diff] [blame] | 59 | if (strstr(format, "%@")) { |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 60 | CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); |
| 61 | CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args); |
levin@chromium.org | 80f8179 | 2009-06-21 22:46:47 +0000 | [diff] [blame] | 62 | |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 63 | int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 64 | char* buffer = (char*)malloc(length + 1); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 65 | |
| 66 | CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8); |
| 67 | |
| 68 | fputs(buffer, stderr); |
| 69 | |
| 70 | free(buffer); |
| 71 | CFRelease(str); |
| 72 | CFRelease(cfFormat); |
| 73 | } else |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 74 | #elif COMPILER(MSVC) && !defined(WINCEBASIC) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 75 | if (IsDebuggerPresent()) { |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 76 | size_t size = 1024; |
| 77 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 78 | do { |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 79 | char* buffer = (char*)malloc(size); |
| 80 | |
| 81 | if (buffer == NULL) |
| 82 | break; |
| 83 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 84 | if (_vsnprintf(buffer, size, format, args) != -1) { |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 85 | #if PLATFORM(WINCE) |
| 86 | // WinCE only supports wide chars |
| 87 | wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t)); |
| 88 | if (wideBuffer == NULL) |
| 89 | break; |
| 90 | for (unsigned int i = 0; i < size; ++i) { |
| 91 | if (!(wideBuffer[i] = buffer[i])) |
| 92 | break; |
| 93 | } |
| 94 | OutputDebugStringW(wideBuffer); |
| 95 | free(wideBuffer); |
| 96 | #else |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 97 | OutputDebugStringA(buffer); |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 98 | #endif |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 99 | free(buffer); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | free(buffer); |
| 104 | size *= 2; |
| 105 | } while (size > 1024); |
beidson | 240db31 | 2006-12-19 23:08:10 +0000 | [diff] [blame] | 106 | } |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 107 | #endif |
eric@webkit.org | ecc7025 | 2009-09-29 16:10:02 +0000 | [diff] [blame] | 108 | #if PLATFORM(SYMBIAN) |
| 109 | vfprintf(stdout, format, args); |
| 110 | #else |
mrowe@apple.com | 85aac95 | 2007-11-22 03:08:41 +0000 | [diff] [blame] | 111 | vfprintf(stderr, format, args); |
eric@webkit.org | ecc7025 | 2009-09-29 16:10:02 +0000 | [diff] [blame] | 112 | #endif |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
mrowe@apple.com | 85aac95 | 2007-11-22 03:08:41 +0000 | [diff] [blame] | 115 | WTF_ATTRIBUTE_PRINTF(1, 2) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 116 | static void printf_stderr_common(const char* format, ...) |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 117 | { |
| 118 | va_list args; |
| 119 | va_start(args, format); |
| 120 | vprintf_stderr_common(format, args); |
| 121 | va_end(args); |
| 122 | } |
| 123 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 124 | static void printCallSite(const char* file, int line, const char* function) |
| 125 | { |
treat@webkit.org | 1ffac0e | 2009-07-15 17:20:11 +0000 | [diff] [blame] | 126 | #if PLATFORM(WIN) && !PLATFORM(WINCE) && defined _DEBUG |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 127 | _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function); |
| 128 | #else |
| 129 | printf_stderr_common("(%s:%d %s)\n", file, line, function); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 134 | { |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 135 | if (assertion) |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 136 | printf_stderr_common("ASSERTION FAILED: %s\n", assertion); |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 137 | else |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 138 | printf_stderr_common("SHOULD NEVER BE REACHED\n"); |
| 139 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 142 | 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] | 143 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 144 | printf_stderr_common("ASSERTION FAILED: "); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 145 | va_list args; |
| 146 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 147 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 148 | va_end(args); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 149 | printf_stderr_common("\n%s\n", assertion); |
| 150 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 153 | 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] | 154 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 155 | printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion); |
| 156 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 157 | } |
| 158 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 159 | void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 160 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 161 | printf_stderr_common("FATAL ERROR: "); |
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); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 166 | printf_stderr_common("\n"); |
| 167 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 168 | } |
| 169 | |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 170 | void WTFReportError(const char* file, int line, const char* function, const char* format, ...) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 171 | { |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 172 | printf_stderr_common("ERROR: "); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 173 | va_list args; |
| 174 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 175 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 176 | va_end(args); |
darin | 9864c1a | 2006-10-04 20:34:21 +0000 | [diff] [blame] | 177 | printf_stderr_common("\n"); |
| 178 | printCallSite(file, line, function); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
weinig | cdc6bc9 | 2007-05-31 00:16:27 +0000 | [diff] [blame] | 181 | void WTFLog(WTFLogChannel* channel, const char* format, ...) |
| 182 | { |
mjs | bb86351 | 2006-05-09 09:27:55 +0000 | [diff] [blame] | 183 | if (channel->state != WTFLogChannelOn) |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 184 | return; |
weinig | cdc6bc9 | 2007-05-31 00:16:27 +0000 | [diff] [blame] | 185 | |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 186 | va_list args; |
| 187 | va_start(args, format); |
eseidel | 9259f14 | 2006-02-28 03:51:43 +0000 | [diff] [blame] | 188 | vprintf_stderr_common(format, args); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 189 | va_end(args); |
| 190 | if (format[strlen(format) - 1] != '\n') |
seangies | 264ec63 | 2006-09-20 21:59:15 +0000 | [diff] [blame] | 191 | printf_stderr_common("\n"); |
darin | 7e7b97b | 2002-09-09 06:11:13 +0000 | [diff] [blame] | 192 | } |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 193 | |
weinig | cdc6bc9 | 2007-05-31 00:16:27 +0000 | [diff] [blame] | 194 | void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) |
| 195 | { |
| 196 | if (channel->state != WTFLogChannelOn) |
| 197 | return; |
| 198 | |
| 199 | va_list args; |
| 200 | va_start(args, format); |
| 201 | vprintf_stderr_common(format, args); |
| 202 | va_end(args); |
| 203 | if (format[strlen(format) - 1] != '\n') |
| 204 | printf_stderr_common("\n"); |
| 205 | printCallSite(file, line, function); |
| 206 | } |
| 207 | |
mjs | cff5e5e | 2005-09-27 22:37:33 +0000 | [diff] [blame] | 208 | } // extern "C" |