1 /*
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 *
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
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "Assertions.h"
28
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #if PLATFORM(MAC)
34 #include <CoreFoundation/CFString.h>
35 #endif
36
37 #if COMPILER(MSVC) && !PLATFORM(WIN_CE)
38 #ifndef WINVER
39 #define WINVER 0x0500
40 #endif
41 #ifndef _WIN32_WINNT
42 #define _WIN32_WINNT 0x0500
43 #endif
44 #include <windows.h>
45 #include <crtdbg.h>
46 #endif
47
48 extern "C" {
49
50 WTF_ATTRIBUTE_PRINTF(1, 0)
vprintf_stderr_common(const char * format,va_list args)51 static void vprintf_stderr_common(const char* format, va_list args)
52 {
53 #if PLATFORM(MAC)
54 if (strstr(format, "%@")) {
55 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
56 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
57
58 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
59 char* buffer = (char*)malloc(length + 1);
60
61 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
62
63 fputs(buffer, stderr);
64
65 free(buffer);
66 CFRelease(str);
67 CFRelease(cfFormat);
68 } else
69 #elif COMPILER(MSVC) && !PLATFORM(WIN_CE)
70 if (IsDebuggerPresent()) {
71 size_t size = 1024;
72
73 do {
74 char* buffer = (char*)malloc(size);
75
76 if (buffer == NULL)
77 break;
78
79 if (_vsnprintf(buffer, size, format, args) != -1) {
80 OutputDebugStringA(buffer);
81 free(buffer);
82 break;
83 }
84
85 free(buffer);
86 size *= 2;
87 } while (size > 1024);
88 }
89 #endif
90 vfprintf(stderr, format, args);
91 }
92
93 WTF_ATTRIBUTE_PRINTF(1, 2)
printf_stderr_common(const char * format,...)94 static void printf_stderr_common(const char* format, ...)
95 {
96 va_list args;
97 va_start(args, format);
98 vprintf_stderr_common(format, args);
99 va_end(args);
100 }
101
printCallSite(const char * file,int line,const char * function)102 static void printCallSite(const char* file, int line, const char* function)
103 {
104 #if PLATFORM(WIN) && defined _DEBUG
105 _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
WTFReportAssertionFailure(const char * file,int line,const char * function,const char * assertion)111 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
112 {
113 if (assertion)
114 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
115 else
116 printf_stderr_common("SHOULD NEVER BE REACHED\n");
117 printCallSite(file, line, function);
118 }
119
WTFReportAssertionFailureWithMessage(const char * file,int line,const char * function,const char * assertion,const char * format,...)120 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
121 {
122 printf_stderr_common("ASSERTION FAILED: ");
123 va_list args;
124 va_start(args, format);
125 vprintf_stderr_common(format, args);
126 va_end(args);
127 printf_stderr_common("\n%s\n", assertion);
128 printCallSite(file, line, function);
129 }
130
WTFReportArgumentAssertionFailure(const char * file,int line,const char * function,const char * argName,const char * assertion)131 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
132 {
133 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
134 printCallSite(file, line, function);
135 }
136
WTFReportFatalError(const char * file,int line,const char * function,const char * format,...)137 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
138 {
139 printf_stderr_common("FATAL ERROR: ");
140 va_list args;
141 va_start(args, format);
142 vprintf_stderr_common(format, args);
143 va_end(args);
144 printf_stderr_common("\n");
145 printCallSite(file, line, function);
146 }
147
WTFReportError(const char * file,int line,const char * function,const char * format,...)148 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
149 {
150 printf_stderr_common("ERROR: ");
151 va_list args;
152 va_start(args, format);
153 vprintf_stderr_common(format, args);
154 va_end(args);
155 printf_stderr_common("\n");
156 printCallSite(file, line, function);
157 }
158
WTFLog(WTFLogChannel * channel,const char * format,...)159 void WTFLog(WTFLogChannel* channel, const char* format, ...)
160 {
161 if (channel->state != WTFLogChannelOn)
162 return;
163
164 va_list args;
165 va_start(args, format);
166 vprintf_stderr_common(format, args);
167 va_end(args);
168 if (format[strlen(format) - 1] != '\n')
169 printf_stderr_common("\n");
170 }
171
WTFLogVerbose(const char * file,int line,const char * function,WTFLogChannel * channel,const char * format,...)172 void 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
186 } // extern "C"
187