SWC-DB  v0.5.12 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() noexcept;
63 
64  LogWriter(LogWriter&&) = delete;
65 
66  LogWriter(const LogWriter&) = delete;
67 
68  LogWriter& operator=(const LogWriter&) = delete;
69 
70  LogWriter& operator=(LogWriter&&) = delete;
71 
72 
73  ~LogWriter() noexcept;
74 
75  void initialize(const std::string& name);
76 
77  void daemon(const std::string& logs_path);
78 
79  constexpr SWC_CAN_INLINE
80  const std::string& name() const noexcept {
81  return m_name;
82  }
83 
84  constexpr SWC_CAN_INLINE
85  void set_level(uint8_t level) noexcept {
86  m_priority.store(level);
87  }
88 
89  constexpr SWC_CAN_INLINE
90  uint8_t get_level() const noexcept {
91  return m_priority;
92  }
93 
94  constexpr SWC_CAN_INLINE
95  bool is_enabled(uint8_t level) const noexcept {
96  return level <= m_priority;
97  }
98 
99  constexpr SWC_CAN_INLINE
100  bool show_line_numbers() const noexcept {
101  return m_show_line_numbers;
102  }
103 
104  void log(uint8_t priority, const char* fmt, ...)
105  noexcept __attribute__((format(printf, 3, 4)));
106 
107  void log(uint8_t priority, const char* filen, int fline,
108  const char* fmt, ...)
109  noexcept __attribute__((format(printf, 5, 6)));
110 
111  template<typename T>
112  SWC_SHOULD_NOT_INLINE
113  void msg(uint8_t priority, const T& msg) noexcept {
114  try {
115  MutexSptd::scope lock(mutex);
116  _time_and_level(priority);
117  SWC_LOG_OSTREAM << msg << std::endl;
118  } catch(...) { }
119  }
120 
121  template<typename T>
122  SWC_SHOULD_NOT_INLINE
123  void msg(uint8_t priority, const char* filen, int fline,
124  const T& msg) noexcept {
125  try {
126  MutexSptd::scope lock(mutex);
127  _print_prefix(priority, filen, fline);
128  SWC_LOG_OSTREAM << msg << std::endl;
129  } catch(...) { }
130  }
131 
132  void _print_prefix(uint8_t priority, const char* filen, int fline);
133 
134 
135  private:
136 
137  void _time_and_level(uint8_t priority);
138 
139  void _renew_files(time_t secs);
140 
141  std::string m_name;
142  std::string m_logs_path;
143  FILE* m_file_out;
144  //FILE* m_file_err;
147  bool m_daemon;
148  time_t m_next_time;
149 };
150 
151 
152 extern LogWriter logger;
153 
154 }} // namespace SWC::Core
155 
156 
157 
158 
165 #define SWC_LOG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
166 
167 #define SWC_PRINT { try { \
168  ::SWC::Core::MutexSptd::scope \
169  swcdb_logger_lock(::SWC::Core::logger.mutex); \
170  SWC_LOG_OSTREAM
171 #define SWC_PRINT_CLOSE std::endl; } catch(...) { } }
172 
173 
174 #ifndef SWC_DISABLE_LOG_ALL
175 
176 
177 // stream interface, SWC_LOG_OUT(LOG_ERROR, code{SWC_LOG_OSTREAM << "msg";});
178 #define SWC_LOG_OUT(pr, _code_) { \
179  uint8_t _log_pr = pr; \
180  if(::SWC::Core::logger.is_enabled(_log_pr)) { try { \
181  ::SWC::Core::MutexSptd::scope \
182  swcdb_logger_lock(::SWC::Core::logger.mutex); \
183  ::SWC::Core::logger._print_prefix(_log_pr, __FILE__, __LINE__); \
184  {_code_}; \
185  SWC_LOG_OSTREAM << std::endl; \
186  } catch(...) { } } }
187 
188 #define SWC_LOGF(priority, fmt, ...) \
189  SWC_LOG_OUT(priority, SWC_LOG_PRINTF(fmt, __VA_ARGS__); )
190 
191 #define SWC_LOG(priority, message) \
192  if(::SWC::Core::logger.is_enabled(priority)) { \
193  if(::SWC::Core::logger.show_line_numbers()) \
194  ::SWC::Core::logger.msg(priority, __FILE__, __LINE__, message); \
195  else \
196  ::SWC::Core::logger.msg(priority, message); \
197  }
198 
199 
200 #ifndef SWC_DISABLE_LOG_FATAL
201 #define SWC_LOG_FATAL(msg) do { \
202  SWC_LOG(::SWC::LOG_FATAL, msg); \
203  SWC_ABORT; \
204 } while (0)
205 #define SWC_LOG_FATALF(msg, ...) do { \
206  SWC_LOGF(::SWC::LOG_FATAL, msg, __VA_ARGS__); \
207  SWC_ABORT; \
208 } while (0)
209 #else
210 #define SWC_LOG_FATAL(msg)
211 #define SWC_LOG_FATALF(msg, ...)
212 #endif
213 
214 #else // SWC_DISABLE_LOGGING
215 
216 #define SWC_LOG(priority, msg)
217 #define SWC_LOGF(priority, fmt, ...)
218 
219 #define SWC_LOG_OUT(pr, code)
220 
221 #define SWC_LOG_FATAL(msg)
222 #define SWC_LOG_FATAL(msg, ...)
223 
224 #endif // SWC_DISABLE_LOGGING
225 
226 
227 
233 #ifdef SWC_IMPL_SOURCE
234 #include "swcdb/core/Logger.cc"
235 #endif
236 
237 #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:95
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:80
SWC::Core::LogWriter::name
constexpr SWC_CAN_INLINE const std::string & name() const noexcept
Definition: Logger.h:80
SWC::LOG_FATAL
@ LOG_FATAL
Definition: Logger.h:29
SWC::Core::LogWriter::m_logs_path
std::string m_logs_path
Definition: Logger.h:142
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:141
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Core::LogWriter::m_next_time
time_t m_next_time
Definition: Logger.h:148
SWC::Core::LogWriter::get_level
constexpr SWC_CAN_INLINE uint8_t get_level() const noexcept
Definition: Logger.h:90
SWC::Core::LogWriter::LogWriter
LogWriter() noexcept
Definition: Logger.cc:65
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:143
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:145
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:113
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:195
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:123
SWC::Core::LogWriter::m_daemon
bool m_daemon
Definition: Logger.h:147
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:162
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:100
MutexSptd.h
SWC::Core::LogWriter::_renew_files
void _renew_files(time_t secs)
Definition: Logger.cc:117
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:146
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:101
SWC::Core::LogWriter::set_level
constexpr SWC_CAN_INLINE void set_level(uint8_t level) noexcept
Definition: Logger.h:85
SWC::Core::LogWriter::repr
static std::string repr(uint8_t priority)
Definition: Logger.cc:38