诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

216 lines
5.9 KiB

  1. // MIT License
  2. //
  3. // Copyright (c) 2016-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. #pragma once
  23. #include <memory>
  24. #include <mutex>
  25. #include <string>
  26. namespace tacopie {
  27. //!
  28. //! logger_iface
  29. //! should be inherited by any class intended to be used for logging
  30. //!
  31. class logger_iface {
  32. public:
  33. //! ctor
  34. logger_iface(void) = default;
  35. //! dtor
  36. virtual ~logger_iface(void) = default;
  37. //! copy ctor
  38. logger_iface(const logger_iface&) = default;
  39. //! assignment operator
  40. logger_iface& operator=(const logger_iface&) = default;
  41. public:
  42. //!
  43. //! debug logging
  44. //!
  45. //! \param msg message to be logged
  46. //! \param file file from which the message is coming
  47. //! \param line line in the file of the message
  48. //!
  49. virtual void debug(const std::string& msg, const std::string& file, std::size_t line) = 0;
  50. //!
  51. //! info logging
  52. //!
  53. //! \param msg message to be logged
  54. //! \param file file from which the message is coming
  55. //! \param line line in the file of the message
  56. //!
  57. virtual void info(const std::string& msg, const std::string& file, std::size_t line) = 0;
  58. //!
  59. //! warn logging
  60. //!
  61. //! \param msg message to be logged
  62. //! \param file file from which the message is coming
  63. //! \param line line in the file of the message
  64. //!
  65. virtual void warn(const std::string& msg, const std::string& file, std::size_t line) = 0;
  66. //!
  67. //! error logging
  68. //!
  69. //! \param msg message to be logged
  70. //! \param file file from which the message is coming
  71. //! \param line line in the file of the message
  72. //!
  73. virtual void error(const std::string& msg, const std::string& file, std::size_t line) = 0;
  74. };
  75. //!
  76. //! default logger class provided by the library
  77. //!
  78. class logger : public logger_iface {
  79. public:
  80. //!
  81. //! log level
  82. //!
  83. enum class log_level {
  84. error = 0,
  85. warn = 1,
  86. info = 2,
  87. debug = 3
  88. };
  89. public:
  90. //! ctor
  91. logger(log_level level = log_level::info);
  92. //! dtor
  93. ~logger(void) = default;
  94. //! copy ctor
  95. logger(const logger&) = default;
  96. //! assignment operator
  97. logger& operator=(const logger&) = default;
  98. public:
  99. //!
  100. //! debug logging
  101. //!
  102. //! \param msg message to be logged
  103. //! \param file file from which the message is coming
  104. //! \param line line in the file of the message
  105. //!
  106. void debug(const std::string& msg, const std::string& file, std::size_t line);
  107. //!
  108. //! info logging
  109. //!
  110. //! \param msg message to be logged
  111. //! \param file file from which the message is coming
  112. //! \param line line in the file of the message
  113. //!
  114. void info(const std::string& msg, const std::string& file, std::size_t line);
  115. //!
  116. //! warn logging
  117. //!
  118. //! \param msg message to be logged
  119. //! \param file file from which the message is coming
  120. //! \param line line in the file of the message
  121. //!
  122. void warn(const std::string& msg, const std::string& file, std::size_t line);
  123. //!
  124. //! error logging
  125. //!
  126. //! \param msg message to be logged
  127. //! \param file file from which the message is coming
  128. //! \param line line in the file of the message
  129. //!
  130. void error(const std::string& msg, const std::string& file, std::size_t line);
  131. private:
  132. //!
  133. //! current log level in use
  134. //!
  135. log_level m_level;
  136. //!
  137. //! mutex used to serialize logs in multithreaded environment
  138. //!
  139. std::mutex m_mutex;
  140. };
  141. //!
  142. //! variable containing the current logger
  143. //! by default, not set (no logs)
  144. //!
  145. extern std::unique_ptr<logger_iface> active_logger;
  146. //!
  147. //! debug logging
  148. //! convenience function used internaly to call the logger
  149. //!
  150. //! \param msg message to be logged
  151. //! \param file file from which the message is coming
  152. //! \param line line in the file of the message
  153. //!
  154. void debug(const std::string& msg, const std::string& file, std::size_t line);
  155. //!
  156. //! info logging
  157. //! convenience function used internaly to call the logger
  158. //!
  159. //! \param msg message to be logged
  160. //! \param file file from which the message is coming
  161. //! \param line line in the file of the message
  162. //!
  163. void info(const std::string& msg, const std::string& file, std::size_t line);
  164. //!
  165. //! warn logging
  166. //! convenience function used internaly to call the logger
  167. //!
  168. //! \param msg message to be logged
  169. //! \param file file from which the message is coming
  170. //! \param line line in the file of the message
  171. //!
  172. void warn(const std::string& msg, const std::string& file, std::size_t line);
  173. //!
  174. //! error logging
  175. //! convenience function used internaly to call the logger
  176. //!
  177. //! \param msg message to be logged
  178. //! \param file file from which the message is coming
  179. //! \param line line in the file of the message
  180. //!
  181. void error(const std::string& msg, const std::string& file, std::size_t line);
  182. //!
  183. //! convenience macro to log with file and line information
  184. //!
  185. #ifdef __TACOPIE_LOGGING_ENABLED
  186. #define __TACOPIE_LOG(level, msg) tacopie::level(msg, __FILE__, __LINE__);
  187. #else
  188. #define __TACOPIE_LOG(level, msg)
  189. #endif /* __TACOPIE_LOGGING_ENABLED */
  190. } // namespace tacopie