VCCC  2024.05
VisualCamp Common C++ library
logger.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/12/08.
3 # */
4 #
5 # ifndef VCCC_LOG_LOG_HPP
6 # define VCCC_LOG_LOG_HPP
7 #
10 # include "vccc/__log/detail/tag.h"
12 
13 
14 namespace vccc {
15 
18 
54 class Logger {
55  public:
56  enum Priority {
61  };
62 
64  using string_type = std::string;
65 
66  constexpr Logger() = default;
67 
69  template<typename ...Args> void d(const Args&... args) const {
70  d_(VCCC_LOG_TAG_DEBUG, to_string(args...));
71  }
72 
74  template<typename ...Args> void i(const Args&... args) const {
75  i_(VCCC_LOG_TAG_INFO, to_string(args...));
76  }
77 
79  template<typename ...Args> void w(const Args&... args) const {
80  w_(VCCC_LOG_TAG_WARN, to_string(args...));
81  }
82 
84  template<typename ...Args> void e(const Args&... args) const {
85  e_(VCCC_LOG_TAG_ERROR, to_string(args...));
86  }
87 
88  template<typename... Args>
89  void operator()(Priority priority, const char* tag, const Args&... args) const {
90  switch (priority) {
91  case kDebug:
92  this->d_(tag, to_string(args...));
93  return;
94 
95  case kInfo:
96  this->i_(tag, to_string(args...));
97  return;
98 
99  case kWarn:
100  this->w_(tag, to_string(args...));
101  return;
102 
103  case kError:
104  this->e_(tag, to_string(args...));
105  return;
106  }
107  }
108 
115  template<typename ...Args> string_type to_string(const Args&... args) const {
116  stream_type stream;
117  int dummy[] = { (stream << args, 0)... };
118  return stream.stream().str();
119  }
120 
127  }
128 
129  private:
130  template<typename ...Args> void d_(const char* tag, const std::string& str) const { LOGD_IMPL(tag, "%s", str.c_str()); }
131  template<typename ...Args> void i_(const char* tag, const std::string& str) const { LOGI_IMPL(tag, "%s", str.c_str()); }
132  template<typename ...Args> void w_(const char* tag, const std::string& str) const { LOGW_IMPL(tag, "%s", str.c_str()); }
133  template<typename ...Args> void e_(const char* tag, const std::string& str) const { LOGE_IMPL(tag, "%s", str.c_str()); }
134 };
135 
140 
143 
144 # ifdef NDEBUG
145 # define LOGD(...)
146 # define LOGID(...)
147 # define LOGWD(...)
148 # define LOGED(...)
149 #else
161 # define LOGD(...) ::vccc::Log.d(__VA_ARGS__)
162 # define LOGID(...) ::vccc::Log.i(__VA_ARGS__)
163 # define LOGWD(...) ::vccc::Log.w(__VA_ARGS__)
164 # define LOGED(...) ::vccc::Log.e(__VA_ARGS__)
165 # endif
166 
168 # define LOGI(...) ::vccc::Log.i(__VA_ARGS__)
170 # define LOGW(...) ::vccc::Log.w(__VA_ARGS__)
172 # define LOGE(...) ::vccc::Log.e(__VA_ARGS__)
174 
176 
177 } // namespace log
178 
179 # endif //VCCC_LOG_LOG_HPP
stream wrapper that supports extended operator overloading
Definition: stream_wrapper.hpp:186
stream_type & stream()
Return internal stream.
Definition: stream_wrapper.hpp:237
string_type str() const
Equals to stream().str()
Definition: stream_wrapper.hpp:244
Ease use of vccc::StreamWrapper.
Definition: logger.hpp:54
void w(const Args &... args) const
Warning log.
Definition: logger.hpp:79
constexpr Logger()=default
static string_type & global_separator()
Equals to stream_type::global_separator.
Definition: logger.hpp:125
Priority
Definition: logger.hpp:56
@ kDebug
Definition: logger.hpp:57
@ kError
Definition: logger.hpp:60
@ kWarn
Definition: logger.hpp:59
@ kInfo
Definition: logger.hpp:58
void i(const Args &... args) const
Informational log.
Definition: logger.hpp:74
void operator()(Priority priority, const char *tag, const Args &... args) const
Definition: logger.hpp:89
void e(const Args &... args) const
Error log.
Definition: logger.hpp:84
void d(const Args &... args) const
Log output as debug.
Definition: logger.hpp:69
string_type to_string(const Args &... args) const
Return logged value as std::string.
Definition: logger.hpp:115
std::string string_type
Definition: logger.hpp:64
static string_type & global_separator()
Get global separator.
Definition: stream_wrapper.hpp:108
BasicStreamWrapper< char > StreamWrapper
Definition: stream_wrapper.hpp:657
constexpr VCCC_INLINE_OR_STATIC Logger Log
Global vccc::Logger instance for syntax sugar.
Definition: logger.hpp:139
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
#define LOGD_IMPL(tag, fmt,...)
Definition: log_impl.h:13
#define LOGW_IMPL(tag, fmt,...)
Definition: log_impl.h:17
#define LOGI_IMPL(tag, fmt,...)
Definition: log_impl.h:16
#define LOGE_IMPL(tag, fmt,...)
Definition: log_impl.h:18
Definition: directory.h:12
#define VCCC_LOG_TAG_INFO
Definition: tag.h:9
#define VCCC_LOG_TAG_DEBUG
Definition: tag.h:8
#define VCCC_LOG_TAG_ERROR
Definition: tag.h:11
#define VCCC_LOG_TAG_WARN
Definition: tag.h:10