blob: 74a1af46bbceb689e907aea5565d5cb9d69f6c3e [file] [log] [blame]
mjscff5e5e2005-09-27 22:37:33 +00001// -*- mode: c++; c-basic-offset: 4 -*-
darin7e7b97b2002-09-09 06:11:13 +00002/*
eseidel9259f142006-02-28 03:51:43 +00003 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
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
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
mjsb64c50a2005-10-03 21:13:12 +000027#include "config.h"
mjscff5e5e2005-09-27 22:37:33 +000028#include "Assertions.h"
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
seangies264ec632006-09-20 21:59:15 +000037#if PLATFORM(WIN)
38#define WINVER 0x0500
39#define _WIN32_WINNT 0x0500
40#include <windows.h>
darin9864c1a2006-10-04 20:34:21 +000041#include <crtdbg.h>
seangies264ec632006-09-20 21:59:15 +000042#endif
43
mjscff5e5e2005-09-27 22:37:33 +000044extern "C" {
darin7e7b97b2002-09-09 06:11:13 +000045
eseidel9259f142006-02-28 03:51:43 +000046// This is to work around the "you should use a printf format attribute" warning on GCC
47// We can't use _attribute__ ((format (printf, 2, 3))) since we allow %@
darin9864c1a2006-10-04 20:34:21 +000048static int (* vfprintf_no_warning)(FILE *, const char*, va_list) = vfprintf;
darin7e7b97b2002-09-09 06:11:13 +000049
darin9864c1a2006-10-04 20:34:21 +000050static void vprintf_stderr_common(const char* format, va_list args)
darin7e7b97b2002-09-09 06:11:13 +000051{
mjs3bfb61b2006-03-02 09:12:06 +000052#if PLATFORM(MAC)
tomernicdbff46d2006-05-01 20:49:48 +000053 if (strstr(format, "%@")) {
mjscff5e5e2005-09-27 22:37:33 +000054 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
55 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
56
57 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
darin9864c1a2006-10-04 20:34:21 +000058 char* buffer = (char*)malloc(length + 1);
mjscff5e5e2005-09-27 22:37:33 +000059
60 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
61
62 fputs(buffer, stderr);
63
64 free(buffer);
65 CFRelease(str);
66 CFRelease(cfFormat);
67 } else
seangies264ec632006-09-20 21:59:15 +000068#elif PLATFORM(WIN)
darin9864c1a2006-10-04 20:34:21 +000069 if (IsDebuggerPresent()) {
seangies264ec632006-09-20 21:59:15 +000070 size_t size = 1024;
71
darin9864c1a2006-10-04 20:34:21 +000072 do {
seangies264ec632006-09-20 21:59:15 +000073 char* buffer = (char*)malloc(size);
74
75 if (buffer == NULL)
76 break;
77
darin9864c1a2006-10-04 20:34:21 +000078 if (_vsnprintf(buffer, size, format, args) != -1) {
seangies264ec632006-09-20 21:59:15 +000079 OutputDebugStringA(buffer);
80 free(buffer);
81 break;
82 }
83
84 free(buffer);
85 size *= 2;
86 } while (size > 1024);
87 } else
mjscff5e5e2005-09-27 22:37:33 +000088#endif
darin7e7b97b2002-09-09 06:11:13 +000089 vfprintf_no_warning(stderr, format, args);
darin7e7b97b2002-09-09 06:11:13 +000090}
91
darin9864c1a2006-10-04 20:34:21 +000092static void printf_stderr_common(const char* format, ...)
seangies264ec632006-09-20 21:59:15 +000093{
94 va_list args;
95 va_start(args, format);
96 vprintf_stderr_common(format, args);
97 va_end(args);
98}
99
darin9864c1a2006-10-04 20:34:21 +0000100static void printCallSite(const char* file, int line, const char* function)
101{
102#if PLATFORM(WIN)
103 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
104#else
105 printf_stderr_common("(%s:%d %s)\n", file, line, function);
106#endif
107}
108
109void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000110{
mjscff5e5e2005-09-27 22:37:33 +0000111 if (assertion)
darin9864c1a2006-10-04 20:34:21 +0000112 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
mjscff5e5e2005-09-27 22:37:33 +0000113 else
darin9864c1a2006-10-04 20:34:21 +0000114 printf_stderr_common("SHOULD NEVER BE REACHED\n");
115 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000116}
117
darin9864c1a2006-10-04 20:34:21 +0000118void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000119{
darin9864c1a2006-10-04 20:34:21 +0000120 printf_stderr_common("ASSERTION FAILED: ");
darin7e7b97b2002-09-09 06:11:13 +0000121 va_list args;
122 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000123 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000124 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000125 printf_stderr_common("\n%s\n", assertion);
126 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000127}
128
darin9864c1a2006-10-04 20:34:21 +0000129void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
darin7e7b97b2002-09-09 06:11:13 +0000130{
darin9864c1a2006-10-04 20:34:21 +0000131 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
132 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000133}
134
darin9864c1a2006-10-04 20:34:21 +0000135void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000136{
darin9864c1a2006-10-04 20:34:21 +0000137 printf_stderr_common("FATAL ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000138 va_list args;
139 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000140 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000141 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000142 printf_stderr_common("\n");
143 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000144}
145
darin9864c1a2006-10-04 20:34:21 +0000146void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
darin7e7b97b2002-09-09 06:11:13 +0000147{
darin9864c1a2006-10-04 20:34:21 +0000148 printf_stderr_common("ERROR: ");
darin7e7b97b2002-09-09 06:11:13 +0000149 va_list args;
150 va_start(args, format);
eseidel9259f142006-02-28 03:51:43 +0000151 vprintf_stderr_common(format, args);
darin7e7b97b2002-09-09 06:11:13 +0000152 va_end(args);
darin9864c1a2006-10-04 20:34:21 +0000153 printf_stderr_common("\n");
154 printCallSite(file, line, function);
darin7e7b97b2002-09-09 06:11:13 +0000155}
156
darin9864c1a2006-10-04 20:34:21 +0000157void WTFLog(const char*, int, const char*, WTFLogChannel *channel, const char* format, ...)
eseidel9259f142006-02-28 03:51:43 +0000158{
mjsbb863512006-05-09 09:27:55 +0000159 if (channel->state != WTFLogChannelOn)
darin7e7b97b2002-09-09 06:11:13 +0000160 return;
darin7e7b97b2002-09-09 06:11:13 +0000161
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);
166 if (format[strlen(format) - 1] != '\n')
seangies264ec632006-09-20 21:59:15 +0000167 printf_stderr_common("\n");
darin7e7b97b2002-09-09 06:11:13 +0000168}
mjscff5e5e2005-09-27 22:37:33 +0000169
170} // extern "C"