]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Assertions.cpp
JavaScriptCore-621.1.tar.gz
[apple/javascriptcore.git] / wtf / Assertions.cpp
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 #include <CoreFoundation/CFString.h>
35
36 #if COMPILER(MSVC) && !OS(WINCE)
37 #ifndef WINVER
38 #define WINVER 0x0500
39 #endif
40 #ifndef _WIN32_WINNT
41 #define _WIN32_WINNT 0x0500
42 #endif
43 #include <windows.h>
44 #include <crtdbg.h>
45 #endif
46
47 #if OS(WINCE)
48 #include <winbase.h>
49 #endif
50
51 extern "C" {
52
53 WTF_ATTRIBUTE_PRINTF(1, 0)
54 static void vprintf_stderr_common(const char* format, va_list args)
55 {
56 if (strstr(format, "%@")) {
57 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
58 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
59
60 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
61 char* buffer = (char*)malloc(length + 1);
62
63 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
64
65 fputs(buffer, stderr);
66
67 free(buffer);
68 CFRelease(str);
69 CFRelease(cfFormat);
70 } else
71 #if OS(SYMBIAN)
72 vfprintf(stdout, format, args);
73 #else
74 vfprintf(stderr, format, args);
75 #endif
76 }
77
78 WTF_ATTRIBUTE_PRINTF(1, 2)
79 static void printf_stderr_common(const char* format, ...)
80 {
81 va_list args;
82 va_start(args, format);
83 vprintf_stderr_common(format, args);
84 va_end(args);
85 }
86
87 static void printCallSite(const char* file, int line, const char* function)
88 {
89 #if OS(WIN) && !OS(WINCE) && defined _DEBUG
90 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
91 #else
92 printf_stderr_common("(%s:%d %s)\n", file, line, function);
93 #endif
94 }
95
96 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
97 {
98 if (assertion)
99 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
100 else
101 printf_stderr_common("SHOULD NEVER BE REACHED\n");
102 printCallSite(file, line, function);
103 }
104
105 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
106 {
107 printf_stderr_common("ASSERTION FAILED: ");
108 va_list args;
109 va_start(args, format);
110 vprintf_stderr_common(format, args);
111 va_end(args);
112 printf_stderr_common("\n%s\n", assertion);
113 printCallSite(file, line, function);
114 }
115
116 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
117 {
118 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
119 printCallSite(file, line, function);
120 }
121
122 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
123 {
124 printf_stderr_common("FATAL ERROR: ");
125 va_list args;
126 va_start(args, format);
127 vprintf_stderr_common(format, args);
128 va_end(args);
129 printf_stderr_common("\n");
130 printCallSite(file, line, function);
131 }
132
133 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
134 {
135 printf_stderr_common("ERROR: ");
136 va_list args;
137 va_start(args, format);
138 vprintf_stderr_common(format, args);
139 va_end(args);
140 printf_stderr_common("\n");
141 printCallSite(file, line, function);
142 }
143
144 void WTFLog(WTFLogChannel* channel, const char* format, ...)
145 {
146 if (channel->state != WTFLogChannelOn)
147 return;
148
149 va_list args;
150 va_start(args, format);
151 vprintf_stderr_common(format, args);
152 va_end(args);
153 if (format[strlen(format) - 1] != '\n')
154 printf_stderr_common("\n");
155 }
156
157 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
158 {
159 if (channel->state != WTFLogChannelOn)
160 return;
161
162 va_list args;
163 va_start(args, format);
164 vprintf_stderr_common(format, args);
165 va_end(args);
166 if (format[strlen(format) - 1] != '\n')
167 printf_stderr_common("\n");
168 printCallSite(file, line, function);
169 }
170
171 } // extern "C"