blob: 098186e64b1fbc5d1fa9b0aa08ccdd0ec0a5319d [file] [log] [blame]
darin7e7b97b2002-09-09 06:11:13 +00001/*
weinigcdc6bc92007-05-31 00:16:27 +00002 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
darin7e7b97b2002-09-09 06:11:13 +00003 *
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.comf716e152008-04-10 16:03:42 +000023 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
darin7e7b97b2002-09-09 06:11:13 +000024 */
25
mjsb64c50a2005-10-03 21:13:12 +000026#include "config.h"
mjscff5e5e2005-09-27 22:37:33 +000027#include "Assertions.h"
weinigcdc6bc92007-05-31 00:16:27 +000028
darinfb4d9372006-04-22 05:31:57 +000029#include <stdio.h>
30#include <stdarg.h>
31#include <string.h>
mjscff5e5e2005-09-27 22:37:33 +000032
mjs3bfb61b2006-03-02 09:12:06 +000033#if PLATFORM(MAC)
mjscff5e5e2005-09-27 22:37:33 +000034#include <CoreFoundation/CFString.h>
35#endif
36
treat@webkit.org068b0692009-06-17 16:39:56 +000037#if COMPILER(MSVC) && !PLATFORM(WINCE)
ddkilzerd7084232007-03-05 01:33:33 +000038#ifndef WINVER
seangies264ec632006-09-20 21:59:15 +000039#define WINVER 0x0500
ddkilzerd7084232007-03-05 01:33:33 +000040#endif
41#ifndef _WIN32_WINNT
seangies264ec632006-09-20 21:59:15 +000042#define _WIN32_WINNT 0x0500
ddkilzerd7084232007-03-05 01:33:33 +000043#endif
seangies264ec632006-09-20 21:59:15 +000044#include <windows.h>
darin9864c1a2006-10-04 20:34:21 +000045#include <crtdbg.h>
seangies264ec632006-09-20 21:59:15 +000046#endif
47
mjscff5e5e2005-09-27 22:37:33 +000048extern "C" {
darin7e7b97b2002-09-09 06:11:13 +000049
mrowe@apple.com85aac952007-11-22 03:08:41 +000050WTF_ATTRIBUTE_PRINTF(1, 0)
darin9864c1a2006-10-04 20:34:21 +000051static void vprintf_stderr_common(const char* format, va_list args)
darin7e7b97b2002-09-09 06:11:13 +000052{
mjs3bfb61b2006-03-02 09:12:06 +000053#if PLATFORM(MAC)
tomernicdbff46d2006-05-01 20:49:48 +000054 if (strstr(format, "%@")) {
mjscff5e5e2005-09-27 22:37:33 +000055 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
56 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
levin@chromium.org80f81792009-06-21 22:46:47 +000057
mjscff5e5e2005-09-27 22:37:33 +000058 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
darin9864c1a2006-10-04 20:34:21 +000059 char* buffer = (char*)malloc(length + 1);
mjscff5e5e2005-09-27 22:37:33 +000060
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.org068b0692009-06-17 16:39:56 +000069#elif COMPILER(MSVC) && !PLATFORM(WINCE)
darin9864c1a2006-10-04 20:34:21 +000070 if (IsDebuggerPresent()) {
seangies264ec632006-09-20 21:59:15 +000071 size_t size = 1024;
72
darin9864c1a2006-10-04 20:34:21 +000073 do {
seangies264ec632006-09-20 21:59:15 +000074 char* buffer = (char*)malloc(size);
75
76 if (buffer == NULL)
77 break;
78
darin9864c1a2006-10-04 20:34:21 +000079 if (_vsnprintf(buffer, size, format, args) != -1) {
seangies264ec632006-09-20 21:59:15 +000080 OutputDebugStringA(buffer);
81 free(buffer);
82 break;
83 }
84
85 free(buffer);
86 size *= 2;
87 } while (size > 1024);
beidson240db312006-12-19 23:08:10 +000088 }
mjscff5e5e2005-09-27 22:37:33 +000089#endif
mrowe@apple.com85aac952007-11-22 03:08:41 +000090 vfprintf(stderr, format, args);
darin7e7b97b2002-09-09 06:11:13 +000091}
92
mrowe@apple.com85aac952007-11-22 03:08:41 +000093WTF_ATTRIBUTE_PRINTF(1, 2)
darin9864c1a2006-10-04 20:34:21 +000094static void printf_stderr_common(const char* format, ...)
seangies264ec632006-09-20 21:59:15 +000095{
96 va_list args;
97 va_start(args, format);
98 vprintf_stderr_common(format, args);
99 va_end(args);
100}
101
darin9864c1a2006-10-04 20:34:21 +0000102static void printCallSite(const char* file, int line, const char* function)
103{
darin6fca5762006-10-05 17:20:04 +0000104#if PLATFORM(WIN) && defined _DEBUG
darin9864c1a2006-10-04 20:34:21 +0000105 _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
111void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000112{
mjscff5e5e2005-09-27 22:37:33 +0000113 if (assertion)
darin9864c1a2006-10-04 20:34:21 +0000114 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
mjscff5e5e2005-09-27 22:37:33 +0000115 else
darin9864c1a2006-10-04 20:34:21 +0000116 printf_stderr_common("SHOULD NEVER BE REACHED\n");
117 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000118}
119
darin9864c1a2006-10-04 20:34:21 +0000120void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000121{
darin9864c1a2006-10-04 20:34:21 +0000122 printf_stderr_common("ASSERTION FAILED: ");
darin7e7b97b2002-09-09 06:11:13 +0000123 va_list args;
124 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000125 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000126 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000127 printf_stderr_common("\n%s\n", assertion);
128 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000129}
130
darin9864c1a2006-10-04 20:34:21 +0000131void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000132{
darin9864c1a2006-10-04 20:34:21 +0000133 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
134 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000135}
136
darin9864c1a2006-10-04 20:34:21 +0000137void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000138{
darin9864c1a2006-10-04 20:34:21 +0000139 printf_stderr_common("FATAL ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000140 va_list args;
141 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000142 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000143 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000144 printf_stderr_common("\n");
145 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000146}
147
darin9864c1a2006-10-04 20:34:21 +0000148void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000149{
darin9864c1a2006-10-04 20:34:21 +0000150 printf_stderr_common("ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000151 va_list args;
152 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000153 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000154 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000155 printf_stderr_common("\n");
156 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000157}
158
weinigcdc6bc92007-05-31 00:16:27 +0000159void WTFLog(WTFLogChannel* channel, const char* format, ...)
160{
mjsbb863512006-05-09 09:27:55 +0000161 if (channel->state != WTFLogChannelOn)
darin7e7b97b2002-09-09 06:11:13 +0000162 return;
weinigcdc6bc92007-05-31 00:16:27 +0000163
darin7e7b97b2002-09-09 06:11:13 +0000164 va_list args;
165 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000166 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000167 va_end(args);
168 if (format[strlen(format) - 1] != '\n')
seangies264ec632006-09-20 21:59:15 +0000169 printf_stderr_common("\n");
darin7e7b97b2002-09-09 06:11:13 +0000170}
mjscff5e5e2005-09-27 22:37:33 +0000171
weinigcdc6bc92007-05-31 00:16:27 +0000172void 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
mjscff5e5e2005-09-27 22:37:33 +0000186} // extern "C"