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