| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2007-2009 Torch Mobile, Inc. | |
| 4 * Copyright (C) 2011 University of Szeged. All rights reserved. | |
| 5 * | |
| 6 * Redistribution and use in source and binary forms, with or without | |
| 7 * modification, are permitted provided that the following conditions | |
| 8 * are met: | |
| 9 * 1. Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 * notice, this list of conditions and the following disclaimer in the | |
| 13 * documentation and/or other materials provided with the distribution. | |
| 14 * | |
| 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 // The vprintf_stderr_common function triggers this error in the Mac build. | |
| 29 // Feel free to remove this pragma if this file builds on Mac. | |
| 30 // According to | |
| 31 // http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Diagnostic-Pragmas.html#Diagnosti
c-Pragmas | |
| 32 // we need to place this directive before any data or functions are defined. | |
| 33 #pragma GCC diagnostic ignored "-Wmissing-format-attribute" | |
| 34 | |
| 35 #include "wtf/Assertions.h" | |
| 36 | |
| 37 #include "wtf/Compiler.h" | |
| 38 #include "wtf/PtrUtil.h" | |
| 39 #include "wtf/ThreadSpecific.h" | |
| 40 #include "wtf/Threading.h" | |
| 41 #include <memory> | |
| 42 #include <stdarg.h> | |
| 43 #include <stdio.h> | |
| 44 #include <stdlib.h> | |
| 45 #include <string.h> | |
| 46 | |
| 47 #if OS(MACOSX) | |
| 48 #include <AvailabilityMacros.h> | |
| 49 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 | |
| 50 #define WTF_USE_APPLE_SYSTEM_LOG 1 | |
| 51 #include <asl.h> | |
| 52 #endif | |
| 53 #endif // OS(MACOSX) | |
| 54 | |
| 55 #if COMPILER(MSVC) | |
| 56 #include <crtdbg.h> | |
| 57 #endif | |
| 58 | |
| 59 #if OS(WIN) | |
| 60 #include <windows.h> | |
| 61 #endif | |
| 62 | |
| 63 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__)) | |
| 64 #include <cxxabi.h> | |
| 65 #include <dlfcn.h> | |
| 66 #include <execinfo.h> | |
| 67 #endif | |
| 68 | |
| 69 #if OS(ANDROID) | |
| 70 #include <android/log.h> | |
| 71 #endif | |
| 72 | |
| 73 PRINTF_FORMAT(1, 0) | |
| 74 static void vprintf_stderr_common(const char* format, va_list args) { | |
| 75 #if OS(MACOSX) && USE(APPLE_SYSTEM_LOG) | |
| 76 va_list copyOfArgs; | |
| 77 va_copy(copyOfArgs, args); | |
| 78 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs); | |
| 79 va_end(copyOfArgs); | |
| 80 #elif OS(ANDROID) | |
| 81 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args); | |
| 82 #elif OS(WIN) | |
| 83 if (IsDebuggerPresent()) { | |
| 84 size_t size = 1024; | |
| 85 | |
| 86 do { | |
| 87 char* buffer = (char*)malloc(size); | |
| 88 if (!buffer) | |
| 89 break; | |
| 90 | |
| 91 if (_vsnprintf(buffer, size, format, args) != -1) { | |
| 92 OutputDebugStringA(buffer); | |
| 93 free(buffer); | |
| 94 break; | |
| 95 } | |
| 96 | |
| 97 free(buffer); | |
| 98 size *= 2; | |
| 99 } while (size > 1024); | |
| 100 } | |
| 101 #endif | |
| 102 vfprintf(stderr, format, args); | |
| 103 } | |
| 104 | |
| 105 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0)) | |
| 106 #pragma GCC diagnostic push | |
| 107 #pragma GCC diagnostic ignored "-Wformat-nonliteral" | |
| 108 #endif | |
| 109 | |
| 110 static void vprintf_stderr_with_trailing_newline(const char* format, | |
| 111 va_list args) { | |
| 112 size_t formatLength = strlen(format); | |
| 113 if (formatLength && format[formatLength - 1] == '\n') { | |
| 114 vprintf_stderr_common(format, args); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 std::unique_ptr<char[]> formatWithNewline = | |
| 119 wrapArrayUnique(new char[formatLength + 2]); | |
| 120 memcpy(formatWithNewline.get(), format, formatLength); | |
| 121 formatWithNewline[formatLength] = '\n'; | |
| 122 formatWithNewline[formatLength + 1] = 0; | |
| 123 | |
| 124 vprintf_stderr_common(formatWithNewline.get(), args); | |
| 125 } | |
| 126 | |
| 127 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0)) | |
| 128 #pragma GCC diagnostic pop | |
| 129 #endif | |
| 130 | |
| 131 namespace { | |
| 132 | |
| 133 class FrameToNameScope { | |
| 134 public: | |
| 135 explicit FrameToNameScope(void*); | |
| 136 ~FrameToNameScope(); | |
| 137 const char* nullableName() { return m_name; } | |
| 138 | |
| 139 private: | |
| 140 const char* m_name; | |
| 141 char* m_cxaDemangled; | |
| 142 }; | |
| 143 | |
| 144 FrameToNameScope::FrameToNameScope(void* addr) : m_name(0), m_cxaDemangled(0) { | |
| 145 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__)) | |
| 146 Dl_info info; | |
| 147 if (!dladdr(addr, &info) || !info.dli_sname) | |
| 148 return; | |
| 149 const char* mangledName = info.dli_sname; | |
| 150 if ((m_cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0))) | |
| 151 m_name = m_cxaDemangled; | |
| 152 else | |
| 153 m_name = mangledName; | |
| 154 #else | |
| 155 (void)addr; | |
| 156 #endif | |
| 157 } | |
| 158 | |
| 159 FrameToNameScope::~FrameToNameScope() { | |
| 160 free(m_cxaDemangled); | |
| 161 } | |
| 162 | |
| 163 } // anonymous namespace | |
| 164 | |
| 165 #if !LOG_DISABLED | |
| 166 namespace WTF { | |
| 167 | |
| 168 ScopedLogger::ScopedLogger(bool condition, const char* format, ...) | |
| 169 : m_parent(condition ? current() : 0), m_multiline(false) { | |
| 170 if (!condition) | |
| 171 return; | |
| 172 | |
| 173 va_list args; | |
| 174 va_start(args, format); | |
| 175 init(format, args); | |
| 176 va_end(args); | |
| 177 } | |
| 178 | |
| 179 ScopedLogger::~ScopedLogger() { | |
| 180 if (current() == this) { | |
| 181 if (m_multiline) | |
| 182 indent(); | |
| 183 else | |
| 184 print(" "); | |
| 185 print(")\n"); | |
| 186 current() = m_parent; | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 void ScopedLogger::setPrintFuncForTests(PrintFunctionPtr ptr) { | |
| 191 m_printFunc = ptr; | |
| 192 }; | |
| 193 | |
| 194 void ScopedLogger::init(const char* format, va_list args) { | |
| 195 current() = this; | |
| 196 if (m_parent) | |
| 197 m_parent->writeNewlineIfNeeded(); | |
| 198 indent(); | |
| 199 print("( "); | |
| 200 m_printFunc(format, args); | |
| 201 } | |
| 202 | |
| 203 void ScopedLogger::writeNewlineIfNeeded() { | |
| 204 if (!m_multiline) { | |
| 205 print("\n"); | |
| 206 m_multiline = true; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 void ScopedLogger::indent() { | |
| 211 if (m_parent) { | |
| 212 m_parent->indent(); | |
| 213 printIndent(); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 void ScopedLogger::log(const char* format, ...) { | |
| 218 if (current() != this) | |
| 219 return; | |
| 220 | |
| 221 va_list args; | |
| 222 va_start(args, format); | |
| 223 | |
| 224 writeNewlineIfNeeded(); | |
| 225 indent(); | |
| 226 printIndent(); | |
| 227 m_printFunc(format, args); | |
| 228 print("\n"); | |
| 229 | |
| 230 va_end(args); | |
| 231 } | |
| 232 | |
| 233 void ScopedLogger::print(const char* format, ...) { | |
| 234 va_list args; | |
| 235 va_start(args, format); | |
| 236 m_printFunc(format, args); | |
| 237 va_end(args); | |
| 238 } | |
| 239 | |
| 240 void ScopedLogger::printIndent() { | |
| 241 print(" "); | |
| 242 } | |
| 243 | |
| 244 ScopedLogger*& ScopedLogger::current() { | |
| 245 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<ScopedLogger*>, ref, | |
| 246 new ThreadSpecific<ScopedLogger*>); | |
| 247 return *ref; | |
| 248 } | |
| 249 | |
| 250 ScopedLogger::PrintFunctionPtr ScopedLogger::m_printFunc = | |
| 251 vprintf_stderr_common; | |
| 252 | |
| 253 } // namespace WTF | |
| 254 #endif // !LOG_DISABLED | |
| 255 | |
| 256 void WTFLogAlways(const char* format, ...) { | |
| 257 va_list args; | |
| 258 va_start(args, format); | |
| 259 vprintf_stderr_with_trailing_newline(format, args); | |
| 260 va_end(args); | |
| 261 } | |
| OLD | NEW |