logger.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_LOGGER_HPP
  7. #define BOOST_REDIS_LOGGER_HPP
  8. #include <boost/redis/response.hpp>
  9. #include <boost/asio/ip/tcp.hpp>
  10. #include <string>
  11. namespace boost::system {class error_code;}
  12. namespace boost::redis {
  13. /** @brief Logger class
  14. * @ingroup high-level-api
  15. *
  16. * The class can be passed to the connection objects to log to `std::clog`
  17. *
  18. * Notice that currently this class has no stable interface. Users
  19. * that don't want any logging can disable it by contructing a logger
  20. * with logger::level::emerg to the connection.
  21. */
  22. class logger {
  23. public:
  24. /** @brief Syslog-like log levels
  25. * @ingroup high-level-api
  26. */
  27. enum class level
  28. {
  29. /// Disabled
  30. disabled,
  31. /// Emergency
  32. emerg,
  33. /// Alert
  34. alert,
  35. /// Critical
  36. crit,
  37. /// Error
  38. err,
  39. /// Warning
  40. warning,
  41. /// Notice
  42. notice,
  43. /// Info
  44. info,
  45. /// Debug
  46. debug
  47. };
  48. /** @brief Constructor
  49. * @ingroup high-level-api
  50. *
  51. * @param l Log level.
  52. */
  53. logger(level l = level::disabled)
  54. : level_{l}
  55. {}
  56. /** @brief Called when the resolve operation completes.
  57. * @ingroup high-level-api
  58. *
  59. * @param ec Error returned by the resolve operation.
  60. * @param res Resolve results.
  61. */
  62. void on_resolve(system::error_code const& ec, asio::ip::tcp::resolver::results_type const& res);
  63. /** @brief Called when the connect operation completes.
  64. * @ingroup high-level-api
  65. *
  66. * @param ec Error returned by the connect operation.
  67. * @param ep Endpoint to which the connection connected.
  68. */
  69. void on_connect(system::error_code const& ec, asio::ip::tcp::endpoint const& ep);
  70. /** @brief Called when the ssl handshake operation completes.
  71. * @ingroup high-level-api
  72. *
  73. * @param ec Error returned by the handshake operation.
  74. */
  75. void on_ssl_handshake(system::error_code const& ec);
  76. /** @brief Called when the connection is lost.
  77. * @ingroup high-level-api
  78. *
  79. * @param ec Error returned when the connection is lost.
  80. */
  81. void on_connection_lost(system::error_code const& ec);
  82. /** @brief Called when the write operation completes.
  83. * @ingroup high-level-api
  84. *
  85. * @param ec Error code returned by the write operation.
  86. * @param payload The payload written to the socket.
  87. */
  88. void on_write(system::error_code const& ec, std::string const& payload);
  89. /** @brief Called when the read operation completes.
  90. * @ingroup high-level-api
  91. *
  92. * @param ec Error code returned by the read operation.
  93. * @param n Number of bytes read.
  94. */
  95. void on_read(system::error_code const& ec, std::size_t n);
  96. /** @brief Called when the run operation completes.
  97. * @ingroup high-level-api
  98. *
  99. * @param reader_ec Error code returned by the read operation.
  100. * @param writer_ec Error code returned by the write operation.
  101. */
  102. void on_run(system::error_code const& reader_ec, system::error_code const& writer_ec);
  103. /** @brief Called when the `HELLO` request completes.
  104. * @ingroup high-level-api
  105. *
  106. * @param ec Error code returned by the async_exec operation.
  107. * @param resp Response sent by the Redis server.
  108. */
  109. void on_hello(system::error_code const& ec, generic_response const& resp);
  110. /** @brief Sets a prefix to every log message
  111. * @ingroup high-level-api
  112. *
  113. * @param prefix The prefix.
  114. */
  115. void set_prefix(std::string_view prefix)
  116. {
  117. prefix_ = prefix;
  118. }
  119. /** @brief Called when the runner operation completes.
  120. * @ingroup high-level-api
  121. *
  122. * @param run_all_ec Error code returned by the run_all operation.
  123. * @param health_check_ec Error code returned by the health checker operation.
  124. * @param hello_ec Error code returned by the health checker operation.
  125. */
  126. void
  127. on_runner(
  128. system::error_code const& run_all_ec,
  129. system::error_code const& health_check_ec,
  130. system::error_code const& hello_ec);
  131. void
  132. on_check_health(
  133. system::error_code const& ping_ec,
  134. system::error_code const& check_timeout_ec);
  135. void trace(std::string_view reason);
  136. private:
  137. void write_prefix();
  138. level level_;
  139. std::string_view prefix_;
  140. };
  141. } // boost::redis
  142. #endif // BOOST_REDIS_LOGGER_HPP