diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/ThirdParty/ANGLE/src/common/debug.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/ThirdParty/ANGLE/src/common/debug.cpp')
-rw-r--r-- | Source/ThirdParty/ANGLE/src/common/debug.cpp | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/Source/ThirdParty/ANGLE/src/common/debug.cpp b/Source/ThirdParty/ANGLE/src/common/debug.cpp new file mode 100644 index 000000000..dc49d24c5 --- /dev/null +++ b/Source/ThirdParty/ANGLE/src/common/debug.cpp @@ -0,0 +1,177 @@ +// +// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// + +// debug.cpp: Debugging utilities. + +#include "common/debug.h" + +#include <stdarg.h> + +#include <cstdio> +#include <fstream> +#include <iostream> +#include <vector> + +#include "common/angleutils.h" +#include "common/platform.h" +#include "common/Optional.h" + +namespace gl +{ + +namespace +{ + +class FormattedString final : angle::NonCopyable +{ + public: + FormattedString(const char *format, va_list vararg) : mFormat(format) + { + va_copy(mVarArg, vararg); + } + + const char *c_str() { return str().c_str(); } + + const std::string &str() + { + if (!mMessage.valid()) + { + mMessage = FormatString(mFormat, mVarArg); + } + return mMessage.value(); + } + + size_t length() + { + c_str(); + return mMessage.value().length(); + } + + private: + const char *mFormat; + va_list mVarArg; + Optional<std::string> mMessage; +}; +enum DebugTraceOutputType +{ + DebugTraceOutputTypeNone, + DebugTraceOutputTypeSetMarker, + DebugTraceOutputTypeBeginEvent +}; + +DebugAnnotator *g_debugAnnotator = nullptr; + +void output(bool traceInDebugOnly, MessageType messageType, DebugTraceOutputType outputType, + const char *format, va_list vararg) +{ + if (DebugAnnotationsActive()) + { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wexit-time-destructors" + static std::vector<char> buffer(512); +#pragma clang diagnostic pop + size_t len = FormatStringIntoVector(format, vararg, buffer); + std::wstring formattedWideMessage(buffer.begin(), buffer.begin() + len); + + ASSERT(g_debugAnnotator != nullptr); + switch (outputType) + { + case DebugTraceOutputTypeNone: + break; + case DebugTraceOutputTypeBeginEvent: + g_debugAnnotator->beginEvent(formattedWideMessage.c_str()); + break; + case DebugTraceOutputTypeSetMarker: + g_debugAnnotator->setMarker(formattedWideMessage.c_str()); + break; + } + } + + FormattedString formattedMessage(format, vararg); + + if (messageType == MESSAGE_ERR) + { + std::cerr << formattedMessage.c_str(); +#if !defined(NDEBUG) && defined(_MSC_VER) + OutputDebugStringA(formattedMessage.c_str()); +#endif // !defined(NDEBUG) && defined(_MSC_VER) + } + +#if defined(ANGLE_ENABLE_DEBUG_TRACE) +#if defined(NDEBUG) + if (traceInDebugOnly) + { + return; + } +#endif // NDEBUG + static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app); + if (file) + { + file.write(formattedMessage.c_str(), formattedMessage.length()); + file.flush(); + } + +#if defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER) + OutputDebugStringA(formattedMessage.c_str()); +#endif // ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER + +#endif // ANGLE_ENABLE_DEBUG_TRACE +} + +} // namespace + +bool DebugAnnotationsActive() +{ +#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) + return g_debugAnnotator != nullptr && g_debugAnnotator->getStatus(); +#else + return false; +#endif +} + +void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator) +{ + UninitializeDebugAnnotations(); + g_debugAnnotator = debugAnnotator; +} + +void UninitializeDebugAnnotations() +{ + // Pointer is not managed. + g_debugAnnotator = nullptr; +} + +void trace(bool traceInDebugOnly, MessageType messageType, const char *format, ...) +{ + va_list vararg; + va_start(vararg, format); + output(traceInDebugOnly, messageType, DebugTraceOutputTypeSetMarker, format, vararg); + va_end(vararg); +} + +ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) +{ +#if !defined(ANGLE_ENABLE_DEBUG_TRACE) + if (!DebugAnnotationsActive()) + { + return; + } +#endif // !ANGLE_ENABLE_DEBUG_TRACE + va_list vararg; + va_start(vararg, format); + output(true, MESSAGE_EVENT, DebugTraceOutputTypeBeginEvent, format, vararg); + va_end(vararg); +} + +ScopedPerfEventHelper::~ScopedPerfEventHelper() +{ + if (DebugAnnotationsActive()) + { + g_debugAnnotator->endEvent(); + } +} + +} |