BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Loggable.h
1 #pragma once
2 
3 #include <typeinfo>
4 #include <string>
5 
6 #include <Poco/Exception.h>
7 #include <Poco/Message.h>
8 
9 namespace Poco {
10 
11 class Logger;
12 
13 }
14 
15 namespace BeeeOn {
16 
17 class ClassInfo;
18 
19 class Loggable {
20 public:
21  Loggable();
22  Loggable(const ClassInfo &info);
23  Loggable(const std::type_info &info);
24  virtual ~Loggable();
25 
26  static Poco::Logger &forMethod(const char *name);
27  static Poco::Logger &forClass(const ClassInfo &info);
28  static Poco::Logger &forClass(const std::type_info &info);
29 
30  template <typename T>
31  static Poco::Logger &forInstance(const T *i)
32  {
33  return forClass(typeid(*i));
34  }
35 
36  static void configureSimple(
37  Poco::Logger &logger, const std::string &level);
38 
39  static void logException(
40  Poco::Logger &logger,
41  const Poco::Message::Priority priority,
42  const Poco::Exception &e,
43  const char *file,
44  size_t line);
45 
46 protected:
47  void setupLogger(Poco::Logger *logger = 0) const;
48 
49  Poco::Logger &logger() const
50  {
51  setupLogger();
52  return *m_logger;
53  }
54 
55 private:
56  const std::string m_name;
57  mutable Poco::Logger *m_logger;
58 };
59 
60 }
61 
62 #define BEEEON_CATCH_CHAIN(logger) \
63  catch (const Poco::Exception &e) { \
64  Loggable::logException((logger), \
65  Poco::Message::PRIO_ERROR, e, __FILE__, __LINE__); \
66  } \
67  catch (const std::exception &e) { \
68  (logger).critical(e.what(), __FILE__, __LINE__); \
69  } \
70  catch (const char *m) { \
71  (logger).fatal(m, __FILE__, __LINE__); \
72  } \
73  catch (...) { \
74  (logger).fatal("unknown error occured", __FILE__, __LINE__); \
75  }
76 
77 #define BEEEON_CATCH_CHAIN_MESSAGE(logger, message) \
78  catch (const Poco::Exception &e) { \
79  Loggable::logException((logger), \
80  Poco::Message::PRIO_ERROR, e, __FILE__, __LINE__); \
81  (logger).error(message, __FILE__, __LINE__); \
82  } \
83  catch (const std::exception &e) { \
84  (logger).critical(e.what(), __FILE__, __LINE__); \
85  (logger).critical(message, __FILE__, __LINE__); \
86  } \
87  catch (const char *m) { \
88  (logger).fatal(m, __FILE__, __LINE__); \
89  (logger).fatal(message, __FILE__, __LINE__); \
90  } \
91  catch (...) { \
92  (logger).fatal("unknown error occured", __FILE__, __LINE__); \
93  (logger).fatal(message, __FILE__, __LINE__); \
94  }
95 
96 #define BEEEON_CATCH_CHAIN_RETHROW(logger) \
97  catch (const Poco::Exception &e) { \
98  Loggable::logException((logger), \
99  Poco::Message::PRIO_ERROR, e, __FILE__, __LINE__); \
100  e.rethrow(); \
101  } \
102  catch (const std::exception &e) { \
103  (logger).critical(e.what(), __FILE__, __LINE__); \
104  throw Poco::RuntimeException(e.what()); \
105  } \
106  catch (const char *m) { \
107  (logger).fatal(m, __FILE__, __LINE__); \
108  throw Poco::RuntimeException(m); \
109  } \
110  catch (...) { \
111  (logger).fatal("unknown error occured", __FILE__, __LINE__); \
112  throw Poco::RuntimeException("unknown error"); \
113  }
114 
115 #define BEEEON_CATCH_CHAIN_ACTION(logger, action) \
116  catch (const Poco::Exception &e) { \
117  Loggable::logException((logger), \
118  Poco::Message::PRIO_ERROR, e, __FILE__, __LINE__); \
119  action; \
120  } \
121  catch (const std::exception &e) { \
122  (logger).critical(e.what(), __FILE__, __LINE__); \
123  action; \
124  } \
125  catch (const char *m) { \
126  (logger).fatal(m, __FILE__, __LINE__); \
127  action; \
128  } \
129  catch (...) { \
130  (logger).fatal("unknown error occured", __FILE__, __LINE__); \
131  action; \
132  }
133 
134 #define BEEEON_CATCH_CHAIN_ACTION_RETHROW(logger, action) \
135  catch (const Poco::Exception &e) { \
136  Loggable::logException((logger), \
137  Poco::Message::PRIO_ERROR, e, __FILE__, __LINE__); \
138  action; \
139  e.rethrow(); \
140  } \
141  catch (const std::exception &e) { \
142  (logger).critical(e.what(), __FILE__, __LINE__); \
143  action; \
144  throw Poco::RuntimeException(e.what()); \
145  } \
146  catch (const char *m) { \
147  (logger).fatal(m, __FILE__, __LINE__); \
148  action; \
149  throw Poco::RuntimeException(m); \
150  } \
151  catch (...) { \
152  (logger).fatal("unknown error occured", __FILE__, __LINE__); \
153  action; \
154  throw Poco::RuntimeException("unknown error"); \
155  }
Definition: ClassInfo.h:9
Definition: Loggable.h:19