• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) && !PLATFORM(WINCE)
39 #ifndef WINVER
40 #define WINVER 0x0500
41 #endif
42 #ifndef _WIN32_WINNT
43 #define _WIN32_WINNT 0x0500
44 #endif
45 #include <windows.h>
46 #include <crtdbg.h>
47 #endif
48 
49 #if PLATFORM(WINCE)
50 #include <winbase.h>
51 #endif
52 
53 extern "C" {
54 
55 WTF_ATTRIBUTE_PRINTF(1, 0)
vprintf_stderr_common(const char * format,va_list args)56 static void vprintf_stderr_common(const char* format, va_list args)
57 {
58 #if PLATFORM(MAC)
59     if (strstr(format, "%@")) {
60         CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
61         CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
62 
63         int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
64         char* buffer = (char*)malloc(length + 1);
65 
66         CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
67 
68         fputs(buffer, stderr);
69 
70         free(buffer);
71         CFRelease(str);
72         CFRelease(cfFormat);
73     } else
74 #elif COMPILER(MSVC) && !defined(WINCEBASIC)
75     if (IsDebuggerPresent()) {
76         size_t size = 1024;
77 
78         do {
79             char* buffer = (char*)malloc(size);
80 
81             if (buffer == NULL)
82                 break;
83 
84             if (_vsnprintf(buffer, size, format, args) != -1) {
85 #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
97                 OutputDebugStringA(buffer);
98 #endif
99                 free(buffer);
100                 break;
101             }
102 
103             free(buffer);
104             size *= 2;
105         } while (size > 1024);
106     }
107 #endif
108     vfprintf(stderr, format, args);
109 }
110 
111 WTF_ATTRIBUTE_PRINTF(1, 2)
printf_stderr_common(const char * format,...)112 static void printf_stderr_common(const char* format, ...)
113 {
114     va_list args;
115     va_start(args, format);
116     vprintf_stderr_common(format, args);
117     va_end(args);
118 }
119 
printCallSite(const char * file,int line,const char * function)120 static void printCallSite(const char* file, int line, const char* function)
121 {
122 #if PLATFORM(WIN) && !PLATFORM(WINCE) && defined _DEBUG
123     _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
124 #else
125     printf_stderr_common("(%s:%d %s)\n", file, line, function);
126 #endif
127 }
128 
WTFReportAssertionFailure(const char * file,int line,const char * function,const char * assertion)129 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
130 {
131     if (assertion)
132         printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
133     else
134         printf_stderr_common("SHOULD NEVER BE REACHED\n");
135     printCallSite(file, line, function);
136 }
137 
WTFReportAssertionFailureWithMessage(const char * file,int line,const char * function,const char * assertion,const char * format,...)138 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
139 {
140     printf_stderr_common("ASSERTION FAILED: ");
141     va_list args;
142     va_start(args, format);
143     vprintf_stderr_common(format, args);
144     va_end(args);
145     printf_stderr_common("\n%s\n", assertion);
146     printCallSite(file, line, function);
147 }
148 
WTFReportArgumentAssertionFailure(const char * file,int line,const char * function,const char * argName,const char * assertion)149 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
150 {
151     printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
152     printCallSite(file, line, function);
153 }
154 
WTFReportFatalError(const char * file,int line,const char * function,const char * format,...)155 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
156 {
157     printf_stderr_common("FATAL ERROR: ");
158     va_list args;
159     va_start(args, format);
160     vprintf_stderr_common(format, args);
161     va_end(args);
162     printf_stderr_common("\n");
163     printCallSite(file, line, function);
164 }
165 
WTFReportError(const char * file,int line,const char * function,const char * format,...)166 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
167 {
168     printf_stderr_common("ERROR: ");
169     va_list args;
170     va_start(args, format);
171     vprintf_stderr_common(format, args);
172     va_end(args);
173     printf_stderr_common("\n");
174     printCallSite(file, line, function);
175 }
176 
WTFLog(WTFLogChannel * channel,const char * format,...)177 void WTFLog(WTFLogChannel* channel, const char* format, ...)
178 {
179     if (channel->state != WTFLogChannelOn)
180         return;
181 
182     va_list args;
183     va_start(args, format);
184     vprintf_stderr_common(format, args);
185     va_end(args);
186     if (format[strlen(format) - 1] != '\n')
187         printf_stderr_common("\n");
188 }
189 
WTFLogVerbose(const char * file,int line,const char * function,WTFLogChannel * channel,const char * format,...)190 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
191 {
192     if (channel->state != WTFLogChannelOn)
193         return;
194 
195     va_list args;
196     va_start(args, format);
197     vprintf_stderr_common(format, args);
198     va_end(args);
199     if (format[strlen(format) - 1] != '\n')
200         printf_stderr_common("\n");
201     printCallSite(file, line, function);
202 }
203 
204 } // extern "C"
205