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