Manticore  Version 1.0
Physics of Molecular Clouds
Exception.h
Go to the documentation of this file.
1 
8 #ifndef MUTILS_EXCEPTION_H
9 #define MUTILS_EXCEPTION_H
10 
11 #include <exception>
12 
13 #include "mutils/Log.h"
14 
24 #define MU_THROW(ex, src, msg, ...) do { \
25  char _mu_buf[4096]; \
26  bool db = (mutils::Log::level >= mutils::Log::DEBUG); \
27  mutils::cxxsnprintf(_mu_buf, sizeof(_mu_buf), "%s:%0*d: " msg "\n", \
28  (db ? (src) : ""), (db ? 1 : 4), __LINE__, ## __VA_ARGS__); \
29  if (mutils::Exception::errors) { \
30  mutils::Log::error(_mu_buf); \
31  } else { \
32  (ex).message(_mu_buf); \
33  throw (ex); \
34  } \
35  } while (0)
36 
44 #define MU_RETHROW(ex, src, msg, ...) do { \
45  (ex).print(); MU_THROW(ex, src, msg, ## __VA_ARGS__); \
46  } while (0)
47 
56 #define MU_THROW_IF(cond, ex, src, msg, ...) do { \
57  if (cond) { MU_THROW(ex, src, msg, ## __VA_ARGS__); } \
58  } while (0)
59 
60 
69 #define MU_THROW_ERRNO(ex, src, msg, ...) \
70  MU_THROW(ex, src, msg ": %s", ## __VA_ARGS__, strerror(errno))
71 
81 #define MU_THROW_ERRNO_IF(cond, ex, src, msg, ...) \
82  MU_THROW_IF(cond, ex, src, msg ": %s", ## __VA_ARGS__, strerror(errno))
83 
84 
90 #define MU_EXCEPTION(src, msg, ...) do { \
91  mutils::Exception _mu_ex; \
92  MU_THROW(_mu_ex, src, msg, ## __VA_ARGS__); \
93  } while (0)
94 
101 #define MU_EXCEPTION_IF(cond, src, msg, ...) do { \
102  mutils::Exception _mu_ex; \
103  MU_THROW_IF(cond, _mu_ex, src, msg, ## __VA_ARGS__); \
104  } while (0)
105 
112 #define MU_EXCEPTION_ERRNO(src, msg, ...) do { \
113  mutils::Exception _mu_ex; \
114  MU_THROW_ERRNO(_mu_ex, src, msg, ## __VA_ARGS__); \
115  } while (0)
116 
124 #define MU_EXCEPTION_ERRNO_IF(cond, src, msg, ...) do { \
125  mutils::Exception _mu_ex; \
126  MU_THROW_ERRNO_IF(cond, _mu_ex, src, msg, ## __VA_ARGS__); \
127  } while (0)
128 
129 
130 namespace mutils {
131 
146 class Exception: public std::exception
147 {
148 public:
152  : level(level) { message("Default exception"); }
153 
155  virtual ~Exception() noexcept { }
156 
159  void message(const char *msg) {
160  msg_ = Log::name(level);
161  msg_ += ": ";
162  msg_ += msg;
163  }
164 
166  void print() const {
167  if (level <= Log::level) { fputs(what(), Log::stream); }
168  fflush(Log::stream);
169  }
170 
172  virtual const char *what() const noexcept { return msg_.c_str(); }
173 
175  int level;
176 
182  static bool errors;
183 
184 protected:
186  std::string msg_;
187 };
188 
194 inline void printException(std::exception &ex)
195 {
196  auto mex = dynamic_cast<mutils::Exception *>(&ex);
197  if (mex) { mex->print(); }
198  else { fprintf(stderr, "%s\n", ex.what()); }
199 }
200 
208 inline void exitException(std::exception &ex, int status = EXIT_FAILURE)
209 {
210  printException(ex);
211  fprintf(stderr, "Exception caught (Log::file = %s); exiting...\n",
212  Log::file.c_str());
213  exit(status);
214 }
215 
216 } // namespace mutils
217 
218 #endif // MUTILS_EXCEPTION_H
static bool errors
Whether to print error messages (else throw exceptions).
Definition: Exception.h:182
static std::string file
Message log file name.
Definition: Log.h:436
static FILE * stream
Message log file stream.
Definition: Log.h:433
MathUtils exception (base) class.
Definition: Exception.h:146
Exception(int level=Log::ERROR)
Default constructor.
Definition: Exception.h:151
static const char * name(int lev=mutils::Log::level)
Standard logging level name string.
Definition: Log.h:355
std::string msg_
Error message.
Definition: Exception.h:186
Declares diagnostic message macros.
virtual ~Exception() noexcept
Destructor.
Definition: Exception.h:155
virtual const char * what() const noexcept
Returns error string.
Definition: Exception.h:172
void print() const
Prints error message to log.
Definition: Exception.h:166
void exitException(std::exception &ex, int status=EXIT_FAILURE)
Exits program with exception message. Exits the program (with status) after displaying the exception ...
Definition: Exception.h:208
static int level
Message logging level for output to stream.
Definition: Log.h:430
MathUtils package.
Definition: CommandLine.cc:10
static constexpr int ERROR
Error condition.
Definition: Log.h:347
void message(const char *msg)
Assigns error message.
Definition: Exception.h:159
int level
Condition log level.
Definition: Exception.h:175
void printException(std::exception &ex)
Prints exception message.
Definition: Exception.h:194