-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trace.h
121 lines (84 loc) · 2.18 KB
/
Trace.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#include <sstream>
#include <string>
template<const wchar_t * FunctionName>
class Trace
{
protected:
static int _depth;
LARGE_INTEGER start_time;
std::wstring m_message;
int m_id;
public:
Trace(std::wstring s);
Trace(std::wstring s, int id);
~Trace();
void WriteMessage(std::wstring &s);
void Write(std::wstringstream &ss);
};
#define TRACE() \
Trace trace(__FUNCTIONW__);
#define TRACEWITHMESSAGE(message) \
Trace trace((message));
#define TRACEWRITE(s) \
trace.WriteMessage(std::wstring(s) );
int Trace::_depth = 1;
template<const wchar_t * FunctionName>
Trace<FunctionName>::Trace(std::wstring s)
{
m_id = -1;
std::wstringstream ss;
ss.str(L"");
for(int i = 0; i < _depth; ++i) ss << ".";
ss << L"Entering: " << m_message;
Write(ss);
_depth++;
QueryPerformanceCounter(&start_time);
}
template<const wchar_t * FunctionName>
Trace<FunctionName>::Trace(std::wstring s, int id)
{
std::wstringstream ss;
ss.str(L"");
ss << id << L":" << s;
m_message = std::wstring(ss.str());
m_id = id;
ss.str(L"");
for(int i = 0; i < _depth; ++i) ss << ".";
ss << L"Entering: " << m_message;
Write(ss);
_depth++;
QueryPerformanceCounter(&start_time);
}
template<const wchar_t * FunctionName>
Trace<FunctionName>::~Trace(void)
{
LARGE_INTEGER end_time;
LARGE_INTEGER freq;
QueryPerformanceCounter(&end_time);
double diff = (double)(end_time.QuadPart - start_time.QuadPart);
QueryPerformanceFrequency(&freq);
double time = (diff / (double)freq.QuadPart) * 1000.0;
--_depth;
std::wstringstream ss;
ss.str(L"");
for(int i = 0; i < _depth; ++i) ss << ".";
ss << L"Exiting: " << m_message << L" (" << time << L" ms)";
Write(ss);
}
template<const wchar_t * FunctionName>
void Trace<FunctionName>::Write(std::wstringstream &ss)
{
ss << std::endl;
std::wstring s = ss.str();
OutputDebugString(s.c_str());
}
template<const wchar_t * FunctionName>
void Trace<FunctionName>::WriteMessage(std::wstring &s)
{
std::wstringstream ss;
ss.str(L"");
// for(int i = 0; i < _depth; ++i) ss << ".";
ss << FunctionName << L": " << s;
Write(ss);
}