Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / common / debug.cpp
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"
10 #include "common/platform.h"
11 #include "common/angleutils.h"
12
13 #include <stdarg.h>
14 #include <vector>
15 #include <fstream>
16 #include <cstdio>
17
18 namespace gl
19 {
20 #if defined(ANGLE_ENABLE_PERF)
21 typedef void (WINAPI *PerfOutputFunction)(D3DCOLOR, LPCWSTR);
22 #else
23 typedef void (*PerfOutputFunction)(unsigned int, const wchar_t*);
24 #endif
25
26 static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg)
27 {
28 #if defined(ANGLE_ENABLE_PERF) || defined(ANGLE_ENABLE_TRACE)
29     std::string formattedMessage = FormatString(format, vararg);
30 #endif
31
32 #if defined(ANGLE_ENABLE_PERF)
33     if (perfActive())
34     {
35         // The perf function only accepts wide strings, widen the ascii message
36         static std::wstring wideMessage;
37         if (wideMessage.capacity() < formattedMessage.length())
38         {
39             wideMessage.reserve(formattedMessage.size());
40         }
41
42         wideMessage.assign(formattedMessage.begin(), formattedMessage.end());
43
44         perfFunc(0, wideMessage.c_str());
45     }
46 #endif // ANGLE_ENABLE_PERF
47
48 #if defined(ANGLE_ENABLE_TRACE)
49 #if defined(NDEBUG)
50     if (traceFileDebugOnly)
51     {
52         return;
53     }
54 #endif // NDEBUG
55
56     static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app);
57     if (file)
58     {
59         file.write(formattedMessage.c_str(), formattedMessage.length());
60         file.flush();
61     }
62
63 #endif // ANGLE_ENABLE_TRACE
64 }
65
66 void trace(bool traceFileDebugOnly, const char *format, ...)
67 {
68     va_list vararg;
69     va_start(vararg, format);
70 #if defined(ANGLE_ENABLE_PERF)
71     output(traceFileDebugOnly, D3DPERF_SetMarker, format, vararg);
72 #else
73     output(traceFileDebugOnly, NULL, format, vararg);
74 #endif
75     va_end(vararg);
76 }
77
78 bool perfActive()
79 {
80 #if defined(ANGLE_ENABLE_PERF)
81     static bool active = D3DPERF_GetStatus() != 0;
82     return active;
83 #else
84     return false;
85 #endif
86 }
87
88 ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
89 {
90 #if defined(ANGLE_ENABLE_PERF)
91 #if !defined(ANGLE_ENABLE_TRACE)
92     if (!perfActive())
93     {
94         return;
95     }
96 #endif // !ANGLE_ENABLE_TRACE
97     va_list vararg;
98     va_start(vararg, format);
99     output(true, reinterpret_cast<PerfOutputFunction>(D3DPERF_BeginEvent), format, vararg);
100     va_end(vararg);
101 #endif // ANGLE_ENABLE_PERF
102 }
103
104 ScopedPerfEventHelper::~ScopedPerfEventHelper()
105 {
106 #if defined(ANGLE_ENABLE_PERF)
107     if (perfActive())
108     {
109         D3DPERF_EndEvent();
110     }
111 #endif
112 }
113 }