CARMA C++
Exception.h
Go to the documentation of this file.
1 #ifndef EXCEPTION_H
2 #define EXCEPTION_H
3 
11 // System includes
12 
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16 
18 #include "carma/szautil/Directives.h"
19 #include "carma/szautil/IoLock.h"
20 #include "carma/szautil/Logger.h"
22 
23 // Create an Exception class in namespace carma::antenna::sza
24 
25 namespace sza {
26  namespace util {
27 
28  class LogStream;
29 
30  class Exception {
31 
32  public:
33 
41  Exception(std::string str, const char * filename,
42  const int lineNumber, bool report);
43 
51  Exception(std::ostringstream& os, const char * filename,
52  const int lineNumber, bool report);
53 
57  Exception(sza::util::LogStream& ls,
58  const char* filename, const int lineNumber, bool report);
59 
63  Exception(sza::util::LogStream* ls,
64  const char* filename, const int lineNumber, bool report);
65 
66 
70  virtual ~Exception();
71 
77  inline void report() {}
78 
84  inline void report(std::string& what)
85  {
86  sza::util::IoLock::lockCerr();
87  std::cerr << what;
88  sza::util::IoLock::unlockCerr();
89  }
90 
96  inline void report(std::string what)
97  {
98  sza::util::IoLock::lockCerr();
99  std::cerr << what;
100  sza::util::IoLock::unlockCerr();
101  }
102 
103  inline const char* what() {
104  return message_.c_str();
105  }
106 
107  private:
108 
109  std::string message_;
110 
111  }; // End class Exception
112 
113  } // namespace util
114 } // namespace sza
115 
116 #define Error(x) sza::util::Exception((x), __FILE__, __LINE__, true)
117 #define ErrorNoReport(x) sza::util::Exception((x), __FILE__, __LINE__, false)
118 #define ErrorDef(x,y) sza::util::Exception (x)((y), __FILE__, __LINE__, true)
119 
120 #ifdef ThrowError
121 #undef ThrowError
122 #endif
123 
124 #define ThrowError(text) \
125 {\
126  std::ostringstream _macroOs; \
127  _macroOs << text;\
128  sza::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, false, false);\
129 }
130 
131 #ifdef ThrowColorError
132 #undef ThrowColorError
133 #endif
134 
135 #define ThrowColorError(text, color) \
136 {\
137  XtermManip _macroXtm;\
138  std::ostringstream _macroOs; \
139  _macroOs << _macroXtm.bg("black") << _macroXtm.fg(color) << _macroXtm.textMode("bold");\
140  _macroOs << text;\
141  _macroOs << _macroXtm.bg("default") << _macroXtm.fg("default") << _macroXtm.textMode("normal");\
142  sza::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, false, false);\
143 }
144 
145 #ifdef ThrowSimpleError
146 #undef ThrowSimpleError
147 #endif
148 
149 #define ThrowSimpleError(text) \
150 {\
151  std::ostringstream _macroOs; \
152  _macroOs << text;\
153  sza::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, true, false);\
154 }
155 
156 #ifdef ThrowSysError
157 #undef ThrowSysError
158 #endif
159 
160 #define ThrowSysError(text) \
161 {\
162  std::ostringstream _macroOs; \
163  _macroOs << text;\
164  sza::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, false, true);\
165 }
166 
167 #ifdef ReportError
168 #undef ReportError
169 #endif
170 
171 #define ReportError(text) \
172 {\
173  std::ostringstream _macroOs; \
174  _macroOs << text;\
175  sza::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, false, false);\
176 }
177 
178 #ifdef ReportSimpleError
179 #undef ReportSimpleError
180 #endif
181 
182 #define ReportSimpleError(text) \
183 {\
184  std::ostringstream _macroOs; \
185  _macroOs << text;\
186  sza::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, true, false);\
187 }
188 
189 #ifdef ReportSysError
190 #undef ReportSysError
191 #endif
192 
193 #define ReportSysError(text) \
194 {\
195  std::ostringstream _macroOs; \
196  _macroOs << text;\
197  sza::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, false, true);\
198 }
199 
200 #ifdef ReportMessage
201 #undef ReportMessage
202 #endif
203 
204 #define ReportMessage(text) \
205 {\
206  std::ostringstream _macroOs; \
207  _macroOs << text;\
208  sza::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, true, false);\
209 }
210 
211 #ifdef LogMessage
212 #undef LogMessage
213 #endif
214 
215 #define LogMessage(error, text) \
216 {\
217  std::ostringstream _macroOs; \
218  _macroOs << text;\
219  sza::util::ErrHandler::log(_macroOs, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, true, false);\
220 }
221 
222 #define COUT(statement) \
223 {\
224  std::ostringstream _macroOs; \
225  _macroOs << statement << std::endl; \
226  sza::util::Logger::printToStdout(_macroOs.str()); \
227 }
228 
229 #define COUTCOLOR(statement, color) \
230 {\
231  XtermManip _macroXtm;\
232  std::ostringstream _macroOs; \
233  _macroOs << _macroXtm.bg("black") << _macroXtm.fg(color) << _macroXtm.textMode("bold");\
234  _macroOs << statement << std::endl; \
235  _macroOs << _macroXtm.bg("default") << _macroXtm.fg("default") << _macroXtm.textMode("normal");\
236  sza::util::Logger::printToStdout(_macroOs.str()); \
237 }
238 
239 #define COUTCOLORNNL(statement, color) \
240  {\
241  sza::util::XtermManip _macroXtm; \
242  std::ostringstream _macroOs; \
243  _macroOs << _macroXtm.bg("black") << _macroXtm.fg(color) << _macroXtm.textMode("bold");\
244  _macroOs << statement; \
245  _macroOs << _macroXtm.bg("default") << _macroXtm.fg("default") << _macroXtm.textMode("normal");\
246  sza::util::Logger::printToStdout(_macroOs.str()); \
247  }
248 
249 #define CTOUT(statement) \
250 {\
251  sza::util::TimeVal _macroTimeVal;\
252  _macroTimeVal.setToCurrentTime();\
253  std::ostringstream _macroOs; \
254  _macroOs << _macroTimeVal << ": " << statement << std::endl; \
255  sza::util::Logger::printToStdout(_macroOs.str()); \
256 }
257 
258 #define CERR(statement) \
259 {\
260  std::ostringstream _macroOs; \
261  _macroOs << statement << std::endl; \
262  sza::util::Logger::printToStderr(_macroOs.str()); \
263 }
264 
265 #define CTERR(statement) \
266 {\
267  sza::util::TimeVal _macroTimeVal;\
268  _macroTimeVal.setToCurrentTime();\
269  std::ostringstream _macroOs; \
270  _macroOs << _macroTimeVal << ": " << statement << std::endl; \
271  sza::util::Logger::printToStderr(_macroOs.str()); \
272 }
273 
274 #endif
Tagged: Mon Apr 10 13:32:05 PDT 2006.
Tagged: Sun Jan 20 13:34:20 NZDT 2008.