-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogDebug.cpp
328 lines (283 loc) · 8.35 KB
/
LogDebug.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "BaseLib/LogDebug.h"
#include "BaseLib/Debug.h"
#include "BaseLib/Exception.h"
#include <stdio.h>
using namespace std;
namespace BaseLib
{
/*std::ostream& operator<<(std::ostream &str, const LogDebugBase::LogType &type) throw()
{
#define PRINTOUT( name ) case name: str << #name; break;
switch(type)
{
PRINTOUT(LogDebugBase::NoLogType)
PRINTOUT(LogDebugBase::INFO)
PRINTOUT(LogDebugBase::DEBUG)
PRINTOUT(LogDebugBase::WARNING)
PRINTOUT(LogDebugBase::CRITICAL)
PRINTOUT(LogDebugBase::FATAL)
default:
IDEBUG() << " did not recoginze LogDebugBase::LogType" ;
break;
}
#undef PRINTOUT
return str;
}*/
// ----------------------------------------------------------------
// class LogDebugBase
// ----------------------------------------------------------------
char* LogDebugBase::makeMessage(const char *fmt, va_list ap)
{
// Guess we need no more than 100 bytes.
int n, size = 150;
char *p, *np;
if ((p = (char*)malloc(size)) == NULL)
return NULL;
//bool firstTime = true;
while(true)
{
// Try to print in the allocated space.
//if(firstTime == false)
// va_start(ap); //, fmt);
//firstTime = true;
n = vsnprintf(p, size, fmt, ap);
va_end(ap);
// If that worked, return the string.
if (n > -1 && n < size)
return p;
// Failsafe: try again with more space.
if (n > -1) // glibc 2.1
size = n+1; // precisely what is needed
else // glibc 2.0
size *= 2; // twice the old size
if ((np = (char*)realloc (p, size)) == NULL)
{
free(p);
return NULL;
}
else
{
p = np;
}
}
}
void LogDebugBase::LogMessage(LogDebugBase::LogType type, const char *msg, va_list ap)
{
char *buf = NULL;
try
{
buf = LogDebugBase::makeMessage(msg, ap);
ASSERT(buf);
//IDEBUG() << "Made message : " << buf ;
LogDebugBase::LogMessage(type, buf);
}
catch(GeneralException ex)
{
IDEBUG() << "WARNING! Exception caught : " << ex.what();
}
catch(...)
{
IDEBUG() << "WARNING! Unhandled exception caught!";
}
if(buf) free(buf);
}
void LogDebugBase::LogMessage(LogDebugBase::LogType type, const char *buf)
{
try
{
//TODO: fputs buf to file if logging is necessary
switch(type)
{
case LogDebugBase::INFO:
{
fprintf(stdout, "%s\n", buf);
fflush(stdout);
break;
}
case LogDebugBase::DEBUG:
{
fprintf(stdout, "%s\n", buf);
fflush(stdout);
break;
}
case LogDebugBase::WARNING:
{
fprintf(stdout, "%s\n", buf);
fflush(stdout);
break;
}
case LogDebugBase::CRITICAL:
{
fprintf(stderr, "%s\n", buf);
fflush(stderr);
DebugReport::GetOrCreate()->ReportError(buf);
//ASSERT(type != LogDebugBase::CRITICAL);
break;
}
case LogDebugBase::FATAL:
{
fprintf(stderr, "%s\n", buf);
fflush(stderr);
DebugReport::GetOrCreate()->ReportError(buf);
ASSERT(type != LogDebugBase::FATAL);
break;
}
case LogDebugBase::NoLogType:
default:
// do not print anything!
break;
}
std::ofstream ofile("intact_log.txt", std::ios::out | std::ios::app);
if(ofile.is_open())
{
ofile << buf << std::endl;
ofile.flush();
ofile.close();
}
}
catch(GeneralException ex)
{
IDEBUG() << "WARNING! Exception caught : " << ex.what();
}
catch(...)
{
IDEBUG() << "WARNING! Unhandled exception caught!";
}
}
// ----------------------------------------------------------------
// debug functions
// ----------------------------------------------------------------
void iInfo(const char *msg, ...)
{
#ifndef INTACT_NO_DEBUG_OUTPUT
va_list ap;
va_start(ap, msg); // use variable arg list
LogDebugBase::LogMessage(LogDebugBase::INFO, msg, ap);
va_end(ap);
#endif
}
void iDebug(const char *msg, ...)
{
#ifndef INTACT_NO_DEBUG_OUTPUT
va_list ap;
va_start(ap, msg); // use variable arg list
LogDebugBase::LogMessage(LogDebugBase::DEBUG, msg, ap);
va_end(ap);
#endif
}
void iWarning(const char *msg, ...)
{
#ifndef INTACT_NO_DEBUG_OUTPUT
va_list ap;
va_start(ap, msg); // use variable arg list
LogDebugBase::LogMessage(LogDebugBase::WARNING, msg, ap);
va_end(ap);
#endif
}
void iCritical(const char *msg, ...)
{
va_list ap;
va_start(ap, msg); // use variable arg list
LogDebugBase::LogMessage(LogDebugBase::CRITICAL, msg, ap);
va_end(ap);
}
void iFatal(const char *msg, ...)
{
va_list ap;
va_start(ap, msg); // use variable arg list
LogDebugBase::LogMessage(LogDebugBase::FATAL, msg, ap);
va_end(ap);
}
// ----------------------------------------------------------------
// class DebugReport
// ----------------------------------------------------------------
DebugReport* DebugReport::staticDebugReport = NULL;
DebugReport* DebugReport::GetOrCreate() //throw()
{
if(DebugReport::staticDebugReport == NULL)
DebugReport::staticDebugReport = new DebugReport(string("Intact"));
return DebugReport::staticDebugReport;
}
void DebugReport::ReportWarning(std::string report)
{
if(report.empty())
cout << "ReportWarning(report): WARNING! Reported warning is empty!" << std::endl;
numberOfWarnings++;
warningStream << numberOfWarnings << ": " << report << std::endl;
}
void DebugReport::ReportError(std::string report)
{
if(report.empty())
cout << "ReportError(report): WARNING! Reported error is empty!" << std::endl;
numberOfErrors++;
errorStream << numberOfErrors << ": " << report << std::endl;
}
void DebugReport::ReportException(std::string report)
{
if(report.empty())
cout << "ReportException(report): WARNING! Reported exception is empty!" << std::endl;
numberOfExceptions++;
exceptionStream << numberOfExceptions << ": " << report << std::endl;
}
std::ostream& operator<<(std::ostream &ostr, const DebugReport &testReport)
{
if(!testReport.GetReportTitle().empty())
ostr << "-------------- " << testReport.GetReportTitle() << " DebugReport --------------" << endl;
else
ostr << "-------------- DebugReport --------------" << endl;
if(!testReport.GetWarningString().empty())
{
ostr << "List of Warnings: " << endl;
ostr << testReport.GetWarningString() << endl;
}
if(!testReport.GetErrorString().empty())
{
ostr << "List of Errors: " << endl;
ostr << testReport.GetErrorString() << endl;
}
if(!testReport.GetExceptionString().empty())
{
ostr << "List of Exceptions: " << endl;
ostr << testReport.GetExceptionString() << endl;
}
ostr << "Number of Warnings: " << testReport.GetNumberOfWarnings() << endl;
ostr << "Number of Errors: " << testReport.GetNumberOfErrors() << endl;
ostr << "Number of Exceptions: " << testReport.GetNumberOfExceptions() << endl;
ostr << endl;
return ostr;
}
} // namespace BaseLib
/*char* LogDebugBase::makeMessage(const char *fmt, ...)
{
// Guess we need no more than 100 bytes.
int n, size = 150;
char *p, *np;
va_list ap;
if ((p = malloc(size)) == NULL)
return NULL;
while(true)
{
// Try to print in the allocated space.
va_start(ap, fmt);
n = vsnprintf(p, size, fmt, ap);
va_end(ap);
// If that worked, return the string.
if (n > -1 && n < size)
return p;
// Else try again with more space.
if (n > -1) // glibc 2.1
size = n+1; // precisely what is needed
else // glibc 2.0
size *= 2; // twice the old size
if ((np = realloc (p, size)) == NULL)
{
free(p);
return NULL;
}
else
{
p = np;
}
}
}
*/