hmbdc
simplify-high-performance-messaging-programming
Logger.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/time/Time.hpp"
5 #include "hmbdc/pattern/GuardedSingleton.hpp"
6 #include <ostream>
7 #include <mutex>
8 
9 
10 #ifdef HMBDC_RUNTIME_DEBUG
11 #define HMBDC_LOG_R(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_R(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[1], __VA_ARGS__, hmbdc::app::LogTrailer(__FILE__, __LINE__))
12 #define HMBDC_LOG_r(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_R(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[1], __VA_ARGS__, '\n')
13 #else
14 #define HMBDC_LOG_R(...)
15 #define HMBDC_LOG_r(...)
16 #endif
17 
18 #define HMBDC_LOG_D(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_D(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[0], __VA_ARGS__, hmbdc::app::LogTrailer(__FILE__, __LINE__))
19 #define HMBDC_LOG_N(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_N(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[2], __VA_ARGS__, hmbdc::app::LogTrailer(__FILE__, __LINE__))
20 #define HMBDC_LOG_W(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_W(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[3], __VA_ARGS__, hmbdc::app::LogTrailer(__FILE__, __LINE__))
21 #define HMBDC_LOG_C(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_C(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[4], __VA_ARGS__, hmbdc::app::LogTrailer(__FILE__, __LINE__))
22 #define HMBDC_LOG_DEBUG(x) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_D(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[0], #x "=", x, hmbdc::app::LogTrailer(__FILE__, __LINE__))
23 #define HMBDC_LOG_d(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_D(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[0], __VA_ARGS__, '\n')
24 #define HMBDC_LOG_n(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_N(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[2], __VA_ARGS__, '\n')
25 #define HMBDC_LOG_w(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_W(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[3], __VA_ARGS__, '\n')
26 #define HMBDC_LOG_c(...) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_C(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[4], __VA_ARGS__, '\n')
27 
28 #define HMBDC_LOG_ONCE(...) {static bool done = false; if (!done) {done = true; {__VA_ARGS__}}}
29 
30 namespace hmbdc { namespace app {
31 
32 char const g_SyncLogLevelStr[][12 + 1] = {
33  " DEBUG : ",
34  " RDEBUG : ",
35  " NOTICE : ",
36  " WARNING : ",
37  " CRITICAL: "
38 };
39 
40 
41 struct LogTrailer {
42  LogTrailer(char const* const file, int line)
43  : f(file)
44  , l(line) {
45  }
46  char const* const f;
47  int l;
48 
49  friend std::ostream& operator << (std::ostream& os, LogTrailer const& t) {
50  os << ' ' << t.f << ':' << t.l << std::endl;
51  return os;
52  }
53 };
55  friend std::ostream& operator << (std::ostream& os, EmptyLogTrailer const&) {
56  os << std::endl;
57  return os;
58  }
59 };
60 
61 /**
62  * @brief a very straightforward logger that works synchronisely.
63  * @details Only use the macrs defined above. Only use for light logging
64  * refer to utils::AsyncLoggerT for heavy logging
65  */
66 struct SyncLogger
67 : pattern::GuardedSingleton<SyncLogger> {
68  friend struct pattern::SingletonGuardian<SyncLogger>;
69 
70  enum Level {
71  L_DEBUG = 0,
72  L_RDEBUG,
73  L_NOTICE,
74  L_WARNING,
75  L_CRITICAL,
76  L_OFF
77  };
78 
79  void setMinLogLevel(Level minLevel) {
80  minLevel_ = minLevel;
81  }
82 
83  template <typename ...Args>
84  void LOG_D(Args&&... args) {
85 #ifndef NDEBUG
86  std::lock_guard<std::recursive_mutex> g(mutex_);
87  log(std::forward<Args>(args)...);
88 #endif
89  }
90 
91  template <typename ...Args>
92  void LOG_R(Args&&... args) {
93 #ifdef HMBDC_RUNTIME_DEBUG
94  if (minLevel_ <= L_RDEBUG) {
95  std::lock_guard<std::recursive_mutex> g(mutex_);
96  log(std::forward<Args>(args)...);
97  }
98 #endif
99  }
100 
101  template <typename ...Args>
102  void LOG_N(Args&&... args) {
103  if (minLevel_ <= L_NOTICE) {
104  std::lock_guard<std::recursive_mutex> g(mutex_);
105  log(std::forward<Args>(args)...);
106  }
107  }
108  template <typename ...Args>
109  void LOG_W(Args&&... args) {
110  if (minLevel_ <= L_WARNING) {
111  std::lock_guard<std::recursive_mutex> g(mutex_);
112  log(std::forward<Args>(args)...);
113  }
114  }
115  template <typename ...Args>
116  void LOG_C(Args&&... args) {
117  if (minLevel_ <= L_CRITICAL) {
118  std::lock_guard<std::recursive_mutex> g(mutex_);
119  log(std::forward<Args>(args)...);
120  }
121  }
122 
123 private:
124  template <typename Arg, typename ...Args>
125  void log(Arg&& arg, Args&&... args) {
126  log_ << std::forward<Arg>(arg);
127  log(std::forward<Args>(args)...);
128  }
129  void log() {
130  }
131  template <typename ... NoOpArgs>
132  SyncLogger(std::ostream& log, NoOpArgs&&...)
133  : log_(log)
134 #ifndef NDEBUG
135  , minLevel_(L_DEBUG)
136 #else
137  , minLevel_(L_NOTICE)
138 #endif
139  {}
140  std::ostream& log_;
141  Level minLevel_;
142  std::recursive_mutex mutex_;
143 };
144 }}
145 
a very straightforward logger that works synchronisely.
Definition: Logger.hpp:66
base for the Singleton that works with SingletonGuardian
Definition: GuardedSingleton.hpp:53
RAII representing the lifespan of the underlying Singleton which also ganrantees the singularity of u...
Definition: GuardedSingleton.hpp:20
Definition: Logger.hpp:54
Definition: Base.hpp:13
Definition: Logger.hpp:41