logger.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
  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 <functional>
  9. #include <string>
  10. #include <string_view>
  11. namespace boost::redis {
  12. /** @brief Defines logging configuration.
  13. *
  14. * See the member descriptions for more info.
  15. */
  16. struct logger {
  17. /// Syslog-like log levels.
  18. enum class level
  19. {
  20. /// Disabled
  21. disabled,
  22. /// Emergency
  23. emerg,
  24. /// Alert
  25. alert,
  26. /// Critical
  27. crit,
  28. /// Error
  29. err,
  30. /// Warning
  31. warning,
  32. /// Notice
  33. notice,
  34. /// Info
  35. info,
  36. /// Debug
  37. debug,
  38. };
  39. /** @brief Constructor from a level.
  40. *
  41. * Constructs a logger with the specified level
  42. * and a logging function that prints messages to `stderr`.
  43. *
  44. * @param l The value to set @ref lvl to.
  45. *
  46. * @par Exceptions
  47. * No-throw guarantee.
  48. */
  49. logger(level l = level::info);
  50. /** @brief Constructor from a level and a function.
  51. *
  52. * Constructs a logger by setting its members to the specified values.
  53. *
  54. * @param l The value to set @ref lvl to.
  55. * @param fn The value to set @ref fn to.
  56. *
  57. * @par Exceptions
  58. * No-throw guarantee.
  59. */
  60. logger(level l, std::function<void(level, std::string_view)> fn)
  61. : lvl{l}
  62. , fn{std::move(fn)}
  63. { }
  64. /**
  65. * @brief Defines a severity filter for messages.
  66. *
  67. * Only messages with a level >= to the one specified by the logger
  68. * will be logged.
  69. */
  70. level lvl;
  71. /**
  72. * @brief Defines a severity filter for messages.
  73. *
  74. * Only messages with a level >= to the one specified by the logger
  75. * will be logged.
  76. */
  77. std::function<void(level, std::string_view)> fn;
  78. };
  79. namespace detail {
  80. struct buffered_logger {
  81. logger lgr;
  82. std::string buffer{};
  83. };
  84. } // namespace detail
  85. } // namespace boost::redis
  86. #endif // BOOST_REDIS_LOGGER_HPP