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_NOTICE(x) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_N(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[2], #x "=", x, hmbdc::app::LogTrailer(__FILE__, __LINE__))
24 #define HMBDC_LOG_WARNING(x) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_N(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[3], #x "=", x, hmbdc::app::LogTrailer(__FILE__, __LINE__))
25 #define HMBDC_LOG_CRITICAL(x) if (hmbdc::app::SyncLogger::initialized()) hmbdc::app::SyncLogger::instance().LOG_N(hmbdc::time::SysTime::now(), hmbdc::app::g_SyncLogLevelStr[4], #x "=", x, hmbdc::app::LogTrailer(__FILE__, __LINE__))
26 #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')
27 #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')
28 #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')
29 #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')
30 
31 #define HMBDC_LOG_ONCE(...) {static bool done = false; if (!done) {done = true; {__VA_ARGS__}}}
32 
33 namespace hmbdc { namespace app {
34 
35 char const g_SyncLogLevelStr[][12 + 1] = {
36  " DEBUG : ",
37  " RDEBUG : ",
38  " NOTICE : ",
39  " WARNING : ",
40  " CRITICAL: "
41 };
42 
43 
44 struct LogTrailer {
45  LogTrailer(char const* const file, int line)
46  : f(file)
47  , l(line) {
48  }
49  char const* const f;
50  int l;
51 
52  friend std::ostream& operator << (std::ostream& os, LogTrailer const& t) {
53  os << ' ' << t.f << ':' << t.l << std::endl;
54  return os;
55  }
56 };
58  friend std::ostream& operator << (std::ostream& os, EmptyLogTrailer const&) {
59  os << std::endl;
60  return os;
61  }
62 };
63 
64 /**
65  * @brief a very straightforward logger that works synchronisely.
66  * @details Only use the macrs defined above. Only use for light logging
67  * refer to utils::AsyncLoggerT for heavy logging
68  */
69 struct SyncLogger
70 : pattern::GuardedSingleton<SyncLogger> {
72 
73  enum Level {
74  L_DEBUG = 0,
75  L_RDEBUG,
76  L_NOTICE,
77  L_WARNING,
78  L_CRITICAL,
79  L_OFF
80  };
81 
82  void setMinLogLevel(Level minLevel) {
83  minLevel_ = minLevel;
84  }
85 
86  template <typename ...Args>
87  void LOG_D(Args&&... args) {
88 #ifndef NDEBUG
89  if (minLevel_ <= L_DEBUG) {
90  std::lock_guard<std::recursive_mutex> g(mutex_);
91  log(std::forward<Args>(args)...);
92  }
93 #endif
94  }
95 
96  template <typename ...Args>
97  void LOG_R(Args&&... args) {
98 #ifdef HMBDC_RUNTIME_DEBUG
99  if (minLevel_ <= L_RDEBUG) {
100  std::lock_guard<std::recursive_mutex> g(mutex_);
101  log(std::forward<Args>(args)...);
102  }
103 #endif
104  }
105 
106  template <typename ...Args>
107  void LOG_N(Args&&... args) {
108  if (minLevel_ <= L_NOTICE) {
109  std::lock_guard<std::recursive_mutex> g(mutex_);
110  log(std::forward<Args>(args)...);
111  }
112  }
113  template <typename ...Args>
114  void LOG_W(Args&&... args) {
115  if (minLevel_ <= L_WARNING) {
116  std::lock_guard<std::recursive_mutex> g(mutex_);
117  log(std::forward<Args>(args)...);
118  }
119  }
120  template <typename ...Args>
121  void LOG_C(Args&&... args) {
122  if (minLevel_ <= L_CRITICAL) {
123  std::lock_guard<std::recursive_mutex> g(mutex_);
124  log(std::forward<Args>(args)...);
125  }
126  }
127 
128 private:
129  template <typename Arg, typename ...Args>
130  void log(Arg&& arg, Args&&... args) {
131  log_ << std::forward<Arg>(arg);
132  log(std::forward<Args>(args)...);
133  }
134  void log() {
135  }
136  template <typename ... NoOpArgs>
137  SyncLogger(std::ostream& log, NoOpArgs&&...)
138  : log_(log)
139 #ifndef NDEBUG
140  , minLevel_(L_DEBUG)
141 #else
142  , minLevel_(L_NOTICE)
143 #endif
144  {}
145  std::ostream& log_;
146  Level minLevel_;
147  std::recursive_mutex mutex_;
148 };
149 }}
a very straightforward logger that works synchronisely.
Definition: Logger.hpp:69
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:57
Definition: Base.hpp:12
Definition: Logger.hpp:44