1 | /*
|
---|
2 | Tracer.h
|
---|
3 |
|
---|
4 | Copyright (C) 2002-2004 René Nyffenegger
|
---|
5 |
|
---|
6 | This source code is provided 'as-is', without any express or implied
|
---|
7 | warranty. In no event will the author be held liable for any damages
|
---|
8 | arising from the use of this software.
|
---|
9 |
|
---|
10 | Permission is granted to anyone to use this software for any purpose,
|
---|
11 | including commercial applications, and to alter it and redistribute it
|
---|
12 | freely, subject to the following restrictions:
|
---|
13 |
|
---|
14 | 1. The origin of this source code must not be misrepresented; you must not
|
---|
15 | claim that you wrote the original source code. If you use this source code
|
---|
16 | in a product, an acknowledgment in the product documentation would be
|
---|
17 | appreciated but is not required.
|
---|
18 |
|
---|
19 | 2. Altered source versions must be plainly marked as such, and must not be
|
---|
20 | misrepresented as being the original source code.
|
---|
21 |
|
---|
22 | 3. This notice may not be removed or altered from any source distribution.
|
---|
23 |
|
---|
24 | René Nyffenegger [email protected]
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef __TRACER_H__
|
---|
28 | #define __TRACER_H__
|
---|
29 |
|
---|
30 | #ifdef DO_TRACE
|
---|
31 |
|
---|
32 | #include <string>
|
---|
33 | #include <sstream>
|
---|
34 | #include <windows.h>
|
---|
35 |
|
---|
36 | #define StartTrace(x) TraceFunc_::StartTrace_(x)
|
---|
37 | #define Trace(x) dummy_____for_trace_func______.Trace_(x)
|
---|
38 | #define Trace2(x,y) dummy_____for_trace_func______.Trace_(x,y)
|
---|
39 | #define TraceFunc(x) TraceFunc_ dummy_____for_trace_func______(x)
|
---|
40 | #define TraceFunc2(x,y) TraceFunc_ dummy_____for_trace_func______(x,y)
|
---|
41 |
|
---|
42 | class TraceFunc_
|
---|
43 | {
|
---|
44 | std::string func_name_;
|
---|
45 | public:
|
---|
46 | TraceFunc_(std::string const&);
|
---|
47 | TraceFunc_(std::string const&, std::string const&);
|
---|
48 | ~TraceFunc_();
|
---|
49 |
|
---|
50 | static void StartTrace_(std::string const& file_name);
|
---|
51 |
|
---|
52 | template <typename T>
|
---|
53 | void Trace_(T const& t)
|
---|
54 | {
|
---|
55 | DWORD d;
|
---|
56 | std::string indent_s;
|
---|
57 | std::stringstream s;
|
---|
58 |
|
---|
59 | s << t;
|
---|
60 |
|
---|
61 | for (int i=0; i< indent; i++) indent_s += " ";
|
---|
62 |
|
---|
63 | ::WriteFile(trace_file_,indent_s.c_str(), indent_s.size(), &d, 0);
|
---|
64 | ::WriteFile(trace_file_, s.str().c_str(), s.str().size() ,&d, 0);
|
---|
65 | ::WriteFile(trace_file_,"\x0a",1,&d,0);
|
---|
66 | }
|
---|
67 |
|
---|
68 | template <class T, class U>
|
---|
69 | void Trace_(T const& t, U const& u)
|
---|
70 | {
|
---|
71 | DWORD d;
|
---|
72 | std::string indent_s;
|
---|
73 | std::stringstream s;
|
---|
74 |
|
---|
75 | s << t;
|
---|
76 | s << u;
|
---|
77 |
|
---|
78 | for (int i=0; i< indent; i++) indent_s += " ";
|
---|
79 |
|
---|
80 | ::WriteFile(trace_file_,indent_s.c_str(), indent_s.size(), &d, 0);
|
---|
81 | ::WriteFile(trace_file_, s.str().c_str(), s.str().size() ,&d, 0);
|
---|
82 | ::WriteFile(trace_file_,"\x0a",1,&d,0);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static int indent;
|
---|
86 | static HANDLE trace_file_;
|
---|
87 | };
|
---|
88 |
|
---|
89 | #else
|
---|
90 |
|
---|
91 | #define StartTrace(x)
|
---|
92 | #define Trace(x)
|
---|
93 | #define Trace2(x,y)
|
---|
94 | #define TraceFunc(x)
|
---|
95 | #define TraceFunc2(x,y)
|
---|
96 |
|
---|
97 |
|
---|
98 | #endif // DO_TRACE
|
---|
99 |
|
---|
100 | #endif // __TRACER_H__
|
---|