-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrace.h
181 lines (152 loc) · 4.21 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef _TRACE_H_
#define _TRACE_H_
#include <stdio.h>
#include <streambuf>
#include <ostream>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
/***************************************************************
* This file defines the following three macros:
* #define TRACE(x) : Appends the argument to the trace file.
* #define TRACE_FUNCTION(x) : Automatically traces entrance to and exit from
* the calling function.
* #define DEBUG(x) : Execute the argument, if enabled.
***************************************************************/
/***************************************************************
* CAppendFile
*
* Helper class. It essentially encapsulates a file descriptor.
***************************************************************/
class CAppendFile
{
public:
CAppendFile(const char *fileName)
: fd()
{
fd = open(fileName, O_CREAT | O_WRONLY | O_APPEND, 0644);
}
~CAppendFile()
{
if (fd != -1)
close(fd);
}
int Write(char *buffer, int num)
{
return write(fd, buffer, num);
}
private:
int fd;
};
/***************************************************************
* CTraceBuf
*
* Helper class. It essentially encapsulates a stream buffer
***************************************************************/
class CTraceBuf : public std::streambuf
{
private:
CTraceBuf(const CTraceBuf&);
const CTraceBuf& operator =(const CTraceBuf&);
protected:
static const int bufferSize = 200;
char buffer[bufferSize];
public:
CTraceBuf(const char *fileName) : fileName(fileName)
{
setp(buffer, buffer + (bufferSize-1));
}
virtual ~CTraceBuf()
{
sync();
}
protected:
int flushBuffer()
{
int num = pptr() - pbase();
{
CAppendFile fd(fileName);
if (fd.Write(buffer, num) != num)
{
return EOF;
}
}
pbump (-num);
return num;
}
virtual int overflow(int c)
{
if (c != EOF)
{
*pptr() = c;
pbump(1);
}
if (flushBuffer() == EOF)
{
return EOF;
}
return c;
}
virtual int sync()
{
if (flushBuffer() == EOF)
{
return -1;
}
return 0;
}
private:
const char *fileName;
}; /* end of class CTraceBuf */
/***************************************************************
* CTrace
*
* Helper class. It essentially encapsulates an output stream.
***************************************************************/
class CTrace : public std::ostream
{
protected:
CTraceBuf buf;
public:
CTrace(const char *fileName) :
std::ostream(&buf) ,
buf(fileName)
{
}
}; /* end of class CTrace */
#ifdef ENABLE_TRACE
extern std::ostream *gpTrace;
/***************************************************************
* CTraceFunction
*
* Helper class. Automatically traces entrance to and exit from the calling
* function.
***************************************************************/
class CTraceFunction
{
private:
CTraceFunction(const CTraceFunction&);
const CTraceFunction& operator =(const CTraceFunction&);
public:
CTraceFunction(const char *functionName) :
functionName(functionName)
{
if (gpTrace) {*gpTrace << "+" << functionName << std::endl;}
}
~CTraceFunction()
{
if (gpTrace) {*gpTrace << "-" << functionName << std::endl;}
}
private:
const char *functionName;
}; /* end of class CTraceFunction */
// These are the three macros supported
#define TRACE(x) if (gpTrace) {*gpTrace << x;}
#define TRACE_FUNCTION(x) CTraceFunction temp(x);
#define DEBUG(x) x
#else // ENABLE_TRACE
#define TRACE(x)
#define TRACE_FUNCTION(x)
#define DEBUG(x)
#endif // ENABLE_TRACE
#endif // _TRACE_H_