SWC-DB  v0.5.11 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Logger.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_core_Logger_h
8 #define swcdb_core_Logger_h
9 
10 #include "swcdb/core/Compat.h"
11 #include "swcdb/core/MutexSptd.h"
12 
13 #include <cstdio>
14 #include <iostream>
15 #include <sys/stat.h>
16 #include <stdio.h>
17 
18 
19 
20 namespace SWC {
21 
22 
28 enum LogPriority : uint8_t {
29  LOG_FATAL = 0,
30  LOG_ALERT = 1,
31  LOG_CRIT = 2,
32  LOG_ERROR = 3,
33  LOG_WARN = 4,
35  LOG_INFO = 6,
36  LOG_DEBUG = 7
37 };
38 
39 
44 #define SWC_LOG_OSTREAM std::cout
45 
46 
47 namespace Core {
48 
49 
50 class LogWriter final {
51  public:
52 
53  static std::string repr(uint8_t priority);
54 
55  static uint8_t SWC_PURE_FUNC
56  from_string(const std::string& loglevel) noexcept;
57 
58 
60 
61 
62  LogWriter(const std::string& name="", const std::string& logs_path="");
63 
64  ~LogWriter() noexcept;
65 
66  void initialize(const std::string& name);
67 
68  void daemon(const std::string& logs_path);
69 
70  constexpr SWC_CAN_INLINE
71  void set_level(uint8_t level) noexcept {
72  m_priority.store(level);
73  }
74 
75  constexpr SWC_CAN_INLINE
76  uint8_t get_level() const noexcept {
77  return m_priority;
78  }
79 
80  constexpr SWC_CAN_INLINE
81  bool is_enabled(uint8_t level) const noexcept {
82  return level <= m_priority;
83  }
84 
85  constexpr SWC_CAN_INLINE
86  bool show_line_numbers() const noexcept {
87  return m_show_line_numbers;
88  }
89 
90  void log(uint8_t priority, const char* fmt, ...)
91  noexcept __attribute__((format(printf, 3, 4)));
92 
93  void log(uint8_t priority, const char* filen, int fline,
94  const char* fmt, ...)
95  noexcept __attribute__((format(printf, 5, 6)));
96 
97  template<typename T>
98  SWC_SHOULD_NOT_INLINE
99  void msg(uint8_t priority, const T& msg) noexcept {
100  try {
101  MutexSptd::scope lock(mutex);
102  _time_and_level(priority);
103  SWC_LOG_OSTREAM << msg << std::endl;
104  } catch(...) { }
105  }
106 
107  template<typename T>
108  SWC_SHOULD_NOT_INLINE
109  void msg(uint8_t priority, const char* filen, int fline,
110  const T& msg) noexcept {
111  try {
112  MutexSptd::scope lock(mutex);
113  _print_prefix(priority, filen, fline);
114  SWC_LOG_OSTREAM << msg << std::endl;
115  } catch(...) { }
116  }
117 
118  void _print_prefix(uint8_t priority, const char* filen, int fline);
119 
120 
121  private:
122 
123  void _time_and_level(uint8_t priority);
124 
125  void _renew_files(time_t secs);
126 
127  std::string m_name;
128  std::string m_logs_path;
129  FILE* m_file_out;
130  //FILE* m_file_err;
133  bool m_daemon;
134  time_t m_next_time;
135 };
136 
137 
138 extern LogWriter logger;
139 
140 }} // namespace SWC::Core
141 
142 
143 
144 
151 #define SWC_LOG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
152 
153 #define SWC_PRINT { try { \
154  ::SWC::Core::MutexSptd::scope \
155  swcdb_logger_lock(::SWC::Core::logger.mutex); \
156  SWC_LOG_OSTREAM
157 #define SWC_PRINT_CLOSE std::endl; } catch(...) { } }
158 
159 
160 #ifndef SWC_DISABLE_LOG_ALL
161 
162 
163 // stream interface, SWC_LOG_OUT(LOG_ERROR, code{SWC_LOG_OSTREAM << "msg";});
164 #define SWC_LOG_OUT(pr, _code_) { \
165  uint8_t _log_pr = pr; \
166  if(::SWC::Core::logger.is_enabled(_log_pr)) { try { \
167  ::SWC::Core::MutexSptd::scope \
168  swcdb_logger_lock(::SWC::Core::logger.mutex); \
169  ::SWC::Core::logger._print_prefix(_log_pr, __FILE__, __LINE__); \
170  {_code_}; \
171  SWC_LOG_OSTREAM << std::endl; \
172  } catch(...) { } } }
173 
174 #define SWC_LOGF(priority, fmt, ...) \
175  SWC_LOG_OUT(priority, SWC_LOG_PRINTF(fmt, __VA_ARGS__); )
176 
177 #define SWC_LOG(priority, message) \
178  if(::SWC::Core::logger.is_enabled(priority)) { \
179  if(::SWC::Core::logger.show_line_numbers()) \
180  ::SWC::Core::logger.msg(priority, __FILE__, __LINE__, message); \
181  else \
182  ::SWC::Core::logger.msg(priority, message); \
183  }
184 
185 
186 #ifndef SWC_DISABLE_LOG_FATAL
187 #define SWC_LOG_FATAL(msg) do { \
188  SWC_LOG(::SWC::LOG_FATAL, msg); \
189  SWC_ABORT; \
190 } while (0)
191 #define SWC_LOG_FATALF(msg, ...) do { \
192  SWC_LOGF(::SWC::LOG_FATAL, msg, __VA_ARGS__); \
193  SWC_ABORT; \
194 } while (0)
195 #else
196 #define SWC_LOG_FATAL(msg)
197 #define SWC_LOG_FATALF(msg, ...)
198 #endif
199 
200 #else // SWC_DISABLE_LOGGING
201 
202 #define SWC_LOG(priority, msg)
203 #define SWC_LOGF(priority, fmt, ...)
204 
205 #define SWC_LOG_OUT(pr, code)
206 
207 #define SWC_LOG_FATAL(msg)
208 #define SWC_LOG_FATAL(msg, ...)
209 
210 #endif // SWC_DISABLE_LOGGING
211 
212 
213 
219 #ifdef SWC_IMPL_SOURCE
220 #include "swcdb/core/Logger.cc"
221 #endif
222 
223 #endif // swcdb_core_Logger_h
SWC::Core::LogWriter::is_enabled
constexpr SWC_CAN_INLINE bool is_enabled(uint8_t level) const noexcept
Definition: Logger.h:81
SWC::Core::logger
LogWriter logger
Definition: Logger.cc:15
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::Core::LogWriter::daemon
void daemon(const std::string &logs_path)
Definition: Logger.cc:81
SWC::LOG_FATAL
@ LOG_FATAL
Definition: Logger.h:29
SWC::Core::LogWriter::m_logs_path
std::string m_logs_path
Definition: Logger.h:128
SWC::Core::Atomic< uint8_t >
SWC::Core::LogWriter
Definition: Logger.h:50
SWC::LOG_ALERT
@ LOG_ALERT
Definition: Logger.h:30
SWC::LOG_INFO
@ LOG_INFO
Definition: Logger.h:35
SWC::Core::LogWriter::m_name
std::string m_name
Definition: Logger.h:127
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Core::LogWriter::m_next_time
time_t m_next_time
Definition: Logger.h:134
SWC::Core::LogWriter::get_level
constexpr SWC_CAN_INLINE uint8_t get_level() const noexcept
Definition: Logger.h:76
SWC::Core::LogWriter::initialize
void initialize(const std::string &name)
Definition: Logger.cc:74
SWC::Core::LogWriter::m_file_out
FILE * m_file_out
Definition: Logger.h:129
SWC::Core::AtomicBase::store
constexpr SWC_CAN_INLINE void store(T v) noexcept
Definition: Atomic.h:37
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::LOG_DEBUG
@ LOG_DEBUG
Definition: Logger.h:36
SWC::Core::LogWriter::m_priority
Core::Atomic< uint8_t > m_priority
Definition: Logger.h:131
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC_PURE_FUNC
#define SWC_PURE_FUNC
Definition: Compat.h:108
Compat.h
SWC::Core::LogWriter::msg
void void SWC_SHOULD_NOT_INLINE void msg(uint8_t priority, const T &msg) noexcept
Definition: Logger.h:99
SWC::LogPriority
LogPriority
Definition: Logger.h:28
SWC::Core::LogWriter::_print_prefix
void _print_prefix(uint8_t priority, const char *filen, int fline)
Definition: Logger.cc:196
SWC::Core::LogWriter::msg
SWC_SHOULD_NOT_INLINE void msg(uint8_t priority, const char *filen, int fline, const T &msg) noexcept
Definition: Logger.h:109
SWC::Core::LogWriter::m_daemon
bool m_daemon
Definition: Logger.h:133
SWC::LOG_ERROR
@ LOG_ERROR
Definition: Logger.h:32
SWC::Core::LogWriter::log
void log(uint8_t priority, const char *fmt,...) noexcept __attribute__((format(printf
Definition: Logger.cc:163
SWC::Core::LogWriter::from_string
static uint8_t SWC_PURE_FUNC from_string(const std::string &loglevel) noexcept
Definition: Logger.cc:44
SWC::format
std::string format(const char *fmt,...) __attribute__((format(printf
Definition: String.cc:17
SWC::LOG_CRIT
@ LOG_CRIT
Definition: Logger.h:31
SWC::Core::LogWriter::show_line_numbers
constexpr SWC_CAN_INLINE bool show_line_numbers() const noexcept
Definition: Logger.h:86
MutexSptd.h
SWC::Core::LogWriter::_renew_files
void _renew_files(time_t secs)
Definition: Logger.cc:118
SWC::Core::LogWriter::~LogWriter
~LogWriter() noexcept
Definition: Logger.cc:71
SWC::Config::T
const uint64_t T
Definition: Property.h:27
SWC::LOG_NOTICE
@ LOG_NOTICE
Definition: Logger.h:34
SWC::Core::LogWriter::m_show_line_numbers
bool m_show_line_numbers
Definition: Logger.h:132
SWC::Core::LogWriter::mutex
MutexSptd mutex
Definition: Logger.h:59
Logger.cc
SWC::Core::MutexSptd
Definition: MutexSptd.h:16
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Core::__attribute__
struct SWC::Core::BitFieldInt __attribute__((packed))
SWC::Core::LogWriter::_time_and_level
void _time_and_level(uint8_t priority)
Definition: Logger.cc:102
SWC::Core::LogWriter::LogWriter
LogWriter(const std::string &name="", const std::string &logs_path="")
Definition: Logger.cc:65
SWC::Core::LogWriter::set_level
constexpr SWC_CAN_INLINE void set_level(uint8_t level) noexcept
Definition: Logger.h:71
SWC::Core::LogWriter::repr
static std::string repr(uint8_t priority)
Definition: Logger.cc:38