-
Notifications
You must be signed in to change notification settings - Fork 13
/
Logger.h
266 lines (243 loc) · 6.39 KB
/
Logger.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
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
// **************************************************************************
// Logger.h
//
// Description:
//
// DISCLAIMER:
// This software was developed by U.S. Government employees as part of
// their official duties and is not subject to copyright. No warranty implied
// or intended.
// **************************************************************************
#pragma once
#include "atlstr.h"
#include <fstream>
#include <iostream>
#include <atlcomtime.h>
#include "NIST/StdStringFcn.h"
#define LOGLISTENERS
#ifdef LOGLISTENERS
// This is for notifying listeners to logged messages - mnany to many relationship
#include <boost/function.hpp>
#include <boost/function_equal.hpp>
typedef boost::function<void (const TCHAR * msg)> FcnLognotify;
#include <vector>
#endif
// This is useful for preventing endless repeated error messages
#define LOGONCE static long nLog##__LINE__=0; if( 0 == nLog##__LINE__++)
//#define LOGONCE
/** Allow redirected of std::cout to vc console output
std::cout.rdbuf(&Logger);
std::cout << "Hello World\n";
std::cout.flush();
*/
class basic_debugbuf : public std::streambuf
{
public:
std::vector<FcnLognotify> listeners;
basic_debugbuf(int bufferSize=1000)
{
if (bufferSize)
{
char *ptr = new char[bufferSize];
setp(ptr, ptr + bufferSize);
}
else
setp(0, 0);
}
virtual ~basic_debugbuf()
{
sync();
delete[] pbase();
}
virtual void writeString(const std::string &str)
{
for(int i=0; i< listeners.size(); i++)
listeners[i](str.c_str());
OutputDebugString(str.c_str());
}
private:
int overflow(int c)
{
sync();
if (c != EOF)
{
if (pbase() == epptr())
{
std::string temp;
temp += char(c);
writeString(temp);
}
else
sputc(c);
}
return 0;
}
int sync()
{
if (pbase() != pptr())
{
int len = int(pptr() - pbase());
std::string temp(pbase(), len);
writeString(temp);
setp(pbase(), epptr());
}
return 0;
}
};
struct CDebugLevel
{
CDebugLevel(int n, char * name) : _debug(n), _name(name) {}
int operator ()() { return _debug; }
operator int () { return _debug; }
operator std::string () { return _name; }
int _debug;
std::string _name;
};
__declspec(selectany) CDebugLevel ENTRY (0, "ENTRY");
__declspec(selectany) CDebugLevel FATAL (0, "FATAL");
__declspec(selectany) CDebugLevel LOWERROR (1, "ERROR");
__declspec(selectany) CDebugLevel WARNING (2, "WARN ");
__declspec(selectany) CDebugLevel INFO (3, "INFO ");
__declspec(selectany) CDebugLevel DBUG (4, "DEBUG");
__declspec(selectany) CDebugLevel DETAILED (5, "DETAIL ");
__declspec(selectany) CDebugLevel HEAVYDEBUG (5, "HDEBUG ");
/**
Sample use:
CLogger logger;
logger.DebugString()=true;
logger << FATAL << "Fatal Message1\n";
logger << FATAL << "Fatal Message2\n";
logger.DebugString()=false;
logger.Open(::ExeDirectory() + "debug.txt");
logger << FATAL << "Fatal Message\n";
logger << DETAILED << "DETAILED Message\n";
logger << INFO << "INFO Message\n";
logger().close();
*/
class CLogger : public basic_debugbuf
{
struct nullstream: std::fstream {
nullstream(): std::ios(0), std::fstream((_Filet *) NULL) {}
};
public:
CLogger()
{
DebugLevel()=7;
filename=::ExeDirectory() + "debug.txt";
//std::cout.rdbuf(&_outputdebugstream);
//std::cerr.rdbuf(&_outputdebugstream);
OutputConsole()=false;
Timestamping()=false;
DebugString()=false;
_nCounter=1;
}
~CLogger()
{
if(DebugFile.is_open())
DebugFile.close();
}
int & DebugLevel() { return _debuglevel; }
bool & Timestamping() { return bTimestamp; }
bool & DebugString() { return bDebugString; }
bool & OutputConsole() { return bOutputConsole; }
std::ofstream & operator()(void) { return this->DebugFile; }
void Open(std::string filename, int bAppend=false)
{
this->filename=filename;
Open(bAppend);
}
void Open(int bAppend=0)
{
int opMode = std::fstream::out;
if(bAppend)
opMode |= std::fstream::app;
DebugFile.open(filename.c_str(), opMode, OF_SHARE_DENY_NONE);
}
std::ostream &operator<<( CDebugLevel level)
{
std::fstream tmp;
if( DebugLevel() < (int) level)
return this->devnull;
if(DebugString())
{
return PrintTimestamp(std::cout,level );
}
//return PrintTimestamp(std::ostream(static_cast<std::streambuf*>( &_outputdebugstream)) ,level);
#if ( _MSC_VER >= 1600) // 2010
return PrintTimestamp(std::ostream(static_cast<std::streambuf*>( &_outputdebugstream)) ,level);
#else
return PrintTimestamp(this->DebugFile,level);
#endif
}
std::ostream & PrintTimestamp(std::ostream & s, CDebugLevel level)
{
if(Timestamping())
{
s << Timestamp() << ": ";
s << (std::string) level << " [" << (int ) _nCounter++ << "] ";
}
return s;
}
int LogMessage(std::string msg, int level=-1)
{
if( DebugLevel() < level)
return level;
if(OutputConsole())
OutputDebugString(msg.c_str());
if(!DebugFile.is_open())
return level;
if(Timestamping())
DebugFile << Timestamp();
DebugFile << msg;
// LOGLISTENERS is for notifying windows or other listeners
#ifdef LOGLISTENERS
for(int i=0; i< listeners.size(); i++)
listeners[i](msg.c_str());
#endif
DebugFile.flush();
return level;
}
int Fatal(std::string msg){ return LogMessage(msg,FATAL); }
int Error(std::string msg){ return LogMessage(msg,LOWERROR); }
int Warning(std::string msg){return LogMessage(msg,WARNING); }
int Info(std::string msg){ return LogMessage(msg,INFO); }
int Status(std::string msg){return LogMessage(msg,FATAL); }
/////////////////////////////////////////////////////////////////////////////
//2012-12-22T03:06:56.0984Z
static std::string Timestamp()
{
SYSTEMTIME st;
GetSystemTime(&st);
return StdStringFormat("%4d-%02d-%02dT%02d:%02d:%02d.%04dZ", st.wYear, st.wMonth, st.wDay, st.wHour,
st.wMinute, st.wSecond, st.wMilliseconds);
//return (LPCSTR) COleDateTime::GetCurrentTime().Format();
}
public:
basic_debugbuf _outputdebugstream;
std::ofstream DebugFile;
protected:
int _debuglevel;
bool bTimestamp;
bool bDebugString;
bool bOutputConsole;
int nDebugReset;
std::string filename;
nullstream devnull;
int _nCounter;
#ifdef LOGLISTENERS
/**
void CMainFrame::MutexStep(std::string s)
{
OutputDebugString(s.c_str());
}
...
m_view.AddListener("Step", boost::bind(&CMainFrame::MutexStep, this,_1))
*/
public:
void AddListener(FcnLognotify notify)
{
_outputdebugstream.listeners.push_back(notify);
}
#endif
};
__declspec(selectany) CLogger GLogger;