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