RTC Toolkit  0.1.0-alpha
exceptions.hpp
Go to the documentation of this file.
1 
9 #ifndef RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
11 
18 #include <exception>
19 #include <string>
20 #include <vector>
21 #include <ostream>
22 #include <chrono>
23 #include <thread>
24 #include <type_traits>
25 
26 #include <boost/stacktrace.hpp>
27 #include <boost/filesystem.hpp>
28 #include <boost/core/demangle.hpp>
29 
30 namespace elt {
31 namespace error {
32 
36 class CiiException : public std::exception {
37  public:
49  typedef std::chrono::system_clock clock_t;
50 
56  explicit CiiException(const std::string& message);
57 
65  template<typename ... Args>
66  explicit CiiException(const std::string& formatter, Args ... args)
67  : CiiException(BuildFormattedString(formatter, args...)) {
68  }
69 
73  virtual ~CiiException() = default;
74 
75  virtual std::string getCiiType() const {
76  return m_attribs->ciiType;
77  }
78 
79  virtual std::string getCiiMethodName() const {
80  return m_attribs->ciiMethodName;
81  }
82 
83  virtual std::string getCiiClassName() const {
84  return m_attribs->ciiClassName;
85  }
86 
87  virtual std::string getCiiFileName() const {
88  return m_attribs->ciiFileName;
89  }
90 
91  virtual std::string getCiiHostName() const {
92  return m_attribs->ciiHostName;
93  }
94 
95  virtual std::string getCiiStackTrace() const;
96 
97  virtual std::vector<std::string> getCiiExceptionStack() const {
98  return m_attribs->ciiExceptionStack;
99  }
100 
101  virtual std::string getCiiMessage() const {
102  return m_attribs->ciiMessage;
103  }
104 
105  virtual std::string getCiiDetails() const {
106  return m_attribs->ciiDetails;
107  }
108 
109  virtual int64_t getCiiCreationDate() const {
110  return m_attribs->ciiCreationDate;
111  }
112 
113  const std::string GetCiiStackTrace() const noexcept {
114  return getCiiStackTrace();
115  }
116 
122  const boost::stacktrace::stacktrace& GetStackTrace() const;
123 
129  const boost::stacktrace::stacktrace& GetOriginalStackTrace() const;
130 
136  std::string GetStackTraceAsString() const;
137 
143  std::string GetOriginalStackTraceAsString() const;
144 
150  std::string GetFullStackTraceAsString() const;
151 
157  std::chrono::time_point<clock_t> GetTimePoint() const;
158 
164  int64_t GetCreationDate() const;
165 
171  std::thread::id GetThreadId() const;
172 
178  int GetProcessId() const;
179 
185  const std::string& GetHostName() const;
186 
187 
193  std::string GetTypeName() const;
194 
200  void SetLineNumber(uint32_t lineNumber);
201 
207  uint32_t GetLineNumber() const;
208 
214  void SetFunctionName(const std::string& functionName);
215 
221  const std::string& GetFunctionName() const;
222 
228  void SetFileName(const std::string& fileName);
229 
235  const std::string& GetFileName() const;
236 
242  void SetDetails(const std::string& details);
243 
249  std::string GetDetails() const;
250 
256  void SetClassName(const std::string& className);
257 
263  std::string Dump() const;
264 
265  std::string GetSeverity() const;
266 
273  std::string DumpWithNested() const;
274 
275  const char* what() const noexcept override;
276 
283  const char* What() const noexcept {
284  return what();
285  }
286 
287  void SetCiiExceptionStack(const std::vector<std::string>& nested_exceptions);
288 
289  private:
293  template<typename ... Args>
294  static std::string BuildFormattedString(const std::string& formatter,
295  Args ... args) {
296  int finalStringSize = std::snprintf(nullptr, 0, formatter.c_str(), args...);
297  std::vector<char> buffer((size_t) finalStringSize + 1);
298  std::snprintf(buffer.data(), buffer.size(), formatter.c_str(), args...);
299  return std::string(buffer.data(), (size_t) finalStringSize);
300  }
301 
308  const std::string AdditionalDetails() const;
309 
310  struct Attributes {
311  Attributes();
312 
313  std::string ciiType;
314  std::string ciiMethodName;
315  std::string ciiClassName;
316  std::string ciiFileName;
317  std::string ciiHostName;
318  std::string ciiStackTrace;
319  std::vector<std::string> ciiExceptionStack;
320  std::string ciiMessage;
321  std::string ciiDetails;
322  int64_t ciiCreationDate;
323 
324  const boost::stacktrace::stacktrace m_stack_trace;
325  const std::chrono::time_point<clock_t> m_date_time;
326  const std::thread::id m_thread_id;
327  const int m_process_id;
328  const std::string m_host_name;
329 
330  uint32_t m_line_number;
331  std::string m_function_name;
332  std::string m_file_name;
333  std::string m_details;
334  std::string m_severity;
335  bool m_detail_set_by_user;
336  };
337 
338  std::shared_ptr<Attributes> m_attribs;
339 };
340 
341 
346  public:
348 
349  std::string GetSeverity() const{
350  return "Exception";
351  }
352 };
353 
354 
358 class CiiError: public CiiException {
359  public:
361 
362  std::string GetSeverity() const{
363  return "Error";
364  }
365 };
366 
367 
368 } // namespace error
369 } // namespace elt
370 
371 std::ostream& operator<<(std::ostream& os, const elt::error::CiiException& e);
372 
373 namespace {
374 
375 // Constructs an exception object that takes a string message as the first argument.
376 // The file name, line number and function name will be appended to the message.
377 template <typename T, typename... Args>
378 T CreateExceptionWithMessage(const char* file, int line, const char* function,
379  const std::string& msg, Args&&... args) {
380  std::string fullmsg = msg;
381  fullmsg += "\nSource file: " + std::string(file);
382  fullmsg += "\nLine no.: " + std::to_string(line);
383  fullmsg += "\nFunction: " + std::string(function);
384  return T(fullmsg, std::forward<Args>(args)...);
385 }
386 
387 // Constructs an exception object. Depending on the type of the exception, additional context
388 // information is added to the object if supported.
389 // This function is used to implement the CII_THROW and CII_THROW_WITH_NESTED macros.
390 template <typename T, typename N, typename... Args>
391 T CreateExceptionObject(const N& nested_exception, const char* file, int line, const char* function,
392  Args&&... args) {
393  if constexpr (std::is_base_of_v<elt::error::CiiException, T>) {
394  T throwing_exception = T(std::forward<Args>(args)...);
395  throwing_exception.SetFileName(boost::filesystem::path(file).filename().string());
396  throwing_exception.SetFunctionName(function);
397  throwing_exception.SetLineNumber(line);
398  throwing_exception.SetClassName(boost::core::demangle(typeid(T).name()));
399  std::vector<std::string> this_stack;
400  this_stack.push_back(throwing_exception.GetTypeName());
401  if constexpr (std::is_base_of_v<elt::error::CiiException, N>) {
402  std::vector<std::string> nested_stack = nested_exception.getCiiExceptionStack();
403  this_stack.insert(this_stack.end(), nested_stack.begin(), nested_stack.end());
404  }
405  throwing_exception.SetCiiExceptionStack(this_stack);
406  return throwing_exception;
407  } else if constexpr (std::is_base_of_v<std::runtime_error, T> or
408  std::is_base_of_v<std::logic_error, T>) {
409  return CreateExceptionWithMessage<T>(file, line, function, std::forward<Args>(args)...);
410  } else {
411  return T(std::forward<Args>(args)...);
412  }
413 }
414 
415 } // namespace anonymous
416 
422 #define CII_THROW(exceptionType_t, ...) { \
423  throw CreateExceptionObject<exceptionType_t>(0, __FILE__, __LINE__, __FUNCTION__, \
424  __VA_ARGS__); \
425  }
426 
433 #define CII_THROW_WITH_NESTED(exceptionType_t, nested_exception, ...) { \
434  exceptionType_t throwing_exception = CreateExceptionObject<exceptionType_t>( \
435  nested_exception, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
436  std::throw_with_nested(throwing_exception); \
437  }
438 
439 #endif // RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
elt::error::CiiException::GetFileName
const std::string & GetFileName() const
Return the name of the file in which the exception was thrown.
Definition: exceptions.cpp:158
elt::error::CiiException::GetTimePoint
std::chrono::time_point< clock_t > GetTimePoint() const
Return the exception creation time as a chrono timepoint.
Definition: exceptions.cpp:101
elt::error::CiiException::DumpWithNested
std::string DumpWithNested() const
Return all the exception's details, including details of nested exceptions.
Definition: exceptions.cpp:200
elt::error::CiiError::GetSeverity
std::string GetSeverity() const
Definition: exceptions.hpp:362
operator<<
std::ostream & operator<<(std::ostream &os, const elt::error::CiiException &e)
Definition: exceptions.cpp:233
elt::error::CiiException::getCiiMethodName
virtual std::string getCiiMethodName() const
Definition: exceptions.hpp:79
elt::error::CiiException::What
const char * What() const noexcept
Return all the exception's details, including details of nested exceptions.
Definition: exceptions.hpp:283
elt::error::CiiBaseException
Base class for the exceptions.
Definition: exceptions.hpp:345
rtctk::componentFramework::Args
detail::Args Args
Definition: rtcComponent.hpp:31
elt::error::CiiException::getCiiFileName
virtual std::string getCiiFileName() const
Definition: exceptions.hpp:87
elt::error::CiiException::GetFullStackTraceAsString
std::string GetFullStackTraceAsString() const
Return the full stack trace (including nested exceptions)
Definition: exceptions.cpp:80
elt::error::CiiException::GetDetails
std::string GetDetails() const
Get the exception details.
Definition: exceptions.cpp:168
elt::error::CiiBaseException::GetSeverity
std::string GetSeverity() const
Definition: exceptions.hpp:349
rtctkExampleDataTaskRobotTest.path
string path
Definition: rtctkExampleDataTaskRobotTest.py:228
elt::error::CiiException::GetCreationDate
int64_t GetCreationDate() const
Return the exception creation time as number of seconds since Unix Epoch.
Definition: exceptions.cpp:105
elt::error::CiiError
Base class for errors.
Definition: exceptions.hpp:358
elt::error::CiiException::getCiiMessage
virtual std::string getCiiMessage() const
Definition: exceptions.hpp:101
elt::error::CiiException::GetOriginalStackTrace
const boost::stacktrace::stacktrace & GetOriginalStackTrace() const
Return the stack trace captured in the most nested exception constructor.
Definition: exceptions.cpp:46
elt::error::CiiException::GetHostName
const std::string & GetHostName() const
Return the name of the host in which the exception was created.
Definition: exceptions.cpp:118
elt::error::CiiException::SetFileName
void SetFileName(const std::string &fileName)
Set the name of the file in which the exception was thrown.
Definition: exceptions.cpp:153
elt::error::CiiException::GetProcessId
int GetProcessId() const
Return the id of the process in which the exception was created.
Definition: exceptions.cpp:114
elt::error::CiiException
Base class for the exceptions thrown by the client API.
Definition: exceptions.hpp:36
elt::error::CiiException::GetStackTraceAsString
std::string GetStackTraceAsString() const
Same as GetStackTrace() but convert the trace to a string.
Definition: exceptions.cpp:68
elt::error::CiiException::getCiiHostName
virtual std::string getCiiHostName() const
Definition: exceptions.hpp:91
elt::error::CiiException::GetOriginalStackTraceAsString
std::string GetOriginalStackTraceAsString() const
Same as GetOriginalStackTrace() but convert the trace to a string.
Definition: exceptions.cpp:74
elt::error::CiiException::GetStackTrace
const boost::stacktrace::stacktrace & GetStackTrace() const
Return the stack trace captured in the exception constructor.
Definition: exceptions.cpp:42
elt::error::CiiException::getCiiClassName
virtual std::string getCiiClassName() const
Definition: exceptions.hpp:83
elt
Definition: exceptions.cpp:21
elt::error::CiiException::CiiException
CiiException(const std::string &message)
Constructor.
Definition: exceptions.cpp:34
elt::error::CiiException::~CiiException
virtual ~CiiException()=default
Destructor.
elt::error::CiiException::what
const char * what() const noexcept override
Definition: exceptions.cpp:226
elt::error::CiiException::GetSeverity
std::string GetSeverity() const
elt::error::CiiException::GetTypeName
std::string GetTypeName() const
Return the exception class name.
Definition: exceptions.cpp:122
elt::error::CiiException::getCiiExceptionStack
virtual std::vector< std::string > getCiiExceptionStack() const
Definition: exceptions.hpp:97
mudpi::uint32_t
unsigned int uint32_t
Definition: mudpi.h:16
elt::error::CiiException::SetClassName
void SetClassName(const std::string &className)
Set the name of the class in which the exception was thrown.
Definition: exceptions.cpp:174
elt::error::CiiException::SetCiiExceptionStack
void SetCiiExceptionStack(const std::vector< std::string > &nested_exceptions)
Definition: exceptions.cpp:221
elt::error::CiiException::GetCiiStackTrace
const std::string GetCiiStackTrace() const noexcept
Definition: exceptions.hpp:113
elt::error::CiiException::GetFunctionName
const std::string & GetFunctionName() const
Return the name of the function in which the exception was thrown.
Definition: exceptions.cpp:149
elt::error::CiiException::GetThreadId
std::thread::id GetThreadId() const
Return the id of the thread in which the exception was created.
Definition: exceptions.cpp:110
elt::error::CiiException::getCiiCreationDate
virtual int64_t getCiiCreationDate() const
Definition: exceptions.hpp:109
elt::error::CiiException::clock_t
std::chrono::system_clock clock_t
The clock to use for determining the exception creation time.
Definition: exceptions.hpp:49
elt::error::CiiException::getCiiDetails
virtual std::string getCiiDetails() const
Definition: exceptions.hpp:105
elt::error::CiiException::getCiiType
virtual std::string getCiiType() const
Definition: exceptions.hpp:75
elt::error::CiiException::GetLineNumber
uint32_t GetLineNumber() const
Return the line number in which the exception was thrown.
Definition: exceptions.cpp:140
error
void error(const char *msg)
Definition: main.cpp:29
elt::error::CiiException::getCiiStackTrace
virtual std::string getCiiStackTrace() const
Definition: exceptions.cpp:61
elt::error::CiiException::SetDetails
void SetDetails(const std::string &details)
Set the exception details.
Definition: exceptions.cpp:162
rtctkExampleDataTaskGenFitsData.filename
filename
Definition: rtctkExampleDataTaskGenFitsData.py:13
elt::error::CiiException::Dump
std::string Dump() const
Return all the exception's details.
Definition: exceptions.cpp:178
elt::error::CiiException::SetFunctionName
void SetFunctionName(const std::string &functionName)
Set the name of the function in which the exception was thrown.
Definition: exceptions.cpp:144
elt::error::CiiException::SetLineNumber
void SetLineNumber(uint32_t lineNumber)
Set the line number in which the exception was thrown.
Definition: exceptions.cpp:126