cpp_redis  4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
logger.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <memory>
26 #include <mutex>
27 #include <string>
28 
29 namespace cpp_redis {
30 
35 class logger_iface {
36 public:
38  logger_iface(void) = default;
40  virtual ~logger_iface(void) = default;
41 
43  logger_iface(const logger_iface&) = default;
45  logger_iface& operator=(const logger_iface&) = default;
46 
47 public:
55  virtual void debug(const std::string& msg, const std::string& file, std::size_t line) = 0;
56 
64  virtual void info(const std::string& msg, const std::string& file, std::size_t line) = 0;
65 
73  virtual void warn(const std::string& msg, const std::string& file, std::size_t line) = 0;
74 
82  virtual void error(const std::string& msg, const std::string& file, std::size_t line) = 0;
83 };
84 
88 class logger : public logger_iface {
89 public:
93  enum class log_level {
94  error = 0,
95  warn = 1,
96  info = 2,
97  debug = 3
98  };
99 
100 public:
102  logger(log_level level = log_level::info);
104  ~logger(void) = default;
105 
107  logger(const logger&) = default;
109  logger& operator=(const logger&) = default;
110 
111 public:
119  void debug(const std::string& msg, const std::string& file, std::size_t line);
120 
128  void info(const std::string& msg, const std::string& file, std::size_t line);
129 
137  void warn(const std::string& msg, const std::string& file, std::size_t line);
138 
146  void error(const std::string& msg, const std::string& file, std::size_t line);
147 
148 private:
152  log_level m_level;
153 
157  std::mutex m_mutex;
158 };
159 
164 extern std::unique_ptr<logger_iface> active_logger;
165 
174 void debug(const std::string& msg, const std::string& file, std::size_t line);
175 
184 void info(const std::string& msg, const std::string& file, std::size_t line);
185 
194 void warn(const std::string& msg, const std::string& file, std::size_t line);
195 
204 void error(const std::string& msg, const std::string& file, std::size_t line);
205 
209 #ifdef __CPP_REDIS_LOGGING_ENABLED
210 #define __CPP_REDIS_LOG(level, msg) cpp_redis::level(msg, __FILE__, __LINE__);
211 #else
212 #define __CPP_REDIS_LOG(level, msg)
213 #endif /* __CPP_REDIS_LOGGING_ENABLED */
214 
215 } // namespace cpp_redis
log_level
Definition: logger.hpp:93
logger_iface(void)=default
ctor
virtual void error(const std::string &msg, const std::string &file, std::size_t line)=0
virtual ~logger_iface(void)=default
dtor
virtual void warn(const std::string &msg, const std::string &file, std::size_t line)=0
virtual void debug(const std::string &msg, const std::string &file, std::size_t line)=0
Definition: logger.hpp:88
Definition: logger.hpp:35
logger_iface & operator=(const logger_iface &)=default
assignment operator
virtual void info(const std::string &msg, const std::string &file, std::size_t line)=0
Definition: array_builder.hpp:29