| 1 | /* |
| 2 | * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2007-2009 Torch Mobile, Inc. |
| 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 | |
| 27 | #include "config.h" |
| 28 | #include "Assertions.h" |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #if PLATFORM(MAC) |
| 35 | #include <CoreFoundation/CFString.h> |
| 36 | #endif |
| 37 | |
| 38 | #if COMPILER(MSVC) && !OS(WINCE) |
| 39 | #ifndef WINVER |
| 40 | #define WINVER 0x0500 |
| 41 | #endif |
| 42 | #ifndef _WIN32_WINNT |
| 43 | #define _WIN32_WINNT 0x0500 |
| 44 | #endif |
| 45 | #include <windows.h> |
| 46 | #include <crtdbg.h> |
| 47 | #endif |
| 48 | |
| 49 | #if OS(WINCE) |
| 50 | #include <winbase.h> |
| 51 | #endif |
| 52 | |
| 53 | extern "C" { |
| 54 | |
| 55 | WTF_ATTRIBUTE_PRINTF(1, 0) |
| 56 | static void vprintf_stderr_common(const char* format, va_list args) |
| 57 | { |
| 58 | #if PLATFORM(MAC) |
| 59 | if (strstr(format, "%@" )) { |
| 60 | CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); |
| 61 | CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args); |
| 62 | |
| 63 | int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8); |
| 64 | char* buffer = (char*)malloc(length + 1); |
| 65 | |
| 66 | CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8); |
| 67 | |
| 68 | fputs(buffer, stderr); |
| 69 | |
| 70 | free(buffer); |
| 71 | CFRelease(str); |
| 72 | CFRelease(cfFormat); |
| 73 | } else |
| 74 | #elif COMPILER(MSVC) && !defined(WINCEBASIC) |
| 75 | # if !defined(_WIN32_WCE) || (_WIN32_WCE >= 0x600) |
| 76 | if (IsDebuggerPresent()) |
| 77 | # endif |
| 78 | { |
| 79 | size_t size = 1024; |
| 80 | |
| 81 | do { |
| 82 | char* buffer = (char*)malloc(size); |
| 83 | |
| 84 | if (buffer == NULL) |
| 85 | break; |
| 86 | |
| 87 | if (_vsnprintf(buffer, size, format, args) != -1) { |
| 88 | #if OS(WINCE) |
| 89 | // WinCE only supports wide chars |
| 90 | wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t)); |
| 91 | if (wideBuffer == NULL) |
| 92 | break; |
| 93 | for (unsigned int i = 0; i < size; ++i) { |
| 94 | if (!(wideBuffer[i] = buffer[i])) |
| 95 | break; |
| 96 | } |
| 97 | OutputDebugStringW(wideBuffer); |
| 98 | free(wideBuffer); |
| 99 | #else |
| 100 | OutputDebugStringA(buffer); |
| 101 | #endif |
| 102 | free(buffer); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | free(buffer); |
| 107 | size *= 2; |
| 108 | } while (size > 1024); |
| 109 | } |
| 110 | #endif |
| 111 | #if OS(SYMBIAN) |
| 112 | vfprintf(stdout, format, args); |
| 113 | #else |
| 114 | vfprintf(stderr, format: format, arg: args); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | WTF_ATTRIBUTE_PRINTF(1, 2) |
| 119 | static void printf_stderr_common(const char* format, ...) |
| 120 | { |
| 121 | va_list args; |
| 122 | va_start(args, format); |
| 123 | vprintf_stderr_common(format, args); |
| 124 | va_end(args); |
| 125 | } |
| 126 | |
| 127 | static void printCallSite(const char* file, int line, const char* function) |
| 128 | { |
| 129 | #if OS(WIN) && !OS(WINCE) && defined _DEBUG |
| 130 | _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n" , function); |
| 131 | #else |
| 132 | printf_stderr_common(format: "(%s:%d %s)\n" , file, line, function); |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) |
| 137 | { |
| 138 | if (assertion) |
| 139 | printf_stderr_common(format: "ASSERTION FAILED: %s\n" , assertion); |
| 140 | else |
| 141 | printf_stderr_common(format: "SHOULD NEVER BE REACHED\n" ); |
| 142 | printCallSite(file, line, function); |
| 143 | } |
| 144 | |
| 145 | void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) |
| 146 | { |
| 147 | printf_stderr_common(format: "ASSERTION FAILED: " ); |
| 148 | va_list args; |
| 149 | va_start(args, format); |
| 150 | vprintf_stderr_common(format, args); |
| 151 | va_end(args); |
| 152 | printf_stderr_common(format: "\n%s\n" , assertion); |
| 153 | printCallSite(file, line, function); |
| 154 | } |
| 155 | |
| 156 | void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) |
| 157 | { |
| 158 | printf_stderr_common(format: "ARGUMENT BAD: %s, %s\n" , argName, assertion); |
| 159 | printCallSite(file, line, function); |
| 160 | } |
| 161 | |
| 162 | void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) |
| 163 | { |
| 164 | printf_stderr_common(format: "FATAL ERROR: " ); |
| 165 | va_list args; |
| 166 | va_start(args, format); |
| 167 | vprintf_stderr_common(format, args); |
| 168 | va_end(args); |
| 169 | printf_stderr_common(format: "\n" ); |
| 170 | printCallSite(file, line, function); |
| 171 | } |
| 172 | |
| 173 | void WTFReportError(const char* file, int line, const char* function, const char* format, ...) |
| 174 | { |
| 175 | printf_stderr_common(format: "ERROR: " ); |
| 176 | va_list args; |
| 177 | va_start(args, format); |
| 178 | vprintf_stderr_common(format, args); |
| 179 | va_end(args); |
| 180 | printf_stderr_common(format: "\n" ); |
| 181 | printCallSite(file, line, function); |
| 182 | } |
| 183 | |
| 184 | void WTFLog(WTFLogChannel* channel, const char* format, ...) |
| 185 | { |
| 186 | if (channel->state != WTFLogChannelOn) |
| 187 | return; |
| 188 | |
| 189 | va_list args; |
| 190 | va_start(args, format); |
| 191 | vprintf_stderr_common(format, args); |
| 192 | va_end(args); |
| 193 | if (format[strlen(s: format) - 1] != '\n') |
| 194 | printf_stderr_common(format: "\n" ); |
| 195 | } |
| 196 | |
| 197 | void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) |
| 198 | { |
| 199 | if (channel->state != WTFLogChannelOn) |
| 200 | return; |
| 201 | |
| 202 | va_list args; |
| 203 | va_start(args, format); |
| 204 | vprintf_stderr_common(format, args); |
| 205 | va_end(args); |
| 206 | if (format[strlen(s: format) - 1] != '\n') |
| 207 | printf_stderr_common(format: "\n" ); |
| 208 | printCallSite(file, line, function); |
| 209 | } |
| 210 | |
| 211 | } // extern "C" |
| 212 | |