Assertions.cpp revision cad810f21b803229eb11403f9209855525a25d57
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) && !PLATFORM(BREWMP) 39#ifndef WINVER 40#define WINVER 0x0500 41#endif 42#ifndef _WIN32_WINNT 43#define _WIN32_WINNT 0x0500 44#endif 45#include <crtdbg.h> 46#endif 47 48#if OS(WINDOWS) 49#include <windows.h> 50#endif 51 52#if PLATFORM(BREWMP) 53#include <AEEdbg.h> 54#include <wtf/Vector.h> 55#endif 56 57extern "C" { 58 59#if PLATFORM(BREWMP) 60 61static void printLog(const Vector<char>& buffer) 62{ 63 // Each call to DBGPRINTF generates at most 128 bytes of output on the Windows SDK. 64 // On Qualcomm chipset targets, DBGPRINTF() comes out the diag port (though this may change). 65 // The length of each output string is constrained even more than on the Windows SDK. 66#if COMPILER(MSVC) 67 const int printBufferSize = 128; 68#else 69 const int printBufferSize = 32; 70#endif 71 72 char printBuffer[printBufferSize + 1]; 73 printBuffer[printBufferSize] = 0; // to guarantee null termination 74 75 const char* p = buffer.data(); 76 const char* end = buffer.data() + buffer.size(); 77 while (p < end) { 78 strncpy(printBuffer, p, printBufferSize); 79 dbg_Message(printBuffer, DBG_MSG_LEVEL_HIGH, __FILE__, __LINE__); 80 p += printBufferSize; 81 } 82} 83 84#endif 85 86WTF_ATTRIBUTE_PRINTF(1, 0) 87static void vprintf_stderr_common(const char* format, va_list args) 88{ 89#if PLATFORM(MAC) 90 if (strstr(format, "%@")) { 91 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); 92 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args); 93 94 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8); 95 char* buffer = (char*)malloc(length + 1); 96 97 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8); 98 99 fputs(buffer, stderr); 100 101 free(buffer); 102 CFRelease(str); 103 CFRelease(cfFormat); 104 } else 105#elif PLATFORM(BREWMP) 106 // When str is 0, the return value is the number of bytes needed 107 // to accept the result including null termination. 108 int size = vsnprintf(0, 0, format, args); 109 if (size > 0) { 110 Vector<char> buffer(size); 111 vsnprintf(buffer.data(), size, format, args); 112 printLog(buffer); 113 } 114 115#elif HAVE(ISDEBUGGERPRESENT) 116 if (IsDebuggerPresent()) { 117 size_t size = 1024; 118 119 do { 120 char* buffer = (char*)malloc(size); 121 122 if (buffer == NULL) 123 break; 124 125 if (_vsnprintf(buffer, size, format, args) != -1) { 126#if OS(WINCE) 127 // WinCE only supports wide chars 128 wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t)); 129 if (wideBuffer == NULL) 130 break; 131 for (unsigned int i = 0; i < size; ++i) { 132 if (!(wideBuffer[i] = buffer[i])) 133 break; 134 } 135 OutputDebugStringW(wideBuffer); 136 free(wideBuffer); 137#else 138 OutputDebugStringA(buffer); 139#endif 140 free(buffer); 141 break; 142 } 143 144 free(buffer); 145 size *= 2; 146 } while (size > 1024); 147 } 148#endif 149#if OS(SYMBIAN) 150 vfprintf(stdout, format, args); 151#else 152 vfprintf(stderr, format, args); 153#endif 154} 155 156WTF_ATTRIBUTE_PRINTF(1, 2) 157static void printf_stderr_common(const char* format, ...) 158{ 159 va_list args; 160 va_start(args, format); 161 vprintf_stderr_common(format, args); 162 va_end(args); 163} 164 165static void printCallSite(const char* file, int line, const char* function) 166{ 167#if OS(WIN) && !OS(WINCE) && defined _DEBUG 168 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function); 169#else 170 printf_stderr_common("(%s:%d %s)\n", file, line, function); 171#endif 172} 173 174void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) 175{ 176 if (assertion) 177 printf_stderr_common("ASSERTION FAILED: %s\n", assertion); 178 else 179 printf_stderr_common("SHOULD NEVER BE REACHED\n"); 180 printCallSite(file, line, function); 181} 182 183void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) 184{ 185 printf_stderr_common("ASSERTION FAILED: "); 186 va_list args; 187 va_start(args, format); 188 vprintf_stderr_common(format, args); 189 va_end(args); 190 printf_stderr_common("\n%s\n", assertion); 191 printCallSite(file, line, function); 192} 193 194void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) 195{ 196 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion); 197 printCallSite(file, line, function); 198} 199 200void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) 201{ 202 printf_stderr_common("FATAL ERROR: "); 203 va_list args; 204 va_start(args, format); 205 vprintf_stderr_common(format, args); 206 va_end(args); 207 printf_stderr_common("\n"); 208 printCallSite(file, line, function); 209} 210 211void WTFReportError(const char* file, int line, const char* function, const char* format, ...) 212{ 213 printf_stderr_common("ERROR: "); 214 va_list args; 215 va_start(args, format); 216 vprintf_stderr_common(format, args); 217 va_end(args); 218 printf_stderr_common("\n"); 219 printCallSite(file, line, function); 220} 221 222void WTFLog(WTFLogChannel* channel, const char* format, ...) 223{ 224 if (channel->state != WTFLogChannelOn) 225 return; 226 227 va_list args; 228 va_start(args, format); 229 vprintf_stderr_common(format, args); 230 va_end(args); 231 if (format[strlen(format) - 1] != '\n') 232 printf_stderr_common("\n"); 233} 234 235void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) 236{ 237 if (channel->state != WTFLogChannelOn) 238 return; 239 240 va_list args; 241 va_start(args, format); 242 vprintf_stderr_common(format, args); 243 va_end(args); 244 if (format[strlen(format) - 1] != '\n') 245 printf_stderr_common("\n"); 246 printCallSite(file, line, function); 247} 248 249} // extern "C" 250