daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // debug.cpp: Debugging utilities. |
| 8 | |
| 9 | #include "common/debug.h" |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 10 | |
Geoff Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 11 | #include <stdarg.h> |
Jamie Madill | e31fd87 | 2016-05-27 08:35:36 -0400 | [diff] [blame] | 12 | |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 13 | #include <array> |
Geoff Lang | feee44b | 2014-03-05 11:34:31 -0500 | [diff] [blame] | 14 | #include <cstdio> |
Jamie Madill | e31fd87 | 2016-05-27 08:35:36 -0400 | [diff] [blame] | 15 | #include <fstream> |
| 16 | #include <iostream> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "common/angleutils.h" |
| 20 | #include "common/platform.h" |
| 21 | #include "common/Optional.h" |
Geoff Lang | 6850947 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 22 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 23 | namespace gl |
| 24 | { |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 25 | |
| 26 | namespace |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 27 | { |
Jamie Madill | e31fd87 | 2016-05-27 08:35:36 -0400 | [diff] [blame] | 28 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 29 | DebugAnnotator *g_debugAnnotator = nullptr; |
| 30 | |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 31 | constexpr std::array<const char *, LOG_NUM_SEVERITIES> g_logSeverityNames = { |
| 32 | {"EVENT", "WARN", "ERR"}}; |
| 33 | |
| 34 | constexpr const char *LogSeverityName(int severity) |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 35 | { |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 36 | return (severity >= 0 && severity < LOG_NUM_SEVERITIES) ? g_logSeverityNames[severity] |
| 37 | : "UNKNOWN"; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 40 | } // namespace |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 41 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 42 | bool DebugAnnotationsActive() |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 43 | { |
Austin Kinross | 570e83c | 2014-10-20 14:13:58 -0700 | [diff] [blame] | 44 | #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 45 | return g_debugAnnotator != nullptr && g_debugAnnotator->getStatus(); |
Geoff Lang | f571312 | 2013-10-07 17:06:30 -0400 | [diff] [blame] | 46 | #else |
| 47 | return false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 48 | #endif |
| 49 | } |
| 50 | |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 51 | void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator) |
| 52 | { |
| 53 | UninitializeDebugAnnotations(); |
| 54 | g_debugAnnotator = debugAnnotator; |
| 55 | } |
| 56 | |
| 57 | void UninitializeDebugAnnotations() |
| 58 | { |
| 59 | // Pointer is not managed. |
| 60 | g_debugAnnotator = nullptr; |
| 61 | } |
| 62 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 63 | ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) |
| 64 | { |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 65 | #if !defined(ANGLE_ENABLE_DEBUG_TRACE) |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 66 | if (!DebugAnnotationsActive()) |
daniel@transgaming.com | 5dc3b8b | 2012-11-28 19:43:49 +0000 | [diff] [blame] | 67 | { |
| 68 | return; |
| 69 | } |
Austin Kinross | f0360c6 | 2014-10-20 14:26:13 -0700 | [diff] [blame] | 70 | #endif // !ANGLE_ENABLE_DEBUG_TRACE |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 71 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 72 | va_list vararg; |
| 73 | va_start(vararg, format); |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 74 | std::vector<char> buffer(512); |
| 75 | size_t len = FormatStringIntoVector(format, vararg, buffer); |
| 76 | ANGLE_LOG(EVENT) << std::string(&buffer[0], len); |
Jamie Madill | b60fe31 | 2014-09-26 14:56:41 -0400 | [diff] [blame] | 77 | va_end(vararg); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | ScopedPerfEventHelper::~ScopedPerfEventHelper() |
| 81 | { |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 82 | if (DebugAnnotationsActive()) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 83 | { |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 84 | g_debugAnnotator->endEvent(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 85 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 86 | } |
Jamie Madill | 2c7c625 | 2015-03-12 14:15:38 -0400 | [diff] [blame] | 87 | |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 88 | namespace priv |
| 89 | { |
| 90 | |
Jamie Madill | 60e6edf | 2016-10-31 12:17:19 -0400 | [diff] [blame] | 91 | std::ostream &DummyStream() |
| 92 | { |
| 93 | return std::cout; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 94 | } |
Jamie Madill | 60e6edf | 2016-10-31 12:17:19 -0400 | [diff] [blame] | 95 | |
Yuly Novikov | afcc41c | 2016-12-13 12:59:39 -0500 | [diff] [blame^] | 96 | bool ShouldCreateLogMessage(LogSeverity severity) |
| 97 | { |
| 98 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) || defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) |
| 99 | return true; |
| 100 | #elif defined(ANGLE_ENABLE_ASSERTS) |
| 101 | return severity == LOG_ERR; |
| 102 | #else |
| 103 | return false; |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | LogMessage::LogMessage(const char *function, int line, LogSeverity severity) |
| 108 | : mSeverity(severity), mFunction(function), mLine(line) |
| 109 | { |
| 110 | init(function, line); |
| 111 | } |
| 112 | |
| 113 | LogMessage::~LogMessage() |
| 114 | { |
| 115 | mStream << std::endl; |
| 116 | std::string str(mStream.str()); |
| 117 | |
| 118 | if (DebugAnnotationsActive()) |
| 119 | { |
| 120 | std::wstring formattedWideMessage(str.begin(), str.end()); |
| 121 | |
| 122 | switch (mSeverity) |
| 123 | { |
| 124 | case LOG_EVENT: |
| 125 | g_debugAnnotator->beginEvent(formattedWideMessage.c_str()); |
| 126 | break; |
| 127 | default: |
| 128 | g_debugAnnotator->setMarker(formattedWideMessage.c_str()); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Give any log message handler first dibs on the message. |
| 134 | bool handled = g_debugAnnotator != nullptr && |
| 135 | g_debugAnnotator->logMessage(mSeverity, mFunction, mLine, mMessageStart, str); |
| 136 | |
| 137 | if (!handled && mSeverity == LOG_ERR) |
| 138 | { |
| 139 | std::cerr << str; |
| 140 | #if !defined(NDEBUG) && defined(_MSC_VER) |
| 141 | OutputDebugStringA(str.c_str()); |
| 142 | #endif // !defined(NDEBUG) && defined(_MSC_VER) |
| 143 | } |
| 144 | |
| 145 | #if defined(ANGLE_ENABLE_DEBUG_TRACE) |
| 146 | #if defined(NDEBUG) |
| 147 | if (mSeverity == LOG_EVENT || mSeverity == LOG_WARN) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | #endif // NDEBUG |
| 152 | static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app); |
| 153 | if (file) |
| 154 | { |
| 155 | file.write(str.c_str(), str.length()); |
| 156 | file.flush(); |
| 157 | } |
| 158 | |
| 159 | #if defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER) |
| 160 | OutputDebugStringA(str.c_str()); |
| 161 | #endif // ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER |
| 162 | |
| 163 | #endif // ANGLE_ENABLE_DEBUG_TRACE |
| 164 | } |
| 165 | |
| 166 | // writes the common header info to the stream |
| 167 | void LogMessage::init(const char *function, int line) |
| 168 | { |
| 169 | if (mSeverity >= 0) |
| 170 | mStream << LogSeverityName(mSeverity); |
| 171 | else |
| 172 | mStream << "VERBOSE" << -mSeverity; |
| 173 | |
| 174 | mStream << ": " << function << "(" << line << "): "; |
| 175 | |
| 176 | mMessageStart = mStream.str().length(); |
| 177 | } |
| 178 | |
| 179 | } // namespace priv |
| 180 | |
| 181 | #if defined(ANGLE_PLATFORM_WINDOWS) |
| 182 | std::ostream &operator<<(std::ostream &os, const FmtHR &fmt) |
| 183 | { |
| 184 | os << "HRESULT: "; |
| 185 | return FmtHexInt(os, fmt.mHR); |
| 186 | } |
| 187 | #endif // defined(ANGLE_PLATFORM_WINDOWS) |
| 188 | |
Jamie Madill | 60e6edf | 2016-10-31 12:17:19 -0400 | [diff] [blame] | 189 | } // namespace gl |