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 | #if PLATFORM(MAC)
|
---|
35 | #include <CoreFoundation/CFString.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #if COMPILER(MSVC)
|
---|
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 | extern "C" {
|
---|
50 |
|
---|
51 | WTF_ATTRIBUTE_PRINTF(1, 0)
|
---|
52 | static void vprintf_stderr_common(const char* format, va_list args)
|
---|
53 | {
|
---|
54 | #if PLATFORM(MAC)
|
---|
55 | if (strstr(format, "%@")) {
|
---|
56 | CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
|
---|
57 | CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
|
---|
58 |
|
---|
59 | int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
|
---|
60 | char* buffer = (char*)malloc(length + 1);
|
---|
61 |
|
---|
62 | CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
|
---|
63 |
|
---|
64 | fputs(buffer, stderr);
|
---|
65 |
|
---|
66 | free(buffer);
|
---|
67 | CFRelease(str);
|
---|
68 | CFRelease(cfFormat);
|
---|
69 | } else
|
---|
70 | #elif COMPILER(MSVC)
|
---|
71 | if (IsDebuggerPresent()) {
|
---|
72 | size_t size = 1024;
|
---|
73 |
|
---|
74 | do {
|
---|
75 | char* buffer = (char*)malloc(size);
|
---|
76 |
|
---|
77 | if (buffer == NULL)
|
---|
78 | break;
|
---|
79 |
|
---|
80 | if (_vsnprintf(buffer, size, format, args) != -1) {
|
---|
81 | OutputDebugStringA(buffer);
|
---|
82 | free(buffer);
|
---|
83 | break;
|
---|
84 | }
|
---|
85 |
|
---|
86 | free(buffer);
|
---|
87 | size *= 2;
|
---|
88 | } while (size > 1024);
|
---|
89 | }
|
---|
90 | #endif
|
---|
91 | vfprintf(stderr, format, args);
|
---|
92 | }
|
---|
93 |
|
---|
94 | WTF_ATTRIBUTE_PRINTF(1, 2)
|
---|
95 | static void printf_stderr_common(const char* format, ...)
|
---|
96 | {
|
---|
97 | va_list args;
|
---|
98 | va_start(args, format);
|
---|
99 | vprintf_stderr_common(format, args);
|
---|
100 | va_end(args);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void printCallSite(const char* file, int line, const char* function)
|
---|
104 | {
|
---|
105 | #if PLATFORM(WIN) && defined _DEBUG
|
---|
106 | _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
|
---|
107 | #else
|
---|
108 | printf_stderr_common("(%s:%d %s)\n", file, line, function);
|
---|
109 | #endif
|
---|
110 | }
|
---|
111 |
|
---|
112 | void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
|
---|
113 | {
|
---|
114 | if (assertion)
|
---|
115 | printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
|
---|
116 | else
|
---|
117 | printf_stderr_common("SHOULD NEVER BE REACHED\n");
|
---|
118 | printCallSite(file, line, function);
|
---|
119 | }
|
---|
120 |
|
---|
121 | void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
|
---|
122 | {
|
---|
123 | printf_stderr_common("ASSERTION FAILED: ");
|
---|
124 | va_list args;
|
---|
125 | va_start(args, format);
|
---|
126 | vprintf_stderr_common(format, args);
|
---|
127 | va_end(args);
|
---|
128 | printf_stderr_common("\n%s\n", assertion);
|
---|
129 | printCallSite(file, line, function);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
|
---|
133 | {
|
---|
134 | printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
|
---|
135 | printCallSite(file, line, function);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
|
---|
139 | {
|
---|
140 | printf_stderr_common("FATAL ERROR: ");
|
---|
141 | va_list args;
|
---|
142 | va_start(args, format);
|
---|
143 | vprintf_stderr_common(format, args);
|
---|
144 | va_end(args);
|
---|
145 | printf_stderr_common("\n");
|
---|
146 | printCallSite(file, line, function);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
|
---|
150 | {
|
---|
151 | printf_stderr_common("ERROR: ");
|
---|
152 | va_list args;
|
---|
153 | va_start(args, format);
|
---|
154 | vprintf_stderr_common(format, args);
|
---|
155 | va_end(args);
|
---|
156 | printf_stderr_common("\n");
|
---|
157 | printCallSite(file, line, function);
|
---|
158 | }
|
---|
159 |
|
---|
160 | void WTFLog(WTFLogChannel* channel, const char* format, ...)
|
---|
161 | {
|
---|
162 | if (channel->state != WTFLogChannelOn)
|
---|
163 | return;
|
---|
164 |
|
---|
165 | va_list args;
|
---|
166 | va_start(args, format);
|
---|
167 | vprintf_stderr_common(format, args);
|
---|
168 | va_end(args);
|
---|
169 | if (format[strlen(format) - 1] != '\n')
|
---|
170 | printf_stderr_common("\n");
|
---|
171 | }
|
---|
172 |
|
---|
173 | void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
|
---|
174 | {
|
---|
175 | if (channel->state != WTFLogChannelOn)
|
---|
176 | return;
|
---|
177 |
|
---|
178 | va_list args;
|
---|
179 | va_start(args, format);
|
---|
180 | vprintf_stderr_common(format, args);
|
---|
181 | va_end(args);
|
---|
182 | if (format[strlen(format) - 1] != '\n')
|
---|
183 | printf_stderr_common("\n");
|
---|
184 | printCallSite(file, line, function);
|
---|
185 | }
|
---|
186 |
|
---|
187 | } // extern "C"
|
---|