BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
WithTrace.h
1 #pragma once
2 
3 #include "util/Backtrace.h"
4 
5 // Avoid including Poco/Exception.h here as this is up to
6 // the user of the WithTrace template class.
7 namespace Poco {
8 
9 class Exception;
10 
11 }
12 
13 namespace BeeeOn {
14 
15 class Throwable {
16 public:
17  Throwable();
18  virtual ~Throwable();
19 
20  const Backtrace &trace() const;
21 
28  template <typename E>
29  static const Backtrace &traceOf(const E &e);
30 
31 protected:
32  static const Backtrace &emptyBacktrace();
33 
34 protected:
35  Backtrace m_backtrace;
36 };
37 
38 template <typename E>
39 const Backtrace &Throwable::traceOf(const E &e)
40 {
41  try {
42  const Throwable &t = dynamic_cast<const Throwable &>(e);
43  return t.trace();
44  } catch (...) {
45  return emptyBacktrace();
46  }
47 }
48 
49 namespace ForPoco { // BeeeOn::ForPoco - only for Poco::Exception and derived classes
50 
51 template <typename E>
52 class WithTrace : public Throwable, public E {
53 public:
54  WithTrace(const std::string &msg, int code = 0):
55  E(msg, code)
56  {
57  extendWithTrace();
58  }
59 
60  WithTrace(const std::string &msg, const std::string &arg, int code = 0):
61  E(msg, arg, code)
62  {
63  extendWithTrace();
64  }
65 
66  WithTrace(const std::string &msg, const ::Poco::Exception &nested, int code = 0):
67  E(msg, nested, code)
68  {
69  extendWithTrace();
70  }
71 
72  WithTrace(const E &e):
73  E(e)
74  {
75  extendWithTrace();
76  }
77 
78  ~WithTrace() throw()
79  {
80  }
81 
82 private:
83  void extendWithTrace()
84  {
85  if (trace().size() > 0)
86  this->extendedMessage("\n" + trace().toString(" "));
87  }
88 };
89 
90 }
91 
92 }
Definition: WithTrace.h:52
Definition: Backtrace.h:15
static const Backtrace & traceOf(const E &e)
Definition: WithTrace.h:39
Definition: WithTrace.h:15