michael@0: // michael@0: // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // debug.cpp: Debugging utilities. michael@0: michael@0: #include "common/debug.h" michael@0: #include "common/system.h" michael@0: #include michael@0: michael@0: namespace gl michael@0: { michael@0: michael@0: typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR); michael@0: michael@0: static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg) michael@0: { michael@0: #if !defined(ANGLE_DISABLE_PERF) michael@0: if (perfActive()) michael@0: { michael@0: char message[32768]; michael@0: int len = vsprintf_s(message, format, vararg); michael@0: if (len < 0) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: // There are no ASCII variants of these D3DPERF functions. michael@0: wchar_t wideMessage[32768]; michael@0: for (int i = 0; i < len; ++i) michael@0: { michael@0: wideMessage[i] = message[i]; michael@0: } michael@0: wideMessage[len] = 0; michael@0: michael@0: perfFunc(0, wideMessage); michael@0: } michael@0: #endif michael@0: michael@0: #if !defined(ANGLE_DISABLE_TRACE) michael@0: #if defined(NDEBUG) michael@0: if (traceFileDebugOnly) michael@0: { michael@0: return; michael@0: } michael@0: #endif michael@0: michael@0: FILE* file = fopen(TRACE_OUTPUT_FILE, "a"); michael@0: if (file) michael@0: { michael@0: vfprintf(file, format, vararg); michael@0: fclose(file); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: void trace(bool traceFileDebugOnly, const char *format, ...) michael@0: { michael@0: va_list vararg; michael@0: va_start(vararg, format); michael@0: #if defined(ANGLE_DISABLE_PERF) michael@0: output(traceFileDebugOnly, NULL, format, vararg); michael@0: #else michael@0: output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg); michael@0: #endif michael@0: va_end(vararg); michael@0: } michael@0: michael@0: bool perfActive() michael@0: { michael@0: #if defined(ANGLE_DISABLE_PERF) michael@0: return false; michael@0: #else michael@0: static bool active = D3DPERF_GetStatus() != 0; michael@0: return active; michael@0: #endif michael@0: } michael@0: michael@0: ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...) michael@0: { michael@0: #if !defined(ANGLE_DISABLE_PERF) michael@0: #if defined(ANGLE_DISABLE_TRACE) michael@0: if (!perfActive()) michael@0: { michael@0: return; michael@0: } michael@0: #endif michael@0: va_list vararg; michael@0: va_start(vararg, format); michael@0: output(true, reinterpret_cast(D3DPERF_BeginEvent), format, vararg); michael@0: va_end(vararg); michael@0: #endif michael@0: } michael@0: michael@0: ScopedPerfEventHelper::~ScopedPerfEventHelper() michael@0: { michael@0: #if !defined(ANGLE_DISABLE_PERF) michael@0: if (perfActive()) michael@0: { michael@0: D3DPERF_EndEvent(); michael@0: } michael@0: #endif michael@0: } michael@0: }