#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
#include "config.h"
#include "Assertions.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <CoreFoundation/CFString.h>
#if COMPILER(MSVC) && !OS(WINCE) && !PLATFORM(BREWMP)
#ifndef WINVER
#define WINVER 0x0500
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#include <crtdbg.h>
#endif
#if OS(WINDOWS)
#include <windows.h>
#endif
#if PLATFORM(BREWMP)
#include <AEEdbg.h>
#include <wtf/Vector.h>
#endif
#if PLATFORM(MAC)
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
#endif
extern "C" {
#if PLATFORM(BREWMP)
static void printLog(const Vector<char>& buffer)
{
#if COMPILER(MSVC)
const int printBufferSize = 128;
#else
const int printBufferSize = 32;
#endif
char printBuffer[printBufferSize + 1];
printBuffer[printBufferSize] = 0;
const char* p = buffer.data();
const char* end = buffer.data() + buffer.size();
while (p < end) {
strncpy(printBuffer, p, printBufferSize);
dbg_Message(printBuffer, DBG_MSG_LEVEL_HIGH, __FILE__, __LINE__);
p += printBufferSize;
}
}
#endif
WTF_ATTRIBUTE_PRINTF(1, 0)
static void vprintf_stderr_common(const char* format, va_list args)
{
if (strstr(format, "%@")) {
CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
char* buffer = (char*)malloc(length + 1);
CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
fputs(buffer, stderr);
free(buffer);
CFRelease(str);
CFRelease(cfFormat);
return;
}
#if OS(SYMBIAN)
vfprintf(stdout, format, args);
#else
vfprintf(stderr, format, args);
#endif
}
WTF_ATTRIBUTE_PRINTF(1, 2)
static void printf_stderr_common(const char* format, ...)
{
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
}
static void printCallSite(const char* file, int line, const char* function)
{
#if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
_CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
#else
printf_stderr_common("%s(%d) : %s\n", file, line, function);
#endif
}
void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
{
if (assertion)
printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
else
printf_stderr_common("SHOULD NEVER BE REACHED\n");
printCallSite(file, line, function);
}
void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
{
printf_stderr_common("ASSERTION FAILED: ");
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
printf_stderr_common("\n%s\n", assertion);
printCallSite(file, line, function);
}
void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
{
printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
printCallSite(file, line, function);
}
void WTFReportBacktrace()
{
#if PLATFORM(MAC)
static const int maxFrames = 32;
void* samples[maxFrames];
int frames = backtrace(samples, maxFrames);
for (int i = 1; i < frames; ++i) {
void* pointer = samples[i];
Dl_info info;
if (dladdr(pointer, &info) && info.dli_sname) {
const char* mangledName = info.dli_sname;
char* demangledName = abi::__cxa_demangle(mangledName, 0, 0, 0);
if (demangledName) {
fprintf(stderr, "%-3d %s\n", i, demangledName);
free(demangledName);
} else
fprintf(stderr, "%-3d %s\n", i, mangledName);
} else
fprintf(stderr, "%-3d %p\n", i, pointer);
}
#endif
}
void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
{
printf_stderr_common("FATAL ERROR: ");
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
printf_stderr_common("\n");
printCallSite(file, line, function);
}
void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
{
printf_stderr_common("ERROR: ");
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
printf_stderr_common("\n");
printCallSite(file, line, function);
}
void WTFLog(WTFLogChannel* channel, const char* format, ...)
{
if (channel->state != WTFLogChannelOn)
return;
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
size_t formatLength = strlen(format);
if (formatLength && format[formatLength - 1] != '\n')
printf_stderr_common("\n");
}
void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
{
if (channel->state != WTFLogChannelOn)
return;
va_list args;
va_start(args, format);
vprintf_stderr_common(format, args);
va_end(args);
size_t formatLength = strlen(format);
if (formatLength && format[formatLength - 1] != '\n')
printf_stderr_common("\n");
printCallSite(file, line, function);
}
}