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