My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
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 
17 #include "gcp/util/common/ErrHandler.h"
18 #include "gcp/util/common/FunctionName.h"
19 #include "gcp/util/common/Directives.h"
20 #include "gcp/util/common/IoLock.h"
21 #include "gcp/util/common/Logger.h"
22 
23 // Create an Exception class in namespace carma::antenna::gcp
24 
25 namespace gcp {
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 
58  const char* filename, const int lineNumber, bool report);
59 
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  gcp::util::IoLock::lockCerr();
87  std::cerr << what;
88  gcp::util::IoLock::unlockCerr();
89  }
90 
96  inline void report(std::string what)
97  {
98  gcp::util::IoLock::lockCerr();
99  std::cerr << what;
100  gcp::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 gcp
115 
116 #define Error(x) gcp::util::Exception((x), __FILE__, __LINE__, true)
117 #define ErrorNoReport(x) gcp::util::Exception((x), __FILE__, __LINE__, false)
118 #define ErrorDef(x,y) gcp::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  gcp::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, \
129  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
130  true, false, false);\
131 }
132 
133 #ifdef ThrowSimpleError
134 #undef ThrowSimpleError
135 #endif
136 
137 #define ThrowSimpleError(text) \
138 {\
139  std::ostringstream _macroOs; \
140  _macroOs << text;\
141  gcp::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, \
142  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
143  true, true, false);\
144 }
145 
146 #ifdef ThrowSysError
147 #undef ThrowSysError
148 #endif
149 
150 #define ThrowSysError(text) \
151 {\
152  std::ostringstream _macroOs; \
153  _macroOs << text;\
154  gcp::util::ErrHandler::throwError(_macroOs, __FILE__, __LINE__, \
155  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
156  true, false, true);\
157 }
158 
159 #ifdef ReportError
160 #undef ReportError
161 #endif
162 
163 #define ReportError(text) \
164 {\
165  std::ostringstream _macroOs; \
166  _macroOs << text;\
167  gcp::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, \
168  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
169  true, false, false);\
170 }
171 
172 #ifdef ReportSimpleError
173 #undef ReportSimpleError
174 #endif
175 
176 #define ReportSimpleError(text) \
177 {\
178  std::ostringstream _macroOs; \
179  _macroOs << text;\
180  gcp::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, \
181  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
182  true, true, false);\
183 }
184 
185 #ifdef ReportSysError
186 #undef ReportSysError
187 #endif
188 
189 #define ReportSysError(text) \
190 {\
191  std::ostringstream _macroOs; \
192  _macroOs << text;\
193  gcp::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, \
194  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
195  true, false, true);\
196 }
197 
198 #ifdef ReportSimpleSysError
199 #undef ReportSimpleSysError
200 #endif
201 
202 #define ReportSimpleSysError(text) \
203 {\
204  std::ostringstream _macroOs; \
205  _macroOs << text;\
206  gcp::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, \
207  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
208  true, true, true);\
209 }
210 
211 #ifdef ReportMessage
212 #undef ReportMessage
213 #endif
214 
215 #define ReportMessage(text) \
216 {\
217  std::ostringstream _macroOs; \
218  _macroOs << text;\
219  gcp::util::ErrHandler::report(_macroOs, __FILE__, __LINE__, \
220  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
221  false, true, false);\
222 }
223 
224 #ifdef LogMessage
225 #undef LogMessage
226 #endif
227 
228 #define LogMessage(error, text) \
229 {\
230  std::ostringstream _macroOs; \
231  _macroOs << text;\
232  gcp::util::ErrHandler::log(_macroOs, __FILE__, __LINE__, \
233  gcp::util::FunctionName::noArgs(__PRETTY_FUNCTION__).c_str(), \
234  false, true, false);\
235 }
236 
237 #define COUT(statement) \
238 {\
239  std::ostringstream _macroOs; \
240  _macroOs << statement << std::endl; \
241  gcp::util::Logger::printToStdout(_macroOs.str()); \
242 }
243 
244 #define CTOUT(statement) \
245 {\
246  gcp::util::TimeVal _macroTimeVal;\
247  _macroTimeVal.setToCurrentTime();\
248  std::ostringstream _macroOs; \
249  _macroOs << _macroTimeVal << ": " << statement << std::endl; \
250  gcp::util::Logger::printToStdout(_macroOs.str()); \
251 }
252 
253 #define CERR(statement) \
254 {\
255  std::ostringstream _macroOs; \
256  _macroOs << statement << std::endl; \
257  gcp::util::Logger::printToStderr(_macroOs.str()); \
258 }
259 
260 #define CTERR(statement) \
261 {\
262  gcp::util::TimeVal _macroTimeVal;\
263  _macroTimeVal.setToCurrentTime();\
264  std::ostringstream _macroOs; \
265  _macroOs << _macroTimeVal << ": " << statement << std::endl; \
266  gcp::util::Logger::printToStderr(_macroOs.str()); \
267 }
268 
269 #endif
void report(std::string &what)
Definition: Exception.h:84
Definition: LogStream.h:21
Exception(std::string str, const char *filename, const int lineNumber, bool report)
virtual ~Exception()
Definition: Exception.cc:90
void report()
Definition: Exception.h:77
void report(std::string what)
Definition: Exception.h:96
Definition: Exception.h:30