1 | /* ========================================================================
|
---|
2 | * (C) 2000 Vienna University of Technology
|
---|
3 | * ========================================================================
|
---|
4 | * PROJECT: Urban Viz
|
---|
5 | * ========================================================================*/
|
---|
6 | /** several error checking/log facilities.
|
---|
7 | *
|
---|
8 | * for char-strings, 'String' is typedef'd for convenience
|
---|
9 | * This library can be used with old style iostreams
|
---|
10 | * (define \c OLDSTREAM for that)
|
---|
11 | *
|
---|
12 | * $Header: /usr/local/cvsyare/cvsyamp/src/yareutils/merror.cpp,v 1.1 2002/11/14 15:22:58 wimmer Exp $
|
---|
13 | * @author Michael Wimmer
|
---|
14 | * @file */
|
---|
15 | /* ========================================================================*/
|
---|
16 |
|
---|
17 | #include "merror.h"
|
---|
18 |
|
---|
19 | // The following is to allow this to be compiled with new and old style
|
---|
20 | // iostreams. Special care has to be taken because in ms iostreams,
|
---|
21 | // strstream has a different name and behaves differently!
|
---|
22 | #ifndef OLDSTREAM
|
---|
23 | # include <fstream>
|
---|
24 | # include <strstream>
|
---|
25 | # define ERRSTD std
|
---|
26 | #else
|
---|
27 | # include <fstream.h>
|
---|
28 | # if defined(_MSC_VER) && !defined(__SGI_STL)
|
---|
29 | # define MS_STRSTREAM
|
---|
30 | # include <strstrea.h>
|
---|
31 | # else
|
---|
32 | # include <strstream.h>
|
---|
33 | # endif
|
---|
34 | # define ERRSTD
|
---|
35 | #endif
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | // ---------------------------------------------------------------------------
|
---|
41 | // PrivateErrHandler class definition
|
---|
42 | // ---------------------------------------------------------------------------
|
---|
43 |
|
---|
44 |
|
---|
45 | // a few functions encapsulated in a class so initialization is easier
|
---|
46 | class PrivateErrHandler
|
---|
47 | {
|
---|
48 | public:
|
---|
49 | /// constructor
|
---|
50 | PrivateErrHandler();
|
---|
51 |
|
---|
52 | /// will be forwarded to outside macros
|
---|
53 | inline ERRSTD::ostream &GetCurrentLogStream();
|
---|
54 |
|
---|
55 | /// output the current logstream
|
---|
56 | inline void PrintLogStream(bool overrideconsole);
|
---|
57 |
|
---|
58 | /// init the logstream (especially the logfile)
|
---|
59 | void InitLogStream(const char *filename, bool _logtoconsole);
|
---|
60 |
|
---|
61 | /// who handles the console stream
|
---|
62 | void SetConsoleStreamHandler(LogStreamToConsoleFunc handler);
|
---|
63 |
|
---|
64 | /// console only on/off
|
---|
65 | bool SetLogStream(bool _logtoconsole);
|
---|
66 |
|
---|
67 | /// whether to log to file or not
|
---|
68 | void SetLogToFile(bool yesno);
|
---|
69 |
|
---|
70 | private:
|
---|
71 | // the logfile
|
---|
72 | ERRSTD::ofstream log_ofstream;
|
---|
73 |
|
---|
74 | // the logstring (i.e., the primary logging target)
|
---|
75 | ERRSTD::strstream log_ostrstream;
|
---|
76 |
|
---|
77 | // log only to console?
|
---|
78 | bool logtoconsole;
|
---|
79 |
|
---|
80 | // log to file also?
|
---|
81 | bool logtofile;
|
---|
82 |
|
---|
83 | LogStreamToConsoleFunc CurrentLogStreamToConsoleFunc;
|
---|
84 |
|
---|
85 | /// just goes to cout
|
---|
86 | inline static void DefaultLogStreamToConsoleFunc(
|
---|
87 | const char *consolestring);
|
---|
88 |
|
---|
89 | // shoudn't be used
|
---|
90 | PrivateErrHandler(const PrivateErrHandler &) {}
|
---|
91 | PrivateErrHandler &operator=(const PrivateErrHandler &) { return *this; }
|
---|
92 | };
|
---|
93 |
|
---|
94 | // ---------------------------------------------------------------------------
|
---|
95 | // PrivateErrHandler class implementation
|
---|
96 | // ---------------------------------------------------------------------------
|
---|
97 |
|
---|
98 |
|
---|
99 | PrivateErrHandler::PrivateErrHandler()
|
---|
100 | :CurrentLogStreamToConsoleFunc(DefaultLogStreamToConsoleFunc),
|
---|
101 | logtoconsole(false), logtofile(true)
|
---|
102 | {
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | void PrivateErrHandler::SetLogToFile(bool yesno)
|
---|
107 | {
|
---|
108 | logtofile = yesno;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | inline ERRSTD::ostream &PrivateErrHandler::GetCurrentLogStream()
|
---|
113 | {
|
---|
114 | return log_ostrstream;
|
---|
115 | }
|
---|
116 |
|
---|
117 | inline void PrivateErrHandler::PrintLogStream(bool overrideconsole)
|
---|
118 | {
|
---|
119 | // Note: this is only used with the old strstream, not with ostringstream
|
---|
120 | log_ostrstream << ERRSTD::ends;
|
---|
121 |
|
---|
122 | if (log_ofstream && (logtofile || !logtoconsole))
|
---|
123 | log_ofstream << log_ostrstream.str() << ERRSTD::flush;
|
---|
124 |
|
---|
125 | if (logtoconsole || overrideconsole || !log_ofstream)
|
---|
126 | CurrentLogStreamToConsoleFunc(log_ostrstream.str());
|
---|
127 |
|
---|
128 | // Note: this was used with ostringstream, but not compatible
|
---|
129 | // with old iostreams
|
---|
130 | //log_ostringstream.str("");
|
---|
131 |
|
---|
132 | log_ostrstream.rdbuf()->freeze(false);
|
---|
133 | log_ostrstream.seekp(0);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | void PrivateErrHandler::InitLogStream(const char *filename,
|
---|
138 | bool _logtoconsole)
|
---|
139 | {
|
---|
140 | // Note: only works on new iostreams!
|
---|
141 | #ifdef MS_STRSTREAM
|
---|
142 | log_ofstream.rdbuf()->setbuf(NULL, 0);
|
---|
143 | #else
|
---|
144 | log_ofstream.rdbuf()->pubsetbuf(NULL, 0);
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | // SGI bug: ios::in assumed!
|
---|
148 | log_ofstream.open(filename,ERRSTD::ios::out|ERRSTD::ios::trunc);
|
---|
149 |
|
---|
150 | if (!log_ofstream)
|
---|
151 | {
|
---|
152 | EOUT("Could not open logfile!");
|
---|
153 | }
|
---|
154 |
|
---|
155 | SetLogStream(_logtoconsole);
|
---|
156 |
|
---|
157 | OUT0("Logfile opened\n");
|
---|
158 | }
|
---|
159 |
|
---|
160 | void PrivateErrHandler::SetConsoleStreamHandler(
|
---|
161 | LogStreamToConsoleFunc handler)
|
---|
162 | {
|
---|
163 | if (handler == NULL)
|
---|
164 | CurrentLogStreamToConsoleFunc = DefaultLogStreamToConsoleFunc;
|
---|
165 | else
|
---|
166 | CurrentLogStreamToConsoleFunc = handler;
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool PrivateErrHandler::SetLogStream(bool _logtoconsole)
|
---|
170 | {
|
---|
171 | bool x = logtoconsole;
|
---|
172 | logtoconsole = _logtoconsole;
|
---|
173 | return x;
|
---|
174 | }
|
---|
175 |
|
---|
176 | void PrivateErrHandler::DefaultLogStreamToConsoleFunc(
|
---|
177 | const char *consolestring)
|
---|
178 | {
|
---|
179 | ERRSTD::cout << consolestring;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | // ---------------------------------------------------------------------------
|
---|
184 | // Private C-access functions
|
---|
185 | // ---------------------------------------------------------------------------
|
---|
186 |
|
---|
187 | static PrivateErrHandler *theErrHandler = NULL;
|
---|
188 |
|
---|
189 | inline static void _ErrInitDefault()
|
---|
190 | {
|
---|
191 | static bool initialized = false;
|
---|
192 | if (!initialized)
|
---|
193 | {
|
---|
194 | theErrHandler = new PrivateErrHandler;
|
---|
195 | initialized = true;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | // ---------------------------------------------------------------------------
|
---|
200 | // Public interface - exposed to macros
|
---|
201 | // ---------------------------------------------------------------------------
|
---|
202 |
|
---|
203 | // logstream used by macros - this is necessary so that logstreams don't
|
---|
204 | // give an error if used in global constructors (which are constructed
|
---|
205 | // prior to the logfile)
|
---|
206 | ERRSTD::ostream &_GetLogStream()
|
---|
207 | {
|
---|
208 | _ErrInitDefault();
|
---|
209 |
|
---|
210 | return theErrHandler->GetCurrentLogStream();
|
---|
211 | }
|
---|
212 |
|
---|
213 | ERRSTD::ostream &_LogStreamDone(ERRSTD::ostream &os, bool overrideconsole)
|
---|
214 | {
|
---|
215 | _ErrInitDefault();
|
---|
216 | theErrHandler->PrintLogStream(overrideconsole);
|
---|
217 |
|
---|
218 | return os;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | // ---------------------------------------------------------------------------
|
---|
223 | // Public interface - exposed directly to application
|
---|
224 | // ---------------------------------------------------------------------------
|
---|
225 |
|
---|
226 | void SetConsoleStreamHandler(LogStreamToConsoleFunc handler)
|
---|
227 | {
|
---|
228 | _ErrInitDefault();
|
---|
229 |
|
---|
230 | theErrHandler->SetConsoleStreamHandler(handler);
|
---|
231 | }
|
---|
232 |
|
---|
233 | void InitLogStream(const char *filename, bool logtofile)
|
---|
234 | {
|
---|
235 | _ErrInitDefault();
|
---|
236 |
|
---|
237 | theErrHandler->InitLogStream(filename, !logtofile);
|
---|
238 | }
|
---|
239 |
|
---|
240 | bool SetLogStream(bool logtofile)
|
---|
241 | {
|
---|
242 | _ErrInitDefault();
|
---|
243 |
|
---|
244 | return !theErrHandler->SetLogStream(!logtofile);
|
---|
245 | }
|
---|
246 |
|
---|
247 | void SetLogToFile(bool yesno)
|
---|
248 | {
|
---|
249 | _ErrInitDefault();
|
---|
250 |
|
---|
251 | theErrHandler->SetLogToFile(yesno);
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | // ---------------------------------------------------------------------------
|
---|
256 | // Win32-stuff
|
---|
257 | // ---------------------------------------------------------------------------
|
---|
258 |
|
---|
259 | #ifdef WIN32
|
---|
260 |
|
---|
261 | #define WIN32_LEAN_AND_MEAN
|
---|
262 | #define NOMINMAX
|
---|
263 | #include <windows.h> // for MessageBox and stuff
|
---|
264 |
|
---|
265 | void Msg(const char *msg, const char *title)
|
---|
266 | {
|
---|
267 | // MessageBox(NULL, msg, title, MB_OK);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /// gets WIN32 API error message (used by CHKMS-macros)
|
---|
271 | char *GetErrorStringMS(DWORD errorValue)
|
---|
272 | {
|
---|
273 | LPVOID lpMsgBuf;
|
---|
274 |
|
---|
275 | FormatMessage(
|
---|
276 | FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
277 | FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
---|
278 | NULL,
|
---|
279 | errorValue,
|
---|
280 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
---|
281 | (LPTSTR) &lpMsgBuf,
|
---|
282 | 0,
|
---|
283 | NULL
|
---|
284 | );
|
---|
285 |
|
---|
286 | //String message;
|
---|
287 | char *message;
|
---|
288 | if (lpMsgBuf)
|
---|
289 | {
|
---|
290 | message = (char *)lpMsgBuf;
|
---|
291 |
|
---|
292 | // Free the buffer.
|
---|
293 | LocalFree( lpMsgBuf );
|
---|
294 | }
|
---|
295 | else
|
---|
296 | {
|
---|
297 | message = "No error message returned";
|
---|
298 | }
|
---|
299 |
|
---|
300 | return message;
|
---|
301 | }
|
---|
302 |
|
---|
303 | #else
|
---|
304 | // nop functions for non-Win32 systems
|
---|
305 | void Msg(const char *msg, const char *title) {}
|
---|
306 | char *GetErrorStringMS(unsigned long errorValue) { return ""; }
|
---|
307 | #endif
|
---|