blob: 6c5e2e3069b0703b580ba79b8af119819ee91f7c [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.
treat@webkit.org1ffac0e2009-07-15 17:20:11 +00003 * Copyright (C) 2007-2009 Torch Mobile, Inc.
darin7e7b97b2002-09-09 06:11:13 +00004 *
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.comf716e152008-04-10 16:03:42 +000024 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
darin7e7b97b2002-09-09 06:11:13 +000025 */
26
mjsb64c50a2005-10-03 21:13:12 +000027#include "config.h"
mjscff5e5e2005-09-27 22:37:33 +000028#include "Assertions.h"
weinigcdc6bc92007-05-31 00:16:27 +000029
darinfb4d9372006-04-22 05:31:57 +000030#include <stdio.h>
31#include <stdarg.h>
32#include <string.h>
mjscff5e5e2005-09-27 22:37:33 +000033
mjs3bfb61b2006-03-02 09:12:06 +000034#if PLATFORM(MAC)
mjscff5e5e2005-09-27 22:37:33 +000035#include <CoreFoundation/CFString.h>
36#endif
37
treat@webkit.org068b0692009-06-17 16:39:56 +000038#if COMPILER(MSVC) && !PLATFORM(WINCE)
ddkilzerd7084232007-03-05 01:33:33 +000039#ifndef WINVER
seangies264ec632006-09-20 21:59:15 +000040#define WINVER 0x0500
ddkilzerd7084232007-03-05 01:33:33 +000041#endif
42#ifndef _WIN32_WINNT
seangies264ec632006-09-20 21:59:15 +000043#define _WIN32_WINNT 0x0500
ddkilzerd7084232007-03-05 01:33:33 +000044#endif
seangies264ec632006-09-20 21:59:15 +000045#include <windows.h>
darin9864c1a2006-10-04 20:34:21 +000046#include <crtdbg.h>
seangies264ec632006-09-20 21:59:15 +000047#endif
48
treat@webkit.org1ffac0e2009-07-15 17:20:11 +000049#if PLATFORM(WINCE)
50#include <winbase.h>
51#endif
52
mjscff5e5e2005-09-27 22:37:33 +000053extern "C" {
darin7e7b97b2002-09-09 06:11:13 +000054
mrowe@apple.com85aac952007-11-22 03:08:41 +000055WTF_ATTRIBUTE_PRINTF(1, 0)
darin9864c1a2006-10-04 20:34:21 +000056static void vprintf_stderr_common(const char* format, va_list args)
darin7e7b97b2002-09-09 06:11:13 +000057{
mjs3bfb61b2006-03-02 09:12:06 +000058#if PLATFORM(MAC)
tomernicdbff46d2006-05-01 20:49:48 +000059 if (strstr(format, "%@")) {
mjscff5e5e2005-09-27 22:37:33 +000060 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
61 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
levin@chromium.org80f81792009-06-21 22:46:47 +000062
mjscff5e5e2005-09-27 22:37:33 +000063 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
darin9864c1a2006-10-04 20:34:21 +000064 char* buffer = (char*)malloc(length + 1);
mjscff5e5e2005-09-27 22:37:33 +000065
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.org1ffac0e2009-07-15 17:20:11 +000074#elif COMPILER(MSVC) && !defined(WINCEBASIC)
darin9864c1a2006-10-04 20:34:21 +000075 if (IsDebuggerPresent()) {
seangies264ec632006-09-20 21:59:15 +000076 size_t size = 1024;
77
darin9864c1a2006-10-04 20:34:21 +000078 do {
seangies264ec632006-09-20 21:59:15 +000079 char* buffer = (char*)malloc(size);
80
81 if (buffer == NULL)
82 break;
83
darin9864c1a2006-10-04 20:34:21 +000084 if (_vsnprintf(buffer, size, format, args) != -1) {
treat@webkit.org1ffac0e2009-07-15 17:20:11 +000085#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
seangies264ec632006-09-20 21:59:15 +000097 OutputDebugStringA(buffer);
treat@webkit.org1ffac0e2009-07-15 17:20:11 +000098#endif
seangies264ec632006-09-20 21:59:15 +000099 free(buffer);
100 break;
101 }
102
103 free(buffer);
104 size *= 2;
105 } while (size > 1024);
beidson240db312006-12-19 23:08:10 +0000106 }
mjscff5e5e2005-09-27 22:37:33 +0000107#endif
eric@webkit.orgecc70252009-09-29 16:10:02 +0000108#if PLATFORM(SYMBIAN)
109 vfprintf(stdout, format, args);
110#else
mrowe@apple.com85aac952007-11-22 03:08:41 +0000111 vfprintf(stderr, format, args);
eric@webkit.orgecc70252009-09-29 16:10:02 +0000112#endif
darin7e7b97b2002-09-09 06:11:13 +0000113}
114
mrowe@apple.com85aac952007-11-22 03:08:41 +0000115WTF_ATTRIBUTE_PRINTF(1, 2)
darin9864c1a2006-10-04 20:34:21 +0000116static void printf_stderr_common(const char* format, ...)
seangies264ec632006-09-20 21:59:15 +0000117{
118 va_list args;
119 va_start(args, format);
120 vprintf_stderr_common(format, args);
121 va_end(args);
122}
123
darin9864c1a2006-10-04 20:34:21 +0000124static void printCallSite(const char* file, int line, const char* function)
125{
treat@webkit.org1ffac0e2009-07-15 17:20:11 +0000126#if PLATFORM(WIN) && !PLATFORM(WINCE) && defined _DEBUG
darin9864c1a2006-10-04 20:34:21 +0000127 _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
133void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000134{
mjscff5e5e2005-09-27 22:37:33 +0000135 if (assertion)
darin9864c1a2006-10-04 20:34:21 +0000136 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
mjscff5e5e2005-09-27 22:37:33 +0000137 else
darin9864c1a2006-10-04 20:34:21 +0000138 printf_stderr_common("SHOULD NEVER BE REACHED\n");
139 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000140}
141
darin9864c1a2006-10-04 20:34:21 +0000142void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000143{
darin9864c1a2006-10-04 20:34:21 +0000144 printf_stderr_common("ASSERTION FAILED: ");
darin7e7b97b2002-09-09 06:11:13 +0000145 va_list args;
146 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000147 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000148 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000149 printf_stderr_common("\n%s\n", assertion);
150 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000151}
152
darin9864c1a2006-10-04 20:34:21 +0000153void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000154{
darin9864c1a2006-10-04 20:34:21 +0000155 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
156 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000157}
158
darin9864c1a2006-10-04 20:34:21 +0000159void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000160{
darin9864c1a2006-10-04 20:34:21 +0000161 printf_stderr_common("FATAL ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000162 va_list args;
163 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000164 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000165 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000166 printf_stderr_common("\n");
167 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000168}
169
darin9864c1a2006-10-04 20:34:21 +0000170void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000171{
darin9864c1a2006-10-04 20:34:21 +0000172 printf_stderr_common("ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000173 va_list args;
174 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000175 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000176 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000177 printf_stderr_common("\n");
178 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000179}
180
weinigcdc6bc92007-05-31 00:16:27 +0000181void WTFLog(WTFLogChannel* channel, const char* format, ...)
182{
mjsbb863512006-05-09 09:27:55 +0000183 if (channel->state != WTFLogChannelOn)
darin7e7b97b2002-09-09 06:11:13 +0000184 return;
weinigcdc6bc92007-05-31 00:16:27 +0000185
darin7e7b97b2002-09-09 06:11:13 +0000186 va_list args;
187 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000188 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000189 va_end(args);
190 if (format[strlen(format) - 1] != '\n')
seangies264ec632006-09-20 21:59:15 +0000191 printf_stderr_common("\n");
darin7e7b97b2002-09-09 06:11:13 +0000192}
mjscff5e5e2005-09-27 22:37:33 +0000193
weinigcdc6bc92007-05-31 00:16:27 +0000194void 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
mjscff5e5e2005-09-27 22:37:33 +0000208} // extern "C"